java.nio.channels.IllegalBlockingModeException Java Examples

The following examples show how to use java.nio.channels.IllegalBlockingModeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSelectableChannel.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts this channel's blocking mode.
 *
 * <p> If the given blocking mode is different from the current blocking
 * mode then this method invokes the {@link #implConfigureBlocking
 * implConfigureBlocking} method, while holding the appropriate locks, in
 * order to change the mode.  </p>
 */
public final SelectableChannel configureBlocking(boolean block)
    throws IOException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        boolean blocking = !nonBlocking;
        if (block != blocking) {
            if (block && haveValidKeys())
                throw new IllegalBlockingModeException();
            implConfigureBlocking(block);
            nonBlocking = !block;
        }
    }
    return this;
}
 
Example #2
Source File: ServerSocketAdaptor.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public Socket accept() throws IOException {
    SocketChannel sc = null;
    try {
        int timeout = this.timeout;
        if (timeout > 0) {
            long nanos = MILLISECONDS.toNanos(timeout);
            sc = ssc.blockingAccept(nanos);
        } else {
            // accept connection if possible when non-blocking (to preserve
            // long standing behavior)
            sc = ssc.accept();
            if (sc == null) {
                throw new IllegalBlockingModeException();
            }
        }
    } catch (Exception e) {
        Net.translateException(e);
    }
    return sc.socket();
}
 
Example #3
Source File: AbstractSelectableChannel.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts this channel's blocking mode.
 *
 * <p> If the given blocking mode is different from the current blocking
 * mode then this method invokes the {@link #implConfigureBlocking
 * implConfigureBlocking} method, while holding the appropriate locks, in
 * order to change the mode.  </p>
 */
public final SelectableChannel configureBlocking(boolean block)
    throws IOException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        boolean blocking = !nonBlocking;
        if (block != blocking) {
            if (block && haveValidKeys())
                throw new IllegalBlockingModeException();
            implConfigureBlocking(block);
            nonBlocking = !block;
        }
    }
    return this;
}
 
Example #4
Source File: AbstractSelectableChannel.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Adjusts this channel's blocking mode.
 *
 * <p> If the given blocking mode is different from the current blocking
 * mode then this method invokes the {@link #implConfigureBlocking
 * implConfigureBlocking} method, while holding the appropriate locks, in
 * order to change the mode.  </p>
 */
public final SelectableChannel configureBlocking(boolean block)
    throws IOException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        boolean blocking = !nonBlocking;
        if (block != blocking) {
            if (block && haveValidKeys())
                throw new IllegalBlockingModeException();
            implConfigureBlocking(block);
            nonBlocking = !block;
        }
    }
    return this;
}
 
Example #5
Source File: DatagramSocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #6
Source File: DatagramSocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #7
Source File: AbstractSelectableChannel.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts this channel's blocking mode.
 *
 * <p> If the given blocking mode is different from the current blocking
 * mode then this method invokes the {@link #implConfigureBlocking
 * implConfigureBlocking} method, while holding the appropriate locks, in
 * order to change the mode.  </p>
 */
public final SelectableChannel configureBlocking(boolean block)
    throws IOException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        boolean blocking = !nonBlocking;
        if (block != blocking) {
            if (block && haveValidKeys())
                throw new IllegalBlockingModeException();
            implConfigureBlocking(block);
            nonBlocking = !block;
        }
    }
    return this;
}
 
Example #8
Source File: SocketChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #9
Source File: DatagramSocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #10
Source File: AbstractSelectableChannelTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @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
    }
}
 
Example #11
Source File: AbstractSelectableChannel.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adjusts this channel's blocking mode.
 *
 * <p> If the given blocking mode is different from the current blocking
 * mode then this method invokes the {@link #implConfigureBlocking
 * implConfigureBlocking} method, while holding the appropriate locks, in
 * order to change the mode.  </p>
 */
public final SelectableChannel configureBlocking(boolean block)
    throws IOException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        boolean blocking = !nonBlocking;
        if (block != blocking) {
            if (block && haveValidKeys())
                throw new IllegalBlockingModeException();
            implConfigureBlocking(block);
            nonBlocking = !block;
        }
    }
    return this;
}
 
Example #12
Source File: DatagramSocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void receive(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                SocketAddress sender = receive(bb);
                p.setSocketAddress(sender);
                p.setLength(bb.position() - p.getOffset());
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #13
Source File: IllegalBlockingModeExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @tests {@link java.nio.channels.IllegalBlockingModeException#IllegalBlockingModeException()}
 */
public void test_Constructor() {
    IllegalBlockingModeException e = new IllegalBlockingModeException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
 
Example #14
Source File: SocketCommChannel.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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 );
	}
}
 
Example #15
Source File: SocketCommChannel.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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 );
	}
}
 
