View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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              // Deference the external references explicitly to tell the VM that
87              // the allocated byte arrays are temporary so that the call stack
88              // can be utilized.
89              // I'm not sure if the modern VMs do this optimization though.
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 }