NioByteUnsafe Class — netty Architecture
Architecture documentation for the NioByteUnsafe class in AbstractNioByteChannel.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD f52609a8_b5d5_f9b7_0a87_06421659d477["NioByteUnsafe"] b651d516_44e3_d960_a63c_4be7f06c25ba["AbstractNioByteChannel.java"] f52609a8_b5d5_f9b7_0a87_06421659d477 -->|defined in| b651d516_44e3_d960_a63c_4be7f06c25ba 216aa50a_4b2e_d203_0a18_d38f61e1dd7e["closeOnRead()"] f52609a8_b5d5_f9b7_0a87_06421659d477 -->|method| 216aa50a_4b2e_d203_0a18_d38f61e1dd7e a63e69b1_c5a7_0e05_79bc_163803b55680["handleReadException()"] f52609a8_b5d5_f9b7_0a87_06421659d477 -->|method| a63e69b1_c5a7_0e05_79bc_163803b55680 eafbe12c_ddc4_fdfc_7207_cbb526f9b297["read()"] f52609a8_b5d5_f9b7_0a87_06421659d477 -->|method| eafbe12c_ddc4_fdfc_7207_cbb526f9b297
Relationship Graph
Source Code
transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java lines 100–200
protected class NioByteUnsafe extends AbstractNioUnsafe {
private void closeOnRead(ChannelPipeline pipeline) {
if (!isInputShutdown0()) {
if (isAllowHalfClosure(config())) {
shutdownInput();
pipeline.fireUserEventTriggered(ChannelInputShutdownEvent.INSTANCE);
} else {
close(voidPromise());
}
} else if (!inputClosedSeenErrorOnRead) {
inputClosedSeenErrorOnRead = true;
pipeline.fireUserEventTriggered(ChannelInputShutdownReadComplete.INSTANCE);
}
}
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close,
RecvByteBufAllocator.Handle allocHandle) {
if (byteBuf != null) {
if (byteBuf.isReadable()) {
readPending = false;
try {
pipeline.fireChannelRead(byteBuf);
} catch (Exception e) {
cause.addSuppressed(e);
}
} else {
byteBuf.release();
}
}
allocHandle.readComplete();
pipeline.fireChannelReadComplete();
pipeline.fireExceptionCaught(cause);
// If oom will close the read event, release connection.
// See https://github.com/netty/netty/issues/10434
if (close ||
cause instanceof OutOfMemoryError ||
cause instanceof LeakPresenceDetector.AllocationProhibitedException ||
cause instanceof IOException) {
closeOnRead(pipeline);
}
}
@Override
public final void read() {
final ChannelConfig config = config();
if (shouldBreakReadReady(config)) {
clearReadPending();
return;
}
final ChannelPipeline pipeline = pipeline();
final ByteBufAllocator allocator = config.getAllocator();
final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
allocHandle.reset(config);
ByteBuf byteBuf = null;
boolean close = false;
try {
do {
byteBuf = allocHandle.allocate(allocator);
allocHandle.lastBytesRead(doReadBytes(byteBuf));
if (allocHandle.lastBytesRead() <= 0) {
// nothing was read. release the buffer.
byteBuf.release();
byteBuf = null;
close = allocHandle.lastBytesRead() < 0;
if (close) {
// There is nothing left to read as we received an EOF.
readPending = false;
}
break;
}
allocHandle.incMessagesRead(1);
readPending = false;
pipeline.fireChannelRead(byteBuf);
byteBuf = null;
} while (allocHandle.continueReading());
allocHandle.readComplete();
Source
Frequently Asked Questions
What is the NioByteUnsafe class?
NioByteUnsafe is a class in the netty codebase, defined in transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java.
Where is NioByteUnsafe defined?
NioByteUnsafe is defined in transport/src/main/java/io/netty/channel/nio/AbstractNioByteChannel.java at line 100.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free