1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.compression;
18
19 import com.aayushatharva.brotli4j.decoder.DecoderJNI;
20 import io.netty.buffer.ByteBuf;
21 import io.netty.channel.ChannelHandlerContext;
22 import io.netty.handler.codec.ByteToMessageDecoder;
23 import io.netty.util.internal.ObjectUtil;
24
25 import java.nio.ByteBuffer;
26 import java.util.List;
27
28
29
30
31
32
33 public final class BrotliDecoder extends ByteToMessageDecoder {
34
35 private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES;
36 private static final int DEFAULT_INPUT_BUFFER_SIZE = 8 * 1024;
37
38
39 static final int DEFAULT_OUTPUT_BUFFER_SIZE = 64 * 1024;
40
41 private enum State {
42 DONE, NEEDS_MORE_INPUT, ERROR
43 }
44
45 static {
46 try {
47 Brotli.ensureAvailability();
48 } catch (Throwable throwable) {
49 throw new ExceptionInInitializerError(throwable);
50 }
51 }
52
53 private final int inputBufferSize;
54 private final int outputBufferSize;
55 private final int maxForwardBytes;
56 private DecoderJNI.Wrapper decoder;
57 private boolean destroyed;
58 private boolean needsRead;
59 private boolean pulledSinceLastPush;
60 private ByteBuf accumBuffer;
61
62
63
64
65 public BrotliDecoder() {
66 this(DEFAULT_INPUT_BUFFER_SIZE);
67 }
68
69
70
71
72
73 public BrotliDecoder(int inputBufferSize) {
74 this(inputBufferSize == 0 ? DEFAULT_INPUT_BUFFER_SIZE : inputBufferSize, 0, DEFAULT_MAX_FORWARD_BYTES);
75 }
76
77
78
79
80
81
82
83 public BrotliDecoder(int inputBufferSize, int outputBufferSize) {
84 this(ObjectUtil.checkPositive(inputBufferSize, "inputBufferSize"),
85 ObjectUtil.checkPositive(outputBufferSize, "outputBufferSize"), DEFAULT_MAX_FORWARD_BYTES);
86 }
87
88
89
90 BrotliDecoder(int inputBufferSize, int outputBufferSize, int defaultMaxForwardBytes) {
91 this.inputBufferSize = ObjectUtil.checkPositive(inputBufferSize, "inputBufferSize");
92 this.maxForwardBytes = outputBufferSize > 0 ? outputBufferSize : defaultMaxForwardBytes;
93 this.outputBufferSize = maxForwardBytes > 0 ? maxForwardBytes : DEFAULT_OUTPUT_BUFFER_SIZE;
94 }
95
96
97
98
99
100
101
102
103
104
105 public static BrotliDecoder newDecoderWithMaxAllocation(int maxAllocation) {
106 ObjectUtil.checkPositiveOrZero(maxAllocation, "maxAllocation");
107 return maxAllocation > 0 ?
108 new BrotliDecoder(DEFAULT_INPUT_BUFFER_SIZE, maxAllocation) :
109 new BrotliDecoder();
110 }
111
112 private void forwardOutput(ChannelHandlerContext ctx) {
113 ByteBuffer nativeBuffer = decoder.pull(outputBufferSize);
114 pulledSinceLastPush = true;
115
116 int remaining = nativeBuffer.remaining();
117 if (accumBuffer == null) {
118 accumBuffer = ctx.alloc().buffer(remaining);
119 }
120 accumBuffer.writeBytes(nativeBuffer);
121 needsRead = false;
122 if (accumBuffer.isReadable() && accumBuffer.readableBytes() >= maxForwardBytes) {
123 ctx.fireChannelRead(accumBuffer);
124 accumBuffer = null;
125 }
126 }
127
128 private void flushAccumBuffer(ChannelHandlerContext ctx) {
129 if (accumBuffer != null && accumBuffer.isReadable()) {
130 ctx.fireChannelRead(accumBuffer);
131 } else if (accumBuffer != null) {
132 accumBuffer.release();
133 }
134 accumBuffer = null;
135 }
136
137 private State decompress(ChannelHandlerContext ctx, ByteBuf input) {
138 for (;;) {
139 switch (decoder.getStatus()) {
140 case DONE:
141 return State.DONE;
142
143 case OK:
144 push(0);
145 break;
146
147 case NEEDS_MORE_INPUT:
148 while (decoder.hasOutput()) {
149 forwardOutput(ctx);
150 }
151
152 if (!input.isReadable()) {
153 if (pulledSinceLastPush) {
154
155
156
157 push(0);
158 break;
159 }
160 return State.NEEDS_MORE_INPUT;
161 }
162
163 ByteBuffer decoderInputBuffer = decoder.getInputBuffer();
164 decoderInputBuffer.clear();
165 int readBytes = readBytes(input, decoderInputBuffer);
166 push(readBytes);
167 break;
168
169 case NEEDS_MORE_OUTPUT:
170 forwardOutput(ctx);
171 break;
172
173 default:
174 return State.ERROR;
175 }
176 }
177 }
178
179 private void push(int length) {
180 pulledSinceLastPush = false;
181 decoder.push(length);
182 }
183
184 private static int readBytes(ByteBuf in, ByteBuffer dest) {
185 int limit = Math.min(in.readableBytes(), dest.remaining());
186 ByteBuffer slice = dest.slice();
187 slice.limit(limit);
188 in.readBytes(slice);
189 dest.position(dest.position() + limit);
190 return limit;
191 }
192
193 @Override
194 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
195 decoder = new DecoderJNI.Wrapper(inputBufferSize);
196 }
197
198 @Override
199 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
200 needsRead = true;
201 if (destroyed) {
202
203 in.skipBytes(in.readableBytes());
204 return;
205 }
206
207 if (!in.isReadable()) {
208 return;
209 }
210
211 try {
212 State state = decompress(ctx, in);
213 if (state == State.DONE) {
214 destroy();
215 } else if (state == State.ERROR) {
216 throw new DecompressionException("Brotli stream corrupted");
217 }
218 } catch (Exception e) {
219 destroy();
220 throw e;
221 } finally {
222 flushAccumBuffer(ctx);
223 }
224 }
225
226 private void destroy() {
227 if (!destroyed) {
228 destroyed = true;
229 decoder.destroy();
230 }
231 }
232
233 @Override
234 protected void handlerRemoved0(ChannelHandlerContext ctx) throws Exception {
235 try {
236 destroy();
237 } finally {
238 super.handlerRemoved0(ctx);
239 }
240 }
241
242 @Override
243 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
244 try {
245 destroy();
246 } finally {
247 super.channelInactive(ctx);
248 }
249 }
250
251 @Override
252 public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
253
254 discardSomeReadBytes();
255
256 if (needsRead && !ctx.channel().config().isAutoRead()) {
257 ctx.read();
258 }
259 ctx.fireChannelReadComplete();
260 }
261 }