Example #16
Source File: ChannelsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testStreamNonBlocking() throws IOException {
    Pipe.SourceChannel sourceChannel = createNonBlockingChannel("abc".getBytes("UTF-8"));
    try {
        Channels.newInputStream(sourceChannel).read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
}
 
Example #17
Source File: DatagramSocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void send(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                if (dc.isConnected()) {
                    if (p.getAddress() == null) {
                        // Legacy DatagramSocket will send in this case
                        // and set address and port of the packet
                        InetSocketAddress isa = (InetSocketAddress)
                                                dc.remoteAddress();
                        p.setPort(isa.getPort());
                        p.setAddress(isa.getAddress());
                        dc.write(bb);
                    } else {
                        // Target address may not match connected address
                        dc.send(bb, p.getSocketAddress());
                    }
                } else {
                    // Not connected so address must be valid or throw
                    dc.send(bb, p.getSocketAddress());
                }
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #18
Source File: ChannelsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * This fails on the RI which violates its own promise to throw when
 * read in non-blocking mode.
 */
public void testReaderNonBlocking() throws IOException {
    Pipe.SourceChannel sourceChannel = createNonBlockingChannel("abc".getBytes("UTF-8"));
    try {
        Channels.newReader(sourceChannel, "UTF-8").read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
}
 
Example #19
Source File: SocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int read(ByteBuffer bb)
    throws IOException
{
    synchronized (sc.blockingLock()) {
        if (!sc.isBlocking())
            throw new IllegalBlockingModeException();

        if (timeout == 0)
            return sc.read(bb);

        sc.configureBlocking(false);
        try {
            int n;
            if ((n = sc.read(bb)) != 0)
                return n;
            long to = timeout;
            for (;;) {
                if (!sc.isOpen())
                    throw new ClosedChannelException();
                long st = System.currentTimeMillis();
                int result = sc.poll(Net.POLLIN, to);
                if (result > 0) {
                    if ((n = sc.read(bb)) != 0)
                        return n;
                }
                to -= System.currentTimeMillis() - st;
                if (to <= 0)
                    throw new SocketTimeoutException();
            }
        } finally {
            try {
                sc.configureBlocking(true);
            } catch (ClosedChannelException e) { }
        }
    }
}
 
Example #20
Source File: AbstractSelectableChannel.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers this channel with the given selector, returning a selection key.
 *
 * <p>  This method first verifies that this channel is open and that the
 * given initial interest set is valid.
 *
 * <p> If this channel is already registered with the given selector then
 * the selection key representing that registration is returned after
 * setting its interest set to the given value.
 *
 * <p> Otherwise this channel has not yet been registered with the given
 * selector, so the {@link AbstractSelector#register register} method of
 * the selector is invoked while holding the appropriate locks.  The
 * resulting key is added to this channel's key set before being returned.
 * </p>
 *
 * @throws  ClosedSelectorException {@inheritDoc}
 *
 * @throws  IllegalBlockingModeException {@inheritDoc}
 *
 * @throws  IllegalSelectorException {@inheritDoc}
 *
 * @throws  CancelledKeyException {@inheritDoc}
 *
 * @throws  IllegalArgumentException {@inheritDoc}
 */
public final SelectionKey register(Selector sel, int ops,
                                   Object att)
    throws ClosedChannelException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        if ((ops & ~validOps()) != 0)
            throw new IllegalArgumentException();
        if (isBlocking())
            throw new IllegalBlockingModeException();
        SelectionKey k = findKey(sel);
        if (k != null) {
            k.interestOps(ops);
            k.attach(att);
        }
        if (k == null) {
            // New registration
            synchronized (keyLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                k = ((AbstractSelector)sel).register(this, ops, att);
                addKey(k);
            }
        }
        return k;
    }
}
 
Example #21
Source File: AbstractSelectableChannel.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers this channel with the given selector, returning a selection key.
 *
 * <p>  This method first verifies that this channel is open and that the
 * given initial interest set is valid.
 *
 * <p> If this channel is already registered with the given selector then
 * the selection key representing that registration is returned after
 * setting its interest set to the given value.
 *
 * <p> Otherwise this channel has not yet been registered with the given
 * selector, so the {@link AbstractSelector#register register} method of
 * the selector is invoked while holding the appropriate locks.  The
 * resulting key is added to this channel's key set before being returned.
 * </p>
 *
 * @throws  ClosedSelectorException {@inheritDoc}
 *
 * @throws  IllegalBlockingModeException {@inheritDoc}
 *
 * @throws  IllegalSelectorException {@inheritDoc}
 *
 * @throws  CancelledKeyException {@inheritDoc}
 *
 * @throws  IllegalArgumentException {@inheritDoc}
 */
public final SelectionKey register(Selector sel, int ops,
                                   Object att)
    throws ClosedChannelException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        if ((ops & ~validOps()) != 0)
            throw new IllegalArgumentException();
        if (isBlocking())
            throw new IllegalBlockingModeException();
        SelectionKey k = findKey(sel);
        if (k != null) {
            k.interestOps(ops);
            k.attach(att);
        }
        if (k == null) {
            // New registration
            synchronized (keyLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                k = ((AbstractSelector)sel).register(this, ops, att);
                addKey(k);
            }
        }
        return k;
    }
}
 
Example #22
Source File: SeekableOutputStream.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Write all remaining bytes in buffer to the given channel.
 * 
 * @throws IllegalBlockingException
 *             If the channel is selectable and configured non-blocking.
 */
private static void writeFully(WritableByteChannel ch, ByteBuffer bb) throws IOException {
	if (ch instanceof SelectableChannel) {
		SelectableChannel sc = (SelectableChannel) ch;
		synchronized (sc.blockingLock()) {
			if (!sc.isBlocking()) {
				throw new IllegalBlockingModeException();
			}
			writeFullyImpl(ch, bb);
		}
	} else {
		writeFullyImpl(ch, bb);
	}
}
 
Example #23
Source File: AbstractSelectableChannel.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers this channel with the given selector, returning a selection key.
 *
 * <p>  This method first verifies that this channel is open and that the
 * given initial interest set is valid.
 *
 * <p> If this channel is already registered with the given selector then
 * the selection key representing that registration is returned after
 * setting its interest set to the given value.
 *
 * <p> Otherwise this channel has not yet been registered with the given
 * selector, so the {@link AbstractSelector#register register} method of
 * the selector is invoked while holding the appropriate locks.  The
 * resulting key is added to this channel's key set before being returned.
 * </p>
 *
 * @throws  ClosedSelectorException {@inheritDoc}
 *
 * @throws  IllegalBlockingModeException {@inheritDoc}
 *
 * @throws  IllegalSelectorException {@inheritDoc}
 *
 * @throws  CancelledKeyException {@inheritDoc}
 *
 * @throws  IllegalArgumentException {@inheritDoc}
 */
public final SelectionKey register(Selector sel, int ops,
                                   Object att)
    throws ClosedChannelException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        if ((ops & ~validOps()) != 0)
            throw new IllegalArgumentException();
        if (isBlocking())
            throw new IllegalBlockingModeException();
        SelectionKey k = findKey(sel);
        if (k != null) {
            k.interestOps(ops);
            k.attach(att);
        }
        if (k == null) {
            // New registration
            synchronized (keyLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                k = ((AbstractSelector)sel).register(this, ops, att);
                addKey(k);
            }
        }
        return k;
    }
}
 
