Search in sources :

Example 61 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project netty by netty.

the class HttpObjectAggregatorTest method testOversizedRequest.

@Test
public void testOversizedRequest() {
    EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4));
    HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost");
    HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII));
    HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII));
    HttpContent chunk3 = LastHttpContent.EMPTY_LAST_CONTENT;
    assertFalse(embedder.writeInbound(message));
    assertFalse(embedder.writeInbound(chunk1));
    assertFalse(embedder.writeInbound(chunk2));
    FullHttpResponse response = embedder.readOutbound();
    assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
    assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH));
    assertFalse(embedder.isOpen());
    try {
        assertFalse(embedder.writeInbound(chunk3));
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof ClosedChannelException);
    }
    assertFalse(embedder.finish());
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test)

Example 62 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project XobotOS by xamarin.

the class FileChannelImpl method transferFrom.

public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
    checkOpen();
    if (!src.isOpen()) {
        throw new ClosedChannelException();
    }
    checkWritable();
    if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }
    if (position > size()) {
        return 0;
    }
    // so the mmap(2) overhead isn't a concern.
    if (src instanceof FileChannel) {
        FileChannel fileSrc = (FileChannel) src;
        long size = fileSrc.size();
        long filePosition = fileSrc.position();
        count = Math.min(count, size - filePosition);
        ByteBuffer buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
        try {
            fileSrc.position(filePosition + count);
            return write(buffer, position);
        } finally {
            NioUtils.freeDirectBuffer(buffer);
        }
    }
    // For non-file channels, all we can do is read and write via userspace.
    ByteBuffer buffer = ByteBuffer.allocate((int) count);
    src.read(buffer);
    buffer.flip();
    return write(buffer, position);
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FileChannel(java.nio.channels.FileChannel)

Example 63 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project XobotOS by xamarin.

the class SocketChannelImpl method finishConnect.

@Override
public boolean finishConnect() throws IOException {
    synchronized (this) {
        if (!isOpen()) {
            throw new ClosedChannelException();
        }
        if (status == SOCKET_STATUS_CONNECTED) {
            return true;
        }
        if (status != SOCKET_STATUS_PENDING) {
            throw new NoConnectionPendingException();
        }
    }
    boolean finished = false;
    try {
        begin();
        InetAddress inetAddress = connectAddress.getAddress();
        int port = connectAddress.getPort();
        // Return immediately.
        finished = IoBridge.isConnected(fd, inetAddress, port, 0, 0);
        isBound = finished;
    } catch (ConnectException e) {
        if (isOpen()) {
            close();
            finished = true;
        }
        throw e;
    } finally {
        end(finished);
    }
    synchronized (this) {
        status = (finished ? SOCKET_STATUS_CONNECTED : status);
        isBound = finished;
    }
    return finished;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) InetAddress(java.net.InetAddress) ConnectException(java.net.ConnectException)

Example 64 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project voltdb by VoltDB.

the class PooledUnsafeDirectByteBuf method setBytes.

@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    checkIndex(index, length);
    ByteBuffer tmpBuf = internalNioBuffer();
    index = idx(index);
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ByteBuffer(java.nio.ByteBuffer)

Example 65 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project voltdb by VoltDB.

the class UnpooledUnsafeDirectByteBuf method setBytes.

@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ensureAccessible();
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index).limit(index + length);
    try {
        return in.read(tmpBuf);
    } catch (ClosedChannelException ignored) {
        return -1;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ClosedChannelException (java.nio.channels.ClosedChannelException)211 ByteBuffer (java.nio.ByteBuffer)67 IOException (java.io.IOException)60 Test (org.junit.Test)23 InetSocketAddress (java.net.InetSocketAddress)19 SelectionKey (java.nio.channels.SelectionKey)18 SocketChannel (java.nio.channels.SocketChannel)15 ArrayList (java.util.ArrayList)13 NotYetConnectedException (java.nio.channels.NotYetConnectedException)11 InterruptedIOException (java.io.InterruptedIOException)10 CancelledKeyException (java.nio.channels.CancelledKeyException)10 ShutdownCommand (com.cloud.agent.api.ShutdownCommand)9 File (java.io.File)9 ServerSocketChannel (java.nio.channels.ServerSocketChannel)9 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ConnectException (java.net.ConnectException)7 FsVolumeReference (org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeReference)6 AgentControlCommand (com.cloud.agent.api.AgentControlCommand)5 Command (com.cloud.agent.api.Command)5