/** * This protected method is used to help subclasses override * <code>ServerSocket.accept()</code>. The passed in socket will be * connected when this method returns. * * @param socket The socket that is used for the accepted connection * * @exception IOException If an error occurs * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode * * @since 1.1 */ protected final void implAccept(Socket socket) throws IOException { if (isClosed()) throw new SocketException("ServerSocket is closed"); // The Sun spec says that if we have an associated channel and // it is in non-blocking mode, we throw an IllegalBlockingModeException. // However, in our implementation if the channel itself initiated this // operation, then we must honor it regardless of its blocking mode. if (getChannel() != null && ! getChannel().isBlocking() && ! ((PlainSocketImpl) getImpl()).isInChannelOperation()) throw new IllegalBlockingModeException(); impl.accept(socket.impl); socket.bound = true; socket.implCreated = true; SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkAccept(socket.getInetAddress().getHostAddress(), socket.getPort()); }
/** * Reads a datagram packet from the socket. Note that this method * will block until a packet is received from the network. On return, * the passed in <code>DatagramPacket</code> is populated with the data * received and all the other information about the packet. * * @param p A <code>DatagramPacket</code> for storing the data * * @exception IOException If an error occurs. * @exception SocketTimeoutException If setSoTimeout was previously called * and the timeout has expired. * @exception PortUnreachableException If the socket is connected to a * currently unreachable destination. Note, there is no guarantee that the * exception will be thrown. * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode. * @exception SecurityException If a security manager exists and its * checkAccept method doesn't allow the receive. */ public synchronized void receive(DatagramPacket p) throws IOException { if (isClosed()) throw new SocketException("socket is closed"); if (remoteAddress != null && remoteAddress.isMulticastAddress()) throw new IOException ("Socket connected to a multicast address my not receive"); if (getChannel() != null && ! getChannel().isBlocking() && ! ((DatagramChannelImpl) getChannel()).isInChannelOperation()) throw new IllegalBlockingModeException(); DatagramPacket p2 = new DatagramPacket(p.getData(), p.getOffset(), p.maxlen); getImpl().receive(p2); p.length = p2.length; if (p2.getAddress() != null) p.setAddress(p2.getAddress()); if (p2.getPort() != -1) p.setPort(p2.getPort()); SecurityManager s = System.getSecurityManager(); if (s != null && isConnected()) s.checkAccept(p.getAddress().getHostAddress(), p.getPort()); }
public int read() throws IOException { if (ch instanceof SelectableChannel && (! ((SelectableChannel) ch).isBlocking())) throw new IllegalBlockingModeException(); ByteBuffer buffer = ByteBuffer.allocate(1); int result = ch.read(buffer); if (result == -1) return -1; if (result == 0) throw new IOException("Could not read from channel"); return buffer.get(0) & 0xff; }
/** * Connects the socket with a remote address. A timeout of zero is * interpreted as an infinite timeout. The connection will then block * until established or an error occurs. * * @param endpoint The address to connect to * @param timeout The length of the timeout in milliseconds, or * 0 to indicate no timeout. * * @exception IOException If an error occurs * @exception IllegalArgumentException If the address type is not supported * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode * @exception SocketTimeoutException If the timeout is reached * * @since 1.4 */ public void connect(SocketAddress endpoint, int timeout) throws IOException { if (isClosed()) throw new SocketException("socket is closed"); if (! (endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException("unsupported address type"); // The Sun spec says that if we have an associated channel and // it is in non-blocking mode, we throw an IllegalBlockingModeException. // However, in our implementation if the channel itself initiated this // operation, then we must honor it regardless of its blocking mode. if (getChannel() != null && ! getChannel().isBlocking() && ! ((PlainSocketImpl) getImpl()).isInChannelOperation()) throw new IllegalBlockingModeException(); if (! isBound()) bind(null); getImpl().connect(endpoint, timeout); }
@Override public void send(DatagramPacket packet) throws IOException { if (!channelImpl.isBlocking()) { throw new IllegalBlockingModeException(); } // DatagramSocket.send() will implicitly bind if it hasn't been done explicitly. Force // bind() here so that the channel state stays in sync with the socket. boolean wasBound = isBound(); super.send(packet); if (!wasBound) { // DatagramSocket.send() will implicitly bind if it hasn't been done explicitly. // Sync the channel state with the socket. channelImpl.onBind(false /* updateSocketState */); } }
@Override public void connect(SocketAddress remoteAddr, int timeout) throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } if (isConnected()) { throw new AlreadyConnectedException(); } super.connect(remoteAddr, timeout); channel.onBind(false); if (super.isConnected()) { InetSocketAddress remoteInetAddress = (InetSocketAddress) remoteAddr; channel.onConnectStatusChanged( remoteInetAddress, SOCKET_STATUS_CONNECTED, false /* updateSocketState */); } }
/** * @tests serialization/deserialization compatibility. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationSelf", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "IllegalBlockingModeException", args = {} ) }) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new IllegalBlockingModeException()); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationGolden", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "IllegalBlockingModeException", args = {} ) }) public void testSerializationCompatibility() throws Exception { SerializationTest .verifyGolden(this, new IllegalBlockingModeException()); }
/** * @tests AbstractSelectableChannel#configureBlocking(boolean) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "configureBlocking", args = {boolean.class} ) public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception { SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); Selector acceptSelector = SelectorProvider.provider().openSelector(); SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null); assertEquals(sc.keyFor(acceptSelector), acceptKey); SelectableChannel getChannel = sc.configureBlocking(false); assertEquals(getChannel, sc); try { sc.configureBlocking(true); fail("Should throw IllegalBlockingModeException"); } catch (IllegalBlockingModeException e) { // expected } }
public void test_accept() throws IOException { int portNumber = Support_PortManager.getNextPort(); ServerSocket newSocket = new ServerSocket(portNumber); newSocket.setSoTimeout(500); try { Socket accepted = newSocket.accept(); fail("SocketTimeoutException was not thrown: " + accepted); } catch(SocketTimeoutException expected) { } newSocket.close(); ServerSocketChannel ssc = ServerSocketChannel.open(); ServerSocket ss = ssc.socket(); try { ss.accept(); fail("IllegalBlockingModeException was not thrown."); } catch(IllegalBlockingModeException ibme) { //expected } finally { ss.close(); ssc.close(); } }
/** * @see java.net.Socket#connect(java.net.SocketAddress, int) */ @Override public void connect(SocketAddress remoteAddr, int timeout) throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } if (isConnected()) { throw new AlreadyConnectedException(); } super.connect(remoteAddr, timeout); channel.localAddress = networkSystem.getSocketLocalAddress( channel.fd, false); if (super.isConnected()) { channel.setConnected(); channel.isBound = super.isBound(); } }
public void testSocket_configureblocking() throws IOException { byte[] serverWBuf = new byte[CAPACITY_NORMAL]; for (int i = 0; i < serverWBuf.length; i++) { serverWBuf[i] = (byte) i; } java.nio.ByteBuffer buf = java.nio.ByteBuffer .allocate(CAPACITY_NORMAL + 1); channel1.connect(localAddr1); server1.accept(); Socket sock = this.channel1.socket(); channel1.configureBlocking(false); assertFalse(channel1.isBlocking()); OutputStream channelSocketOut = sock.getOutputStream(); try { // write operation is not allowed in non-blocking mode channelSocketOut.write(buf.array()); fail("Non-Blocking mode should cause IllegalBlockingModeException"); } catch (IllegalBlockingModeException e) { // correct } channel1.configureBlocking(true); assertTrue(channel1.isBlocking()); // write operation is allowed in blocking mode channelSocketOut.write(buf.array()); }
/** * @tests AbstractSelectableChannel#configureBlocking(boolean) */ public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception { SocketChannel sc = SocketChannel.open(); sc.configureBlocking(false); Selector acceptSelector = SelectorProvider.provider().openSelector(); SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null); assertEquals(sc.keyFor(acceptSelector), acceptKey); SelectableChannel getChannel = sc.configureBlocking(false); assertEquals(getChannel, sc); try { sc.configureBlocking(true); fail("Should throw IllegalBlockingModeException"); } catch (IllegalBlockingModeException e) { // expected } }
/** * Set the blocking mode of this channel. * * @see java.nio.channels.SelectableChannel#configureBlocking(boolean) * @param blockingMode * <code>true</code> for blocking mode; <code>false</code> * for non-blocking mode. */ public final SelectableChannel configureBlocking(boolean blockingMode) throws IOException { if (isOpen()) { synchronized (blockingLock) { if (isBlocking == blockingMode) { return this; } if (blockingMode && isRegistered()) { throw new IllegalBlockingModeException(); } implConfigureBlocking(blockingMode); isBlocking = blockingMode; } return this; } throw new ClosedChannelException(); }
@Override public void connect(SocketAddress remoteAddr, int timeout) throws IOException { if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } if (isConnected()) { throw new AlreadyConnectedException(); } super.connect(remoteAddr, timeout); channel.localAddress = networkSystem.getSocketLocalAddress( channel.fd, false); if (super.isConnected()) { channel.setConnected(); channel.isBound = super.isBound(); } }
/** * Sends the specified packet. The host and port to which the packet * are to be sent should be set inside the packet. * * @param p The datagram packet to send. * * @exception IOException If an error occurs. * @exception SecurityException If a security manager exists and its * checkMulticast or checkConnect method doesn't allow the send. * @exception PortUnreachableException If the socket is connected to a * currently unreachable destination. Note, there is no guarantee that the * exception will be thrown. * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode. */ public void send(DatagramPacket p) throws IOException { if (isClosed()) throw new SocketException("socket is closed"); // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api. SecurityManager s = System.getSecurityManager(); if (s != null && ! isConnected()) { InetAddress addr = p.getAddress(); if (addr.isMulticastAddress()) s.checkMulticast(addr); else s.checkConnect(addr.getHostAddress(), p.getPort()); } if (isConnected()) { if (p.getAddress() != null && (remoteAddress != p.getAddress() || remotePort != p.getPort())) throw new IllegalArgumentException ("DatagramPacket address does not match remote address"); } // FIXME: if this is a subclass of MulticastSocket, // use getTimeToLive for TTL val. if (getChannel() != null && ! getChannel().isBlocking() && ! ((DatagramChannelImpl) getChannel()).isInChannelOperation()) throw new IllegalBlockingModeException(); getImpl().send(p); }
/** * Connects the socket with a remote address. A timeout of zero is * interpreted as an infinite timeout. The connection will then block * until established or an error occurs. * * @param endpoint The address to connect to * @param timeout The length of the timeout in milliseconds, or * 0 to indicate no timeout. * * @exception IOException If an error occurs * @exception IllegalArgumentException If the address type is not supported * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode * @exception SocketTimeoutException If the timeout is reached * @throws SecurityException if the SocketAddress is an {@link InetSocketAddress} * and a security manager is present which does not * allow connections on the given host and port. * @since 1.4 */ public void connect(SocketAddress endpoint, int timeout) throws IOException { if (isClosed()) throw new SocketException("socket is closed"); if (! (endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException("unsupported address type"); SecurityManager sm = System.getSecurityManager(); if (sm != null) { InetSocketAddress inetAddr = (InetSocketAddress) endpoint; sm.checkConnect(inetAddr.getHostName(), inetAddr.getPort()); } // The Sun spec says that if we have an associated channel and // it is in non-blocking mode, we throw an IllegalBlockingModeException. // However, in our implementation if the channel itself initiated this // operation, then we must honor it regardless of its blocking mode. if (getChannel() != null && ! getChannel().isBlocking() && ! ((PlainSocketImpl) getImpl()).isInChannelOperation()) throw new IllegalBlockingModeException(); if (! isBound()) bind(null); getImpl().connect(endpoint, timeout); }
/** * Registers this channel with the given selector, returning a selection key. * * @param selin the seletor to use * @param ops the interested operations * @param att an attachment for the returned selection key * * @return the registered selection key * * @exception ClosedChannelException If the channel is already closed. * @exception IllegalBlockingModeException If the channel is configured in * blocking mode. */ public final SelectionKey register(Selector selin, int ops, Object att) throws ClosedChannelException { if (! isOpen()) throw new ClosedChannelException(); if ((ops & ~validOps()) != 0) throw new IllegalArgumentException(); SelectionKey key = null; AbstractSelector selector = (AbstractSelector) selin; synchronized (blockingLock()) { if (blocking) throw new IllegalBlockingModeException(); key = locate(selector); if (key != null && key.isValid()) { key.interestOps(ops); key.attach(att); } else { key = selector.register(this, ops, att); if (key != null) addSelectionKey(key); } } return key; }
public void connect (SocketAddress endpoint, int timeout) throws IOException { if (isClosed ()) { throw new SocketException ("socket is closed"); } if (! (endpoint instanceof LocalSocketAddress)) { throw new IllegalArgumentException ("socket address is not a local address"); } if (getChannel() != null && !getChannel().isBlocking()) { throw new IllegalBlockingModeException (); } try { localimpl.doCreate (); localimpl.localConnect ((LocalSocketAddress) endpoint); } catch (IOException ioe) { close (); throw ioe; } localConnected = true; }
public int read(byte[] buf, int off, int len) throws IOException { if (ch instanceof SelectableChannel && (! ((SelectableChannel) ch).isBlocking())) throw new IllegalBlockingModeException(); ByteBuffer b = ByteBuffer.wrap(buf, off, len); return ch.read(b); }
/** * Creates a new input stream for the specified channel. * * @param channel * The UDT socket channel. */ protected NioInputStreamUDT(final SocketChannelUDT channel) { if (channel == null) { throw new NullPointerException("channel == null"); } if (!channel.isBlocking()) { throw new IllegalBlockingModeException(); } this.channel = channel; }
/** * Receives a message from the channel. * @return the received CommMessage * @throws java.io.IOException * @see CommMessage */ @Override protected CommMessage recvImpl() throws IOException { try { return protocol().recv( istream, ostream ); } catch( IllegalBlockingModeException e ) { throw new IOException( e ); } }
/** * Sends a message through the channel. * @param message the CommMessage to send * @see CommMessage * @throws IOException if an error sending the message occurs */ @Override protected void sendImpl( CommMessage message ) throws IOException { try { protocol().send( ostream, message, istream ); ostream.flush(); } catch( IllegalBlockingModeException e ) { throw new IOException( e ); } }