File: CompressDeflater6.java

package info (click to toggle)
tomcat8 8.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 33,280 kB
  • sloc: java: 303,023; xml: 46,103; jsp: 3,182; sh: 1,358; perl: 269; makefile: 114
file content (178 lines) | stat: -rw-r--r-- 6,357 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tomcat.spdy;

import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.apache.tomcat.spdy.SpdyConnection.CompressSupport;

/**
 * Java6 Deflater with the workaround from tomcat http filters.
 */
class CompressDeflater6 implements CompressSupport {
    public static final long DICT_ID = 3751956914L;

    // Make sure to use the latest from net/spdy/spdy_framer.cc, not from spec
    private static final String SPDY_DICT_S =
              "optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-"
            + "languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi"
            + "f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser"
            + "-agent10010120020120220320420520630030130230330430530630740040140240340440"
            + "5406407408409410411412413414415416417500501502503504505accept-rangesageeta"
            + "glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic"
            + "ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran"
            + "sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati"
            + "oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo"
            + "ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe"
            + "pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic"
            + "ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1"
            + ".1statusversionurl ";

    private static final byte[] SPDY_DICT = SPDY_DICT_S.getBytes();
    // C code uses this - not in the spec
    static {
        SPDY_DICT[SPDY_DICT.length - 1] = (byte) 0;
    }

    private Deflater zipOut;
    private Inflater zipIn;

    private byte[] decompressBuffer;
    private int decMax;

    private byte[] compressBuffer;

    public CompressDeflater6() {
    }

    public static CompressDeflater6 get() {
        // TODO: code to plug in v7-specific. It is marginally faster.
        return new CompressDeflater6();
    }

    public void recycle() {
        // TODO
    }

    public void init() {
        if (zipOut != null) {
            return;
        }
        try {
            // false is important - otherwise 'bad method'
            zipOut = new Deflater(Deflater.DEFAULT_COMPRESSION, false);
            zipOut.setDictionary(SPDY_DICT);
            zipIn = new Inflater();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void compress(SpdyFrame frame, int start)
            throws IOException {
        init();

        if (compressBuffer == null) {
            compressBuffer = new byte[frame.data.length];
        }

        // last byte for flush ?
        zipOut.setInput(frame.data, start, frame.endData - start - 1);
        int coff = start;
        zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
        while (true) {
            int rd = zipOut.deflate(compressBuffer, coff, compressBuffer.length - coff);
            if (rd == 0) {
                // needsInput needs to be called - we're done with this frame ?
                zipOut.setInput(frame.data, frame.endData - 1, 1);
                zipOut.setLevel(Deflater.BEST_SPEED);
                while (true) {
                    rd = zipOut.deflate(compressBuffer, coff, compressBuffer.length - coff);
                    coff += rd;
                    if (rd == 0) {
                        break;
                    }
                    byte[] b = new byte[compressBuffer.length * 2];
                    System.arraycopy(compressBuffer, 0, b, 0, coff);
                    compressBuffer = b;
                }
                zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
                break;
            }
            coff += rd;
        }

        byte[] tmp = frame.data;
        frame.data = compressBuffer;
        compressBuffer = tmp;
        frame.endData = coff;
    }

    @Override
    public synchronized void decompress(SpdyFrame frame, int start)
            throws IOException {
        // stream id ( 4 ) + unused ( 2 )
        // nvCount is compressed in impl - spec is different
        init();


        if (decompressBuffer == null) {
            decompressBuffer = new byte[frame.data.length];
        }

        // will read from dec buffer to frame.data
        decMax = frame.endData;

        int off = start;

        zipIn.setInput(frame.data, start, decMax - start);

        while (true) {
            int rd;
            try {
                rd = zipIn.inflate(decompressBuffer, off, decompressBuffer.length - off);
                if (rd == 0 && zipIn.needsDictionary()) {
                    zipIn.setDictionary(SPDY_DICT);
                    continue;
                }
            } catch (DataFormatException e) {
                throw new IOException(e);
            }
            if (rd == 0) {
                break;
            }
            if (rd == -1) {
                break;
            }
            off += rd;
            byte[] b = new byte[decompressBuffer.length * 2];
            System.arraycopy(decompressBuffer, 0, b, 0, off);
            decompressBuffer = b;

        }
        byte[] tmpBuf = decompressBuffer;
        decompressBuffer = frame.data;
        frame.data = tmpBuf;

        frame.off = start;
        frame.endData = off;
    }
}