Home / Function/ doWrite() — netty Function Reference

doWrite() — netty Function Reference

Architecture documentation for the doWrite() function in NioSocketChannel.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  bb6558b5_8b8b_7f6e_4aa7_c7dc82285c63["doWrite()"]
  f2eb70be_1f76_3e54_0854_050839fa58d4["NioSocketChannel"]
  bb6558b5_8b8b_7f6e_4aa7_c7dc82285c63 -->|defined in| f2eb70be_1f76_3e54_0854_050839fa58d4
  9d230ec5_bdc2_68d9_2305_f8304569aa0b["getMaxBytesPerGatheringWrite()"]
  bb6558b5_8b8b_7f6e_4aa7_c7dc82285c63 -->|calls| 9d230ec5_bdc2_68d9_2305_f8304569aa0b
  b2b75b55_1e03_c82a_0faf_e7985b8f9efa["adjustMaxBytesPerGatheringWrite()"]
  bb6558b5_8b8b_7f6e_4aa7_c7dc82285c63 -->|calls| b2b75b55_1e03_c82a_0faf_e7985b8f9efa
  style bb6558b5_8b8b_7f6e_4aa7_c7dc82285c63 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java lines 378–439

    @Override
    protected void doWrite(ChannelOutboundBuffer in) throws Exception {
        SocketChannel ch = javaChannel();
        int writeSpinCount = config().getWriteSpinCount();
        do {
            if (in.isEmpty()) {
                // All written so clear OP_WRITE
                clearOpWrite();
                // Directly return here so incompleteWrite(...) is not called.
                return;
            }

            // Ensure the pending writes are made of ByteBufs only.
            int maxBytesPerGatheringWrite = ((NioSocketChannelConfig) config).getMaxBytesPerGatheringWrite();
            ByteBuffer[] nioBuffers = in.nioBuffers(1024, maxBytesPerGatheringWrite);
            int nioBufferCnt = in.nioBufferCount();

            // Always use nioBuffers() to workaround data-corruption.
            // See https://github.com/netty/netty/issues/2761
            switch (nioBufferCnt) {
                case 0:
                    // We have something else beside ByteBuffers to write so fallback to normal writes.
                    writeSpinCount -= doWrite0(in);
                    break;
                case 1: {
                    // Only one ByteBuf so use non-gathering write
                    // Zero length buffers are not added to nioBuffers by ChannelOutboundBuffer, so there is no need
                    // to check if the total size of all the buffers is non-zero.
                    ByteBuffer buffer = nioBuffers[0];
                    int attemptedBytes = buffer.remaining();
                    final int localWrittenBytes = ch.write(buffer);
                    if (localWrittenBytes <= 0) {
                        incompleteWrite(true);
                        return;
                    }
                    adjustMaxBytesPerGatheringWrite(attemptedBytes, localWrittenBytes, maxBytesPerGatheringWrite);
                    in.removeBytes(localWrittenBytes);
                    --writeSpinCount;
                    break;
                }
                default: {
                    // Zero length buffers are not added to nioBuffers by ChannelOutboundBuffer, so there is no need
                    // to check if the total size of all the buffers is non-zero.
                    // We limit the max amount to int above so cast is safe
                    long attemptedBytes = in.nioBufferSize();
                    final long localWrittenBytes = ch.write(nioBuffers, 0, nioBufferCnt);
                    if (localWrittenBytes <= 0) {
                        incompleteWrite(true);
                        return;
                    }
                    // Casting to int is safe because we limit the total amount of data in the nioBuffers to int above.
                    adjustMaxBytesPerGatheringWrite((int) attemptedBytes, (int) localWrittenBytes,
                            maxBytesPerGatheringWrite);
                    in.removeBytes(localWrittenBytes);
                    --writeSpinCount;
                    break;
                }
            }
        } while (writeSpinCount > 0);

        incompleteWrite(writeSpinCount < 0);
    }

Domain

Subdomains

Frequently Asked Questions

What does doWrite() do?
doWrite() is a function in the netty codebase, defined in transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java.
Where is doWrite() defined?
doWrite() is defined in transport/src/main/java/io/netty/channel/socket/nio/NioSocketChannel.java at line 378.
What does doWrite() call?
doWrite() calls 2 function(s): adjustMaxBytesPerGatheringWrite, getMaxBytesPerGatheringWrite.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free