Example #24
Source File: SocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected int read(ByteBuffer bb)
    throws IOException
{
    synchronized (sc.blockingLock()) {
        if (!sc.isBlocking())
            throw new IllegalBlockingModeException();

        if (timeout == 0)
            return sc.read(bb);

        sc.configureBlocking(false);
        try {
            int n;
            if ((n = sc.read(bb)) != 0)
                return n;
            long to = timeout;
            for (;;) {
                if (!sc.isOpen())
                    throw new ClosedChannelException();
                long st = System.currentTimeMillis();
                int result = sc.poll(Net.POLLIN, to);
                if (result > 0) {
                    if ((n = sc.read(bb)) != 0)
                        return n;
                }
                to -= System.currentTimeMillis() - st;
                if (to <= 0)
                    throw new SocketTimeoutException();
            }
        } finally {
            try {
                sc.configureBlocking(true);
            } catch (ClosedChannelException e) { }
        }
    }
}
 
Example #25
Source File: DatagramSocketAdaptor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void send(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                if (dc.isConnected()) {
                    if (p.getAddress() == null) {
                        // Legacy DatagramSocket will send in this case
                        // and set address and port of the packet
                        InetSocketAddress isa = (InetSocketAddress)
                                                dc.remoteAddress();
                        p.setPort(isa.getPort());
                        p.setAddress(isa.getAddress());
                        dc.write(bb);
                    } else {
                        // Target address may not match connected address
                        dc.send(bb, p.getSocketAddress());
                    }
                } else {
                    // Not connected so address must be valid or throw
                    dc.send(bb, p.getSocketAddress());
                }
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #26
Source File: AbstractSelectableChannel.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers this channel with the given selector, returning a selection key.
 *
 * <p>  This method first verifies that this channel is open and that the
 * given initial interest set is valid.
 *
 * <p> If this channel is already registered with the given selector then
 * the selection key representing that registration is returned after
 * setting its interest set to the given value.
 *
 * <p> Otherwise this channel has not yet been registered with the given
 * selector, so the {@link AbstractSelector#register register} method of
 * the selector is invoked while holding the appropriate locks.  The
 * resulting key is added to this channel's key set before being returned.
 * </p>
 *
 * @throws  ClosedSelectorException {@inheritDoc}
 *
 * @throws  IllegalBlockingModeException {@inheritDoc}
 *
 * @throws  IllegalSelectorException {@inheritDoc}
 *
 * @throws  CancelledKeyException {@inheritDoc}
 *
 * @throws  IllegalArgumentException {@inheritDoc}
 */
public final SelectionKey register(Selector sel, int ops,
                                   Object att)
    throws ClosedChannelException
{
    synchronized (regLock) {
        if (!isOpen())
            throw new ClosedChannelException();
        if ((ops & ~validOps()) != 0)
            throw new IllegalArgumentException();
        if (isBlocking())
            throw new IllegalBlockingModeException();
        SelectionKey k = findKey(sel);
        if (k != null) {
            k.interestOps(ops);
            k.attach(att);
        }
        if (k == null) {
            // New registration
            synchronized (keyLock) {
                if (!isOpen())
                    throw new ClosedChannelException();
                k = ((AbstractSelector)sel).register(this, ops, att);
                addKey(k);
            }
        }
        return k;
    }
}
 
Example #27
Source File: SocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected int read(ByteBuffer bb)
    throws IOException
{
    synchronized (sc.blockingLock()) {
        if (!sc.isBlocking())
            throw new IllegalBlockingModeException();

        if (timeout == 0)
            return sc.read(bb);

        sc.configureBlocking(false);
        try {
            int n;
            if ((n = sc.read(bb)) != 0)
                return n;
            long to = timeout;
            for (;;) {
                if (!sc.isOpen())
                    throw new ClosedChannelException();
                long st = System.currentTimeMillis();
                int result = sc.poll(Net.POLLIN, to);
                if (result > 0) {
                    if ((n = sc.read(bb)) != 0)
                        return n;
                }
                to -= System.currentTimeMillis() - st;
                if (to <= 0)
                    throw new SocketTimeoutException();
            }
        } finally {
            try {
                sc.configureBlocking(true);
            } catch (ClosedChannelException e) { }
        }
    }
}
 
Example #28
Source File: DatagramSocketAdaptor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void send(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                if (dc.isConnected()) {
                    if (p.getAddress() == null) {
                        // Legacy DatagramSocket will send in this case
                        // and set address and port of the packet
                        InetSocketAddress isa = (InetSocketAddress)
                                                dc.remoteAddress();
                        p.setPort(isa.getPort());
                        p.setAddress(isa.getAddress());
                        dc.write(bb);
                    } else {
                        // Target address may not match connected address
                        dc.send(bb, p.getSocketAddress());
                    }
                } else {
                    // Not connected so address must be valid or throw
                    dc.send(bb, p.getSocketAddress());
                }
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example #29
Source File: SocketChannelImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a sequence of bytes to the socket from the given byte array.
 *
 * @apiNote This method is for use by the socket adaptor.
 */
void blockingWriteFully(byte[] b, int off, int len) throws IOException {
    Objects.checkFromIndexSize(off, len, b.length);
    if (len == 0) {
        // nothing to do
        return;
    }

    writeLock.lock();
    try {
        // check that channel is configured blocking
        if (!isBlocking())
            throw new IllegalBlockingModeException();

        // loop until all bytes have been written
        int pos = off;
        int end = off + len;
        beginWrite(true);
        try {
            while (pos < end && isOpen()) {
                int size = end - pos;
                int n = tryWrite(b, pos, size);
                while (IOStatus.okayToRetry(n) && isOpen()) {
                    park(Net.POLLOUT);
                    n = tryWrite(b, pos, size);
                }
                if (n > 0) {
                    pos += n;
                }
            }
        } finally {
            endWrite(true, pos >= end);
        }
    } finally {
        writeLock.unlock();
    }
}
 
Example #30
Source File: SocketAdaptor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected int read(ByteBuffer bb)
    throws IOException
{
    synchronized (sc.blockingLock()) {
        if (!sc.isBlocking())
            throw new IllegalBlockingModeException();

        if (timeout == 0)
            return sc.read(bb);

        sc.configureBlocking(false);
        try {
            int n;
            if ((n = sc.read(bb)) != 0)
                return n;
            long to = timeout;
            for (;;) {
                if (!sc.isOpen())
                    throw new ClosedChannelException();
                long st = System.currentTimeMillis();
                int result = sc.poll(Net.POLLIN, to);
                if (result > 0) {
                    if ((n = sc.read(bb)) != 0)
                        return n;
                }
                to -= System.currentTimeMillis() - st;
                if (to <= 0)
                    throw new SocketTimeoutException();
            }
        } finally {
            try {
                sc.configureBlocking(true);
            } catch (ClosedChannelException e) { }
        }
    }
}