Make #211 work with Java 5
* Use java.util.zip.Deflater only when running under Java 7
This commit is contained in:
parent
5aae8279b9
commit
b22ebbe430
@ -34,7 +34,8 @@ public class SpdyFrameDecoder extends FrameDecoder {
|
|||||||
private final int maxFrameSize;
|
private final int maxFrameSize;
|
||||||
private final int maxHeaderSize;
|
private final int maxHeaderSize;
|
||||||
|
|
||||||
private final SpdyZlibDecoder headerBlockDecompressor = new SpdyZlibDecoder();
|
private final SpdyHeaderBlockDecompressor headerBlockDecompressor =
|
||||||
|
SpdyHeaderBlockDecompressor.newInstance();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance with the default {@code maxChunkSize (8192)},
|
* Creates a new instance with the default {@code maxChunkSize (8192)},
|
||||||
|
@ -34,7 +34,7 @@ import io.netty.handler.codec.oneone.OneToOneEncoder;
|
|||||||
public class SpdyFrameEncoder extends OneToOneEncoder {
|
public class SpdyFrameEncoder extends OneToOneEncoder {
|
||||||
|
|
||||||
private volatile boolean finished;
|
private volatile boolean finished;
|
||||||
private final SpdyZlibEncoder headerBlockCompressor;
|
private final SpdyHeaderBlockCompressor headerBlockCompressor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance with the default {@code compressionLevel (6)},
|
* Creates a new instance with the default {@code compressionLevel (6)},
|
||||||
@ -49,7 +49,7 @@ public class SpdyFrameEncoder extends OneToOneEncoder {
|
|||||||
*/
|
*/
|
||||||
public SpdyFrameEncoder(int compressionLevel, int windowBits, int memLevel) {
|
public SpdyFrameEncoder(int compressionLevel, int windowBits, int memLevel) {
|
||||||
super();
|
super();
|
||||||
headerBlockCompressor = new SpdyZlibEncoder(compressionLevel);
|
headerBlockCompressor = SpdyHeaderBlockCompressor.newInstance(compressionLevel, windowBits, memLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project 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 io.netty.handler.codec.spdy;
|
||||||
|
|
||||||
|
import java.util.zip.Deflater;
|
||||||
|
|
||||||
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
|
||||||
|
abstract class SpdyHeaderBlockCompressor {
|
||||||
|
|
||||||
|
private static final boolean USE_ZLIB;
|
||||||
|
|
||||||
|
static {
|
||||||
|
boolean java7 = false;
|
||||||
|
try {
|
||||||
|
Deflater.class.getDeclaredMethod(
|
||||||
|
"deflate",
|
||||||
|
new Class<?>[] { byte[].class, int.class, int.class, int.class });
|
||||||
|
java7 = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
USE_ZLIB = java7;
|
||||||
|
}
|
||||||
|
|
||||||
|
static SpdyHeaderBlockCompressor newInstance(int compressionLevel, int windowBits, int memLevel) {
|
||||||
|
if (USE_ZLIB) {
|
||||||
|
return new SpdyZlibEncoder(compressionLevel);
|
||||||
|
} else {
|
||||||
|
return new SpdyJZlibEncoder(compressionLevel, windowBits, memLevel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract void setInput(ChannelBuffer decompressed);
|
||||||
|
abstract void encode(ChannelBuffer compressed);
|
||||||
|
abstract void end();
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project 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 io.netty.handler.codec.spdy;
|
||||||
|
|
||||||
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
|
||||||
|
abstract class SpdyHeaderBlockDecompressor {
|
||||||
|
|
||||||
|
static SpdyHeaderBlockDecompressor newInstance() {
|
||||||
|
return new SpdyJZlibDecoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract void setInput(ChannelBuffer compressed);
|
||||||
|
abstract void decode(ChannelBuffer decompressed);
|
||||||
|
}
|
@ -28,21 +28,21 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.jboss.netty.handler.codec.spdy;
|
package io.netty.handler.codec.spdy;
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
import io.netty.buffer.ChannelBuffer;
|
||||||
import org.jboss.netty.handler.codec.compression.CompressionException;
|
import io.netty.handler.codec.compression.CompressionException;
|
||||||
import org.jboss.netty.util.internal.jzlib.JZlib;
|
import io.netty.util.internal.jzlib.JZlib;
|
||||||
import org.jboss.netty.util.internal.jzlib.ZStream;
|
import io.netty.util.internal.jzlib.ZStream;
|
||||||
|
|
||||||
import static org.jboss.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
||||||
|
|
||||||
class SpdyZlibDecoder {
|
class SpdyJZlibDecoder extends SpdyHeaderBlockDecompressor {
|
||||||
|
|
||||||
private final byte[] out = new byte[8192];
|
private final byte[] out = new byte[8192];
|
||||||
private final ZStream z = new ZStream();
|
private final ZStream z = new ZStream();
|
||||||
|
|
||||||
public SpdyZlibDecoder() {
|
public SpdyJZlibDecoder() {
|
||||||
int resultCode;
|
int resultCode;
|
||||||
resultCode = z.inflateInit(JZlib.W_ZLIB);
|
resultCode = z.inflateInit(JZlib.W_ZLIB);
|
||||||
if (resultCode != JZlib.Z_OK) {
|
if (resultCode != JZlib.Z_OK) {
|
||||||
@ -51,6 +51,7 @@ class SpdyZlibDecoder {
|
|||||||
z.next_out = out;
|
z.next_out = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setInput(ChannelBuffer compressed) {
|
public void setInput(ChannelBuffer compressed) {
|
||||||
byte[] in = new byte[compressed.readableBytes()];
|
byte[] in = new byte[compressed.readableBytes()];
|
||||||
compressed.readBytes(in);
|
compressed.readBytes(in);
|
||||||
@ -59,6 +60,7 @@ class SpdyZlibDecoder {
|
|||||||
z.avail_in = in.length;
|
z.avail_in = in.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void decode(ChannelBuffer decompressed) {
|
public void decode(ChannelBuffer decompressed) {
|
||||||
z.next_out_index = 0;
|
z.next_out_index = 0;
|
||||||
z.avail_out = out.length;
|
z.avail_out = out.length;
|
@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 The Netty Project
|
||||||
|
*
|
||||||
|
* The Netty Project 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright 2012 Twitter, Inc.
|
||||||
|
*
|
||||||
|
* Licensed 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 io.netty.handler.codec.spdy;
|
||||||
|
|
||||||
|
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
||||||
|
|
||||||
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
import io.netty.handler.codec.compression.CompressionException;
|
||||||
|
import io.netty.util.internal.jzlib.JZlib;
|
||||||
|
import io.netty.util.internal.jzlib.ZStream;
|
||||||
|
|
||||||
|
class SpdyJZlibEncoder extends SpdyHeaderBlockCompressor {
|
||||||
|
|
||||||
|
private final ZStream z = new ZStream();
|
||||||
|
|
||||||
|
public SpdyJZlibEncoder(int compressionLevel, int windowBits, int memLevel) {
|
||||||
|
if (compressionLevel < 0 || compressionLevel > 9) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"compressionLevel: " + compressionLevel + " (expected: 0-9)");
|
||||||
|
}
|
||||||
|
if (windowBits < 9 || windowBits > 15) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"windowBits: " + windowBits + " (expected: 9-15)");
|
||||||
|
}
|
||||||
|
if (memLevel < 1 || memLevel > 9) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"memLevel: " + memLevel + " (expected: 1-9)");
|
||||||
|
}
|
||||||
|
|
||||||
|
int resultCode = z.deflateInit(
|
||||||
|
compressionLevel, windowBits, memLevel, JZlib.W_ZLIB);
|
||||||
|
if (resultCode != JZlib.Z_OK) {
|
||||||
|
throw new CompressionException(
|
||||||
|
"failed to initialize an SPDY header block deflater: " + resultCode);
|
||||||
|
} else {
|
||||||
|
resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length);
|
||||||
|
if (resultCode != JZlib.Z_OK) {
|
||||||
|
throw new CompressionException(
|
||||||
|
"failed to set the SPDY dictionary: " + resultCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setInput(ChannelBuffer decompressed) {
|
||||||
|
byte[] in = new byte[decompressed.readableBytes()];
|
||||||
|
decompressed.readBytes(in);
|
||||||
|
z.next_in = in;
|
||||||
|
z.next_in_index = 0;
|
||||||
|
z.avail_in = in.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void encode(ChannelBuffer compressed) {
|
||||||
|
try {
|
||||||
|
byte[] out = new byte[(int) Math.ceil(z.next_in.length * 1.001) + 12];
|
||||||
|
z.next_out = out;
|
||||||
|
z.next_out_index = 0;
|
||||||
|
z.avail_out = out.length;
|
||||||
|
|
||||||
|
int resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
|
||||||
|
if (resultCode != JZlib.Z_OK) {
|
||||||
|
throw new CompressionException("compression failure: " + resultCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (z.next_out_index != 0) {
|
||||||
|
compressed.writeBytes(out, 0, z.next_out_index);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Deference the external references explicitly to tell the VM that
|
||||||
|
// the allocated byte arrays are temporary so that the call stack
|
||||||
|
// can be utilized.
|
||||||
|
// I'm not sure if the modern VMs do this optimization though.
|
||||||
|
z.next_in = null;
|
||||||
|
z.next_out = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void end() {
|
||||||
|
z.deflateEnd();
|
||||||
|
z.next_in = null;
|
||||||
|
z.next_out = null;
|
||||||
|
}
|
||||||
|
}
|
@ -30,13 +30,13 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.handler.codec.spdy;
|
package io.netty.handler.codec.spdy;
|
||||||
|
|
||||||
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
|
||||||
|
|
||||||
import java.util.zip.Deflater;
|
import java.util.zip.Deflater;
|
||||||
|
|
||||||
import io.netty.buffer.ChannelBuffer;
|
import io.netty.buffer.ChannelBuffer;
|
||||||
|
|
||||||
class SpdyZlibEncoder {
|
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
||||||
|
|
||||||
|
class SpdyZlibEncoder extends SpdyHeaderBlockCompressor {
|
||||||
|
|
||||||
private final byte[] out = new byte[8192];
|
private final byte[] out = new byte[8192];
|
||||||
private final Deflater compressor;
|
private final Deflater compressor;
|
||||||
@ -50,12 +50,14 @@ class SpdyZlibEncoder {
|
|||||||
compressor.setDictionary(SPDY_DICT);
|
compressor.setDictionary(SPDY_DICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setInput(ChannelBuffer decompressed) {
|
public void setInput(ChannelBuffer decompressed) {
|
||||||
byte[] in = new byte[decompressed.readableBytes()];
|
byte[] in = new byte[decompressed.readableBytes()];
|
||||||
decompressed.readBytes(in);
|
decompressed.readBytes(in);
|
||||||
compressor.setInput(in);
|
compressor.setInput(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void encode(ChannelBuffer compressed) {
|
public void encode(ChannelBuffer compressed) {
|
||||||
while (!compressor.needsInput()) {
|
while (!compressor.needsInput()) {
|
||||||
int numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
|
int numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
|
||||||
@ -63,6 +65,7 @@ class SpdyZlibEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void end() {
|
public void end() {
|
||||||
compressor.end();
|
compressor.end();
|
||||||
}
|
}
|
||||||
|
32
pom.xml
32
pom.xml
@ -212,9 +212,9 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<rules>
|
<rules>
|
||||||
<requireJavaVersion>
|
<requireJavaVersion>
|
||||||
<!-- Enforce java 1.6 as minimum for compiling -->
|
<!-- Enforce java 1.7 as minimum for compiling -->
|
||||||
<!-- This is needed because of the Unsafe detection code -->
|
<!-- This is needed because of java.util.zip.Deflater -->
|
||||||
<version>[1.6.0,)</version>
|
<version>[1.7.0,)</version>
|
||||||
</requireJavaVersion>
|
</requireJavaVersion>
|
||||||
<requireMavenVersion>
|
<requireMavenVersion>
|
||||||
<version>[3.0.2,)</version>
|
<version>[3.0.2,)</version>
|
||||||
@ -237,6 +237,32 @@
|
|||||||
<showWarnings>true</showWarnings>
|
<showWarnings>true</showWarnings>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<!-- ensure that only methods available in java 1.6 can
|
||||||
|
be used even when compiling with java 1.7+ -->
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||||
|
<version>1.7</version>
|
||||||
|
<configuration>
|
||||||
|
<signature>
|
||||||
|
<groupId>org.codehaus.mojo.signature</groupId>
|
||||||
|
<artifactId>java16</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</signature>
|
||||||
|
<ignores>
|
||||||
|
<ignore>sun.misc.Unsafe</ignore>
|
||||||
|
<ignore>java.util.zip.Deflater</ignore>
|
||||||
|
</ignores>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>check</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||||
<version>2.8</version>
|
<version>2.8</version>
|
||||||
|
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012 The Netty Project
|
|
||||||
*
|
|
||||||
* The Netty Project 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.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Copyright 2012 Twitter, Inc.
|
|
||||||
*
|
|
||||||
* Licensed 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.jboss.netty.handler.codec.spdy;
|
|
||||||
|
|
||||||
import java.util.zip.Deflater;
|
|
||||||
|
|
||||||
import org.jboss.netty.buffer.ChannelBuffer;
|
|
||||||
|
|
||||||
import static org.jboss.netty.handler.codec.spdy.SpdyCodecUtil.*;
|
|
||||||
|
|
||||||
class SpdyZlibEncoder {
|
|
||||||
|
|
||||||
private final byte[] out = new byte[8192];
|
|
||||||
private final Deflater compressor;
|
|
||||||
|
|
||||||
public SpdyZlibEncoder(int compressionLevel) {
|
|
||||||
if (compressionLevel < 0 || compressionLevel > 9) {
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"compressionLevel: " + compressionLevel + " (expected: 0-9)");
|
|
||||||
}
|
|
||||||
compressor = new Deflater(compressionLevel);
|
|
||||||
compressor.setDictionary(SPDY_DICT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setInput(ChannelBuffer decompressed) {
|
|
||||||
byte[] in = new byte[decompressed.readableBytes()];
|
|
||||||
decompressed.readBytes(in);
|
|
||||||
compressor.setInput(in);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void encode(ChannelBuffer compressed) {
|
|
||||||
while (!compressor.needsInput()) {
|
|
||||||
int numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
|
|
||||||
compressed.writeBytes(out, 0, numBytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void end() {
|
|
||||||
compressor.end();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user