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;
}
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;
}
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();
}
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);
}
}
}
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
}
}
Aggregations