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 org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBuffers;
20  import org.jboss.netty.handler.codec.compression.CompressionException;
21  import org.jboss.netty.util.internal.jzlib.JZlib;
22  import org.jboss.netty.util.internal.jzlib.ZStream;
23  
24  import static org.jboss.netty.handler.codec.spdy.SpdyCodecUtil.*;
25  
26  class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder {
27  
28      private final ZStream z = new ZStream();
29  
30      private boolean finished;
31  
32      SpdyHeaderBlockJZlibEncoder(
33              SpdyVersion spdyVersion, int compressionLevel, int windowBits, int memLevel) {
34          super(spdyVersion);
35          if (compressionLevel < 0 || compressionLevel > 9) {
36              throw new IllegalArgumentException(
37                      "compressionLevel: " + compressionLevel + " (expected: 0-9)");
38          }
39          if (windowBits < 9 || windowBits > 15) {
40              throw new IllegalArgumentException(
41                      "windowBits: " + windowBits + " (expected: 9-15)");
42          }
43          if (memLevel < 1 || memLevel > 9) {
44              throw new IllegalArgumentException(
45                      "memLevel: " + memLevel + " (expected: 1-9)");
46          }
47  
48          int resultCode = z.deflateInit(
49                  compressionLevel, windowBits, memLevel, JZlib.W_ZLIB);
50          if (resultCode != JZlib.Z_OK) {
51              throw new CompressionException(
52                      "failed to initialize an SPDY header block deflater: " + resultCode);
53          } else {
54              resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length);
55              if (resultCode != JZlib.Z_OK) {
56                  throw new CompressionException(
57                          "failed to set the SPDY dictionary: " + resultCode);
58              }
59          }
60      }
61  
62      private void setInput(ChannelBuffer decompressed) {
63          byte[] in = new byte[decompressed.readableBytes()];
64          decompressed.readBytes(in);
65          z.next_in = in;
66          z.next_in_index = 0;
67          z.avail_in = in.length;
68      }
69  
70      private void encode(ChannelBuffer compressed) {
71          try {
72              byte[] out = new byte[(int) Math.ceil(z.next_in.length * 1.001) + 12];
73              z.next_out = out;
74              z.next_out_index = 0;
75              z.avail_out = out.length;
76  
77              int resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
78              if (resultCode != JZlib.Z_OK) {
79                  throw new CompressionException("compression failure: " + resultCode);
80              }
81  
82              if (z.next_out_index != 0) {
83                  compressed.writeBytes(out, 0, z.next_out_index);
84              }
85          } finally {
86              
87              
88              
89              
90              z.next_in = null;
91              z.next_out = null;
92          }
93      }
94  
95      @Override
96      public synchronized ChannelBuffer encode(SpdyHeadersFrame frame) throws Exception {
97          if (frame == null) {
98              throw new IllegalArgumentException("frame");
99          }
100 
101         if (finished) {
102             return ChannelBuffers.EMPTY_BUFFER;
103         }
104 
105         ChannelBuffer decompressed = super.encode(frame);
106         if (decompressed.readableBytes() == 0) {
107             return ChannelBuffers.EMPTY_BUFFER;
108         }
109 
110         ChannelBuffer compressed = ChannelBuffers.dynamicBuffer();
111         setInput(decompressed);
112         encode(compressed);
113         return compressed;
114     }
115 
116     @Override
117     public synchronized void end() {
118         if (finished) {
119             return;
120         }
121         finished = true;
122         z.deflateEnd();
123         z.next_in = null;
124         z.next_out = null;
125     }
126 }