Skip to content

Commit

Permalink
[netty#1012] Replace forked jzlib with official jzlib and add a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ymnk authored and Norman Maurer committed Feb 20, 2013
1 parent 7f780f4 commit 8fdf788
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 5,281 deletions.
5 changes: 5 additions & 0 deletions codec-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>netty-handler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<optional>true</optional>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
package io.netty.handler.codec.spdy;

import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;

import com.jcraft.jzlib.Deflater;
import com.jcraft.jzlib.JZlib;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.compression.CompressionException;
import io.netty.util.internal.jzlib.JZlib;
import io.netty.util.internal.jzlib.ZStream;

class SpdyHeaderBlockJZlibCompressor extends SpdyHeaderBlockCompressor {

private final ZStream z = new ZStream();
private final Deflater z = new Deflater();

public SpdyHeaderBlockJZlibCompressor(
int version, int compressionLevel, int windowBits, int memLevel) {
Expand Down
5 changes: 5 additions & 0 deletions codec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>jboss-marshalling</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies for jboss marshalling encoder/decoder -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.internal.jzlib.JZlib;
import io.netty.util.internal.jzlib.ZStream;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.Inflater;

public class JZlibDecoder extends ZlibDecoder {

private final ZStream z = new ZStream();
private final Inflater z = new Inflater();
private byte[] dictionary;
private volatile boolean finished;

Expand All @@ -45,7 +45,7 @@ public JZlibDecoder(ZlibWrapper wrapper) {
throw new NullPointerException("wrapper");
}

int resultCode = z.inflateInit(ZlibUtil.convertWrapperType(wrapper));
int resultCode = z.init(ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.internal.jzlib.JZlib;
import io.netty.util.internal.jzlib.ZStream;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.Deflater;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -35,7 +35,7 @@ public class JZlibEncoder extends ZlibEncoder {

private static final byte[] EMPTY_ARRAY = new byte[0];

private final ZStream z = new ZStream();
private final Deflater z = new Deflater();
private final AtomicBoolean finished = new AtomicBoolean();
private volatile ChannelHandlerContext ctx;

Expand Down Expand Up @@ -140,7 +140,7 @@ public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, i
}

synchronized (z) {
int resultCode = z.deflateInit(
int resultCode = z.init(
compressionLevel, windowBits, memLevel,
ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.netty.handler.codec.compression;

import io.netty.util.internal.jzlib.JZlib;
import io.netty.util.internal.jzlib.ZStream;
import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZStream;

/**
* Utility methods used by {@link JZlibEncoder} and {@link JZlibDecoder}.
Expand All @@ -32,8 +32,8 @@ static CompressionException exception(ZStream z, String message, int resultCode)
(z.msg != null? ": " + z.msg : ""));
}

static Enum<?> convertWrapperType(ZlibWrapper wrapper) {
Enum<?> convertedWrapperType;
static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
JZlib.WrapperType convertedWrapperType;
switch (wrapper) {
case NONE:
convertedWrapperType = JZlib.W_NONE;
Expand All @@ -45,7 +45,7 @@ static Enum<?> convertWrapperType(ZlibWrapper wrapper) {
convertedWrapperType = JZlib.W_GZIP;
break;
case ZLIB_OR_NONE:
convertedWrapperType = JZlib.W_ZLIB_OR_NONE;
convertedWrapperType = JZlib.W_ANY;
break;
default:
throw new Error();
Expand Down
133 changes: 133 additions & 0 deletions codec/src/test/java/io/netty/handler/codec/compression/JZlibTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2013 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.compression;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.After;
import org.junit.Test;
import io.netty.channel.embedded.EmbeddedByteChannel;

import static org.junit.Assert.*;

public class JZlibTest {

@After
public void resetSnappy() {
}

@Test
public void testZLIB() throws Exception {
ByteBuf data = Unpooled.wrappedBuffer("test".getBytes());

EmbeddedByteChannel chEncoder =
new EmbeddedByteChannel(new JZlibEncoder(ZlibWrapper.ZLIB));

chEncoder.writeOutbound(data);
assertTrue(chEncoder.finish());

byte[] deflatedData = chEncoder.readOutbound().array();

{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.ZLIB));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}

{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}
}

@Test
public void testNONE() throws Exception {
ByteBuf data = Unpooled.wrappedBuffer("test".getBytes());

EmbeddedByteChannel chEncoder =
new EmbeddedByteChannel(new JZlibEncoder(ZlibWrapper.NONE));

chEncoder.writeOutbound(data);
assertTrue(chEncoder.finish());

byte[] deflatedData = chEncoder.readOutbound().array();

{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.NONE));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}

{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}
}


@Test
public void testGZIP() throws Exception {
ByteBuf data = Unpooled.wrappedBuffer("test".getBytes());

EmbeddedByteChannel chEncoder =
new EmbeddedByteChannel(new JZlibEncoder(ZlibWrapper.GZIP));

chEncoder.writeOutbound(data);
assertTrue(chEncoder.finish());

byte[] deflatedData = chEncoder.readOutbound().array();

{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.GZIP));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}


// This case will be failed with netty's jzlib.
{
EmbeddedByteChannel chDecoder =
new EmbeddedByteChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE));

chDecoder.writeInbound(Unpooled.wrappedBuffer(deflatedData));
assertTrue(chDecoder.finish());

assertEquals(data, chDecoder.readInbound());
}
}
}
119 changes: 0 additions & 119 deletions common/src/main/java/io/netty/util/internal/jzlib/Adler32.java

This file was deleted.

Loading

0 comments on commit 8fdf788

Please sign in to comment.