Search in sources :

Example 16 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project dubbo by alibaba.

the class HeapChannelBuffer method setBytes.

public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
    ByteBuffer buf = ByteBuffer.wrap(array, index, length);
    int readBytes = 0;
    do {
        int localReadBytes;
        try {
            localReadBytes = in.read(buf);
        } catch (ClosedChannelException e) {
            localReadBytes = -1;
        }
        if (localReadBytes < 0) {
            if (readBytes == 0) {
                return -1;
            } else {
                break;
            }
        } else if (localReadBytes == 0) {
            break;
        }
        readBytes += localReadBytes;
    } while (readBytes < length);
    return readBytes;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) ByteBuffer(java.nio.ByteBuffer)

Example 17 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project Openfire by igniterealtime.

the class MemberReceiver method register.

public SelectionKey register(Selector selector) throws IOException {
    try {
        selectionKey = datagramChannel.register(selector, SelectionKey.OP_READ);
    } catch (ClosedChannelException e) {
        callHandler.cancelRequest("register failed, channel closed!");
        throw new IOException("register failed, channel closed!");
    } catch (Exception e) {
        Logger.println("register exception! " + e.getMessage());
        throw new IOException("register exception!  " + e.getMessage());
    }
    datagramChannelRegistered = true;
    selectionKey.attach(this);
    return selectionKey;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) SpeexException(com.sun.voip.SpeexException) SocketException(java.net.SocketException) NoSuchElementException(java.util.NoSuchElementException) ParseException(java.text.ParseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 18 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project NabAlive by jcheype.

the class HttpApiServerHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    if (e.getCause() instanceof ClosedChannelException) {
        return;
    }
    String errorid = UUID.randomUUID().toString();
    logger.error("ERROR HTTP: {}\n", errorid, e.getCause());
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    httpResponse.setHeader("id", errorid);
    if (logger.isDebugEnabled()) {
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
        PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
        e.getCause().printStackTrace(printWriter);
        printWriter.close();
        httpResponse.setContent(channelBuffer);
    }
    ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
    Channel ch = e.getChannel();
    ch.close();
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) PrintWriter(java.io.PrintWriter)

Example 19 with ClosedChannelException

use of java.nio.channels.ClosedChannelException in project hs4j by killme2008.

the class AbstractNioSession method flush0.

protected final void flush0() {
    SelectionKey tmpKey = null;
    Selector writeSelector = null;
    int attempts = 0;
    try {
        while (true) {
            if (writeSelector == null) {
                writeSelector = SelectorFactory.getSelector();
                if (writeSelector == null) {
                    return;
                }
                tmpKey = selectableChannel.register(writeSelector, SelectionKey.OP_WRITE);
            }
            if (writeSelector.select(1000) == 0) {
                attempts++;
                if (attempts > 2) {
                    return;
                }
            } else {
                break;
            }
        }
        onWrite(selectableChannel.keyFor(writeSelector));
    } catch (ClosedChannelException cce) {
        onException(cce);
        log.error("Flush error", cce);
        close();
    } catch (IOException ioe) {
        onException(ioe);
        log.error("Flush error", ioe);
        close();
    } finally {
        if (tmpKey != null) {
            // Cancel the key.
            tmpKey.cancel();
            tmpKey = null;
        }
        if (writeSelector != null) {
            try {
                writeSelector.selectNow();
            } catch (IOException e) {
                log.error("Temp selector selectNow error", e);
            }
            // return selector
            SelectorFactory.returnSelector(writeSelector);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Example 20 with ClosedChannelException

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

the class PhysicalFlushableChannelTest method shouldThrowClosedChannelExceptionWhenChannelUnexpectedlyClosed.

@Test
public void shouldThrowClosedChannelExceptionWhenChannelUnexpectedlyClosed() throws Exception {
    // GIVEN
    final File file = new File(directory.directory(), "file");
    StoreChannel storeChannel = fileSystemRule.get().open(file, "rw");
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
    // just close the underlying channel
    storeChannel.close();
    // WHEN just appending something to the buffer
    channel.put((byte) 0);
    // and wanting to empty that into the channel
    try {
        channel.prepareForFlush();
        fail("Should have thrown exception");
    } catch (ClosedChannelException e) {
    // THEN we should get a ClosedChannelException
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

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