1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.spdy;
17
18 import static org.jboss.netty.handler.codec.spdy.SpdyCodecUtil.*;
19
20 import java.util.zip.Deflater;
21
22 import org.jboss.netty.buffer.ChannelBuffer;
23
24 class SpdyHeaderBlockZlibCompressor extends SpdyHeaderBlockCompressor {
25
26 private final byte[] out = new byte[8192];
27 private final Deflater compressor;
28
29 public SpdyHeaderBlockZlibCompressor(int version, int compressionLevel) {
30 if (version < SPDY_MIN_VERSION || version > SPDY_MAX_VERSION) {
31 throw new IllegalArgumentException(
32 "unsupported version: " + version);
33 }
34 if (compressionLevel < 0 || compressionLevel > 9) {
35 throw new IllegalArgumentException(
36 "compressionLevel: " + compressionLevel + " (expected: 0-9)");
37 }
38 compressor = new Deflater(compressionLevel);
39 if (version < 3) {
40 compressor.setDictionary(SPDY2_DICT);
41 } else {
42 compressor.setDictionary(SPDY_DICT);
43 }
44 }
45
46 @Override
47 public void setInput(ChannelBuffer decompressed) {
48 byte[] in = new byte[decompressed.readableBytes()];
49 decompressed.readBytes(in);
50 compressor.setInput(in);
51 }
52
53 @Override
54 public void encode(ChannelBuffer compressed) {
55 int numBytes = out.length;
56 while (numBytes == out.length) {
57 numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
58 compressed.writeBytes(out, 0, numBytes);
59 }
60 }
61
62 @Override
63 public void end() {
64 compressor.end();
65 }
66 }