类java.nio.channels.IllegalBlockingModeException源码实例Demo

下面列出了怎么用java.nio.channels.IllegalBlockingModeException的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * 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;
}
 
源代码2 项目: dragonwell8_jdk   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码3 项目: TencentKona-8   文件: AbstractSelectableChannel.java
/**
 * 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;
}
 
源代码4 项目: TencentKona-8   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码5 项目: openjdk-jdk8u   文件: AbstractSelectableChannel.java
/**
 * 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;
}
 
源代码6 项目: openjdk-jdk8u   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码7 项目: Bytecoder   文件: AbstractSelectableChannel.java
/**
 * 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;
}
 
源代码8 项目: Bytecoder   文件: ServerSocketAdaptor.java
@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();
}
 
源代码9 项目: jdk8u_jdk   文件: AbstractSelectableChannel.java
/**
 * 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;
}
 
源代码10 项目: jdk8u_jdk   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码11 项目: j2objc   文件: SocketChannelTest.java
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());
}
 
源代码12 项目: j2objc   文件: AbstractSelectableChannelTest.java
/**
 * @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
    }
}
 
/**
 * 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;
    }
}
 
源代码14 项目: dragonwell8_jdk   文件: SocketAdaptor.java
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) { }
        }
    }
}
 
源代码15 项目: dragonwell8_jdk   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码16 项目: TencentKona-8   文件: AbstractSelectableChannel.java
/**
 * 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;
    }
}
 
源代码17 项目: TencentKona-8   文件: SocketAdaptor.java
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) { }
        }
    }
}
 
源代码18 项目: TencentKona-8   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码19 项目: openjdk-jdk8u   文件: AbstractSelectableChannel.java
/**
 * 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;
    }
}
 
源代码20 项目: openjdk-jdk8u   文件: SocketAdaptor.java
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) { }
        }
    }
}
 
源代码21 项目: openjdk-jdk8u   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码22 项目: Bytecoder   文件: AbstractSelectableChannel.java
/**
 * 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
{
    if ((ops & ~validOps()) != 0)
        throw new IllegalArgumentException();
    if (!isOpen())
        throw new ClosedChannelException();
    synchronized (regLock) {
        if (isBlocking())
            throw new IllegalBlockingModeException();
        synchronized (keyLock) {
            // re-check if channel has been closed
            if (!isOpen())
                throw new ClosedChannelException();
            SelectionKey k = findKey(sel);
            if (k != null) {
                k.attach(att);
                k.interestOps(ops);
            } else {
                // New registration
                k = ((AbstractSelector)sel).register(this, ops, att);
                addKey(k);
            }
            return k;
        }
    }
}
 
源代码23 项目: Bytecoder   文件: DatagramChannelImpl.java
/**
 * Receives a datagram into the given buffer.
 *
 * @apiNote This method is for use by the socket adaptor. The buffer is
 * assumed to be trusted, meaning it is not accessible to user code.
 *
 * @throws IllegalBlockingModeException if the channel is non-blocking
 * @throws SocketTimeoutException if the timeout elapses
 */
SocketAddress blockingReceive(ByteBuffer dst, long nanos) throws IOException {
    readLock.lock();
    try {
        ensureOpen();
        if (!isBlocking())
            throw new IllegalBlockingModeException();
        SecurityManager sm = System.getSecurityManager();
        boolean connected = isConnected();
        SocketAddress sender;
        do {
            if (nanos > 0) {
                sender = trustedBlockingReceive(dst, nanos);
            } else {
                sender = trustedBlockingReceive(dst);
            }
            // check sender when security manager set and not connected
            if (sm != null && !connected) {
                InetSocketAddress isa = (InetSocketAddress) sender;
                try {
                    sm.checkAccept(isa.getAddress().getHostAddress(), isa.getPort());
                } catch (SecurityException e) {
                    sender = null;
                }
            }
        } while (sender == null);
        return sender;
    } finally {
        readLock.unlock();
    }
}
 
源代码24 项目: Bytecoder   文件: DatagramChannelImpl.java
/**
 * Sends a datagram from the bytes in given buffer.
 *
 * @apiNote This method is for use by the socket adaptor.
 *
 * @throws IllegalBlockingModeException if the channel is non-blocking
 */
void blockingSend(ByteBuffer src, SocketAddress target) throws IOException {
    writeLock.lock();
    try {
        ensureOpen();
        if (!isBlocking())
            throw new IllegalBlockingModeException();
        send(src, target);
    } finally {
        writeLock.unlock();
    }
}
 
源代码25 项目: Bytecoder   文件: SocketChannelImpl.java
/**
 * Attempts to establish a connection to the given socket address with a
 * timeout. Closes the socket if connection cannot be established.
 *
 * @apiNote This method is for use by the socket adaptor.
 *
 * @throws IllegalBlockingModeException if the channel is non-blocking
 * @throws SocketTimeoutException if the read timeout elapses
 */
void blockingConnect(SocketAddress remote, long nanos) throws IOException {
    InetSocketAddress isa = checkRemote(remote);
    try {
        readLock.lock();
        try {
            writeLock.lock();
            try {
                if (!isBlocking())
                    throw new IllegalBlockingModeException();
                boolean connected = false;
                try {
                    beginConnect(true, isa);
                    // change socket to non-blocking
                    lockedConfigureBlocking(false);
                    try {
                        int n = Net.connect(fd, isa.getAddress(), isa.getPort());
                        connected = (n > 0) ? true : finishTimedConnect(nanos);
                    } finally {
                        // restore socket to blocking mode (if channel is open)
                        tryLockedConfigureBlocking(true);
                    }
                } finally {
                    endConnect(true, connected);
                }
            } finally {
                writeLock.unlock();
            }
        } finally {
            readLock.unlock();
        }
    } catch (IOException ioe) {
        // connect failed, close the channel
        close();
        throw SocketExceptions.of(ioe, isa);
    }
}
 
源代码26 项目: Bytecoder   文件: SocketChannelImpl.java
/**
 * 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();
    }
}
 
源代码27 项目: jdk8u_jdk   文件: AbstractSelectableChannel.java
/**
 * 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;
    }
}
 
源代码28 项目: jdk8u_jdk   文件: SocketAdaptor.java
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) { }
        }
    }
}
 
源代码29 项目: jdk8u_jdk   文件: DatagramSocketAdaptor.java
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);
        }
    }
}
 
源代码30 项目: jolie   文件: SocketCommChannel.java
/**
 * 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 );
	}
}
 
 类所在包
 同包方法