Home / Class/ Writer Class — netty Architecture

Writer Class — netty Architecture

Architecture documentation for the Writer class in BrotliEncoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  64c539b4_9d62_5673_892e_728dc5998630["Writer"]
  7b4f39c5_54d8_a348_18fa_f3dc6dbd5432["BrotliEncoder.java"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|defined in| 7b4f39c5_54d8_a348_18fa_f3dc6dbd5432
  9fe60479_be41_705f_0121_193055192a9d["Writer()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| 9fe60479_be41_705f_0121_193055192a9d
  986db13a_14ff_be0b_4d9e_590235a9bd16["encode()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| 986db13a_14ff_be0b_4d9e_590235a9bd16
  d056c373_d9ff_face_c66b_749abde43b73["allocate()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| d056c373_d9ff_face_c66b_749abde43b73
  fa8504eb_9619_5a41_f2aa_6fadb976fae6["write()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| fa8504eb_9619_5a41_f2aa_6fadb976fae6
  379b62bb_e689_cf53_aef4_ce32ab6cc4da["isOpen()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| 379b62bb_e689_cf53_aef4_ce32ab6cc4da
  d68f3978_6547_b81e_e6ff_20397d066591["close()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| d68f3978_6547_b81e_e6ff_20397d066591
  589580a9_3e45_15df_0d67_4c020f38c2d1["finish()"]
  64c539b4_9d62_5673_892e_728dc5998630 -->|method| 589580a9_3e45_15df_0d67_4c020f38c2d1

Relationship Graph

Source Code

codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliEncoder.java lines 186–278

    private static final class Writer implements WritableByteChannel {

        private ByteBuf writableBuffer;
        private final BrotliEncoderChannel brotliEncoderChannel;
        private final ChannelHandlerContext ctx;
        private boolean isClosed;

        private Writer(Encoder.Parameters parameters, ChannelHandlerContext ctx) throws IOException {
            brotliEncoderChannel = new BrotliEncoderChannel(this, parameters);
            this.ctx = ctx;
        }

        private void encode(ByteBuf msg, boolean preferDirect) throws Exception {
            try {
                allocate(preferDirect);

                // Compress data and flush it into Buffer.
                //
                // As soon as we call flush, Encoder will be triggered to write encoded
                // data into WritableByteChannel.
                //
                // A race condition will not arise because one flush call to encoder will result
                // in only 1 call at `write(ByteBuffer)`.
                ByteBuffer nioBuffer = CompressionUtil.safeReadableNioBuffer(msg);
                int position = nioBuffer.position();
                brotliEncoderChannel.write(nioBuffer);
                msg.skipBytes(nioBuffer.position() - position);
                brotliEncoderChannel.flush();
            } catch (Exception e) {
                ReferenceCountUtil.release(msg);
                throw e;
            }
        }

        private void allocate(boolean preferDirect) {
            if (preferDirect) {
                writableBuffer = ctx.alloc().ioBuffer();
            } else {
                writableBuffer = ctx.alloc().buffer();
            }
        }

        @Override
        public int write(ByteBuffer src) throws IOException {
            if (!isOpen()) {
                throw new ClosedChannelException();
            }

            return writableBuffer.writeBytes(src).readableBytes();
        }

        @Override
        public boolean isOpen() {
            return !isClosed;
        }

        @Override
        public void close() {
            final ChannelPromise promise = ctx.newPromise();

            ctx.executor().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        finish(promise);
                    } catch (IOException ex) {
                        promise.setFailure(new IllegalStateException("Failed to finish encoding", ex));
                    }
                }
            });
        }

        public void finish(final ChannelPromise promise) throws IOException {
            if (!isClosed) {
                // Allocate a buffer and write last pending data.
                allocate(true);

                try {
                    brotliEncoderChannel.close();
                    isClosed = true;
                } catch (Exception ex) {

Frequently Asked Questions

What is the Writer class?
Writer is a class in the netty codebase, defined in codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliEncoder.java.
Where is Writer defined?
Writer is defined in codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliEncoder.java at line 186.

Analyze Your Own Codebase

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

Try Supermodel Free