Home / Function/ doWrite() — netty Function Reference

doWrite() — netty Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  c38db99c_4164_2eab_a0bf_82641dc78f7e["doWrite()"]
  cfd0ddf4_54f6_2e2b_6078_ee84ec74aadf["NioDomainSocketChannel"]
  c38db99c_4164_2eab_a0bf_82641dc78f7e -->|defined in| cfd0ddf4_54f6_2e2b_6078_ee84ec74aadf
  c0e509f0_5dcb_c849_76fa_6f34c05fdf4e["getMaxBytesPerGatheringWrite()"]
  c38db99c_4164_2eab_a0bf_82641dc78f7e -->|calls| c0e509f0_5dcb_c849_76fa_6f34c05fdf4e
  f88024aa_f5aa_7700_4bbd_9abbdaad622c["adjustMaxBytesPerGatheringWrite()"]
  c38db99c_4164_2eab_a0bf_82641dc78f7e -->|calls| f88024aa_f5aa_7700_4bbd_9abbdaad622c
  style c38db99c_4164_2eab_a0bf_82641dc78f7e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/socket/nio/NioDomainSocketChannel.java lines 380–441

    @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 = ((NioDomainSocketChannelConfig) 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/NioDomainSocketChannel.java.
Where is doWrite() defined?
doWrite() is defined in transport/src/main/java/io/netty/channel/socket/nio/NioDomainSocketChannel.java at line 380.
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