0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4669040
0N/A * @summary Test DatagramChannel receive with empty buffer
0N/A * @author Mike McCloskey
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.charset.*;
0N/A
0N/Apublic class EmptyBuffer {
0N/A
0N/A static PrintStream log = System.err;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A test();
0N/A }
0N/A
0N/A static void test() throws Exception {
2475N/A Server server = new Server();
0N/A Thread serverThread = new Thread(server);
0N/A serverThread.start();
0N/A DatagramChannel dc = DatagramChannel.open();
2546N/A try {
2546N/A ByteBuffer bb = ByteBuffer.allocateDirect(12);
2546N/A bb.order(ByteOrder.BIG_ENDIAN);
2546N/A bb.putInt(1).putLong(1);
2546N/A bb.flip();
2546N/A InetAddress address = InetAddress.getLocalHost();
2546N/A InetSocketAddress isa = new InetSocketAddress(address, server.port());
2546N/A dc.connect(isa);
2546N/A dc.write(bb);
2546N/A bb.rewind();
2546N/A dc.write(bb);
2546N/A bb.rewind();
2546N/A dc.write(bb);
2546N/A Thread.sleep(2000);
2546N/A serverThread.interrupt();
2546N/A server.throwException();
2546N/A } finally {
2546N/A dc.close();
2546N/A }
0N/A }
0N/A
2475N/A public static class Server implements Runnable {
2475N/A final DatagramChannel dc;
2475N/A Exception e = null;
0N/A
2475N/A Server() throws IOException {
2475N/A this.dc = DatagramChannel.open().bind(new InetSocketAddress(0));
2475N/A }
0N/A
2475N/A int port() {
2475N/A return dc.socket().getLocalPort();
2475N/A }
2475N/A
2475N/A void throwException() throws Exception {
0N/A if (e != null)
0N/A throw e;
0N/A }
0N/A
0N/A void showBuffer(String s, ByteBuffer bb) {
0N/A log.println(s);
0N/A bb.rewind();
0N/A for (int i=0; i<bb.limit(); i++) {
0N/A byte element = bb.get();
0N/A log.print(element);
0N/A }
0N/A log.println();
0N/A }
0N/A
0N/A public void run() {
0N/A SocketAddress sa = null;
0N/A int numberReceived = 0;
0N/A try {
0N/A ByteBuffer bb = ByteBuffer.allocateDirect(12);
0N/A bb.clear();
0N/A // Only one clear. The buffer will be full after
0N/A // the first receive, but it should still block
0N/A // and receive and discard the next two
0N/A while (!Thread.interrupted()) {
0N/A try {
0N/A sa = dc.receive(bb);
0N/A } catch (ClosedByInterruptException cbie) {
0N/A // Expected
0N/A log.println("Took expected exit");
0N/A break;
0N/A }
0N/A if (sa != null) {
0N/A log.println("Client: " + sa);
0N/A showBuffer("RECV", bb);
0N/A sa = null;
0N/A numberReceived++;
0N/A if (numberReceived > 3)
0N/A throw new RuntimeException("Test failed");
0N/A }
0N/A }
0N/A } catch (Exception ex) {
0N/A e = ex;
2546N/A } finally {
2546N/A try { dc.close(); } catch (IOException ignore) { }
0N/A }
0N/A }
0N/A }
0N/A
0N/A}