Extracted the utility methods in ZlibEncoder to a new utility class 'ZlibUtil'

This commit is contained in:
Trustin Lee 2009-10-21 03:44:05 +00:00
parent f355d74eb0
commit b646071570
3 changed files with 51 additions and 20 deletions

View File

@ -48,7 +48,7 @@ public class ZlibDecoder extends OneToOneDecoder {
public ZlibDecoder() throws ZStreamException { public ZlibDecoder() throws ZStreamException {
int resultCode = z.inflateInit(); int resultCode = z.inflateInit();
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
ZlibEncoder.fail(z, "initialization failure", resultCode); ZlibUtil.fail(z, "initialization failure", resultCode);
} }
} }
@ -65,11 +65,11 @@ public class ZlibDecoder extends OneToOneDecoder {
int resultCode; int resultCode;
resultCode = z.inflateInit(); resultCode = z.inflateInit();
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
ZlibEncoder.fail(z, "initialization failure", resultCode); ZlibUtil.fail(z, "initialization failure", resultCode);
} else { } else {
resultCode = z.inflateSetDictionary(dictionary, dictionary.length); resultCode = z.inflateSetDictionary(dictionary, dictionary.length);
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
ZlibEncoder.fail(z, "failed to set the dictionary", resultCode); ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
} }
} }
} }
@ -111,9 +111,7 @@ public class ZlibDecoder extends OneToOneDecoder {
z.avail_out = out.length; z.avail_out = out.length;
break; break;
default: default:
throw new ZStreamException( ZlibUtil.fail(z, "decompression failure", resultCode);
"decompression failure (" + resultCode + ")" +
(z.msg != null? ": " + z.msg : ""));
} }
} while (z.avail_in > 0); } while (z.avail_in > 0);

View File

@ -79,7 +79,7 @@ public class ZlibEncoder extends OneToOneEncoder {
public ZlibEncoder(int compressionLevel) throws ZStreamException { public ZlibEncoder(int compressionLevel) throws ZStreamException {
int resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format int resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
fail(z, "initialization failure", resultCode); ZlibUtil.fail(z, "initialization failure", resultCode);
} }
} }
@ -119,11 +119,11 @@ public class ZlibEncoder extends OneToOneEncoder {
int resultCode; int resultCode;
resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
fail(z, "initialization failure", resultCode); ZlibUtil.fail(z, "initialization failure", resultCode);
} else { } else {
resultCode = z.deflateSetDictionary(dictionary, dictionary.length); resultCode = z.deflateSetDictionary(dictionary, dictionary.length);
if (resultCode != JZlib.Z_OK){ if (resultCode != JZlib.Z_OK){
fail(z, "failed to set the dictionary", resultCode); ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
} }
} }
} }
@ -152,7 +152,7 @@ public class ZlibEncoder extends OneToOneEncoder {
// Note that Z_PARTIAL_FLUSH has been deprecated. // Note that Z_PARTIAL_FLUSH has been deprecated.
int resultCode = z.deflate(JZlib.Z_SYNC_FLUSH); int resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
if (resultCode != JZlib.Z_OK) { if (resultCode != JZlib.Z_OK) {
fail(z, "compression failure", resultCode); ZlibUtil.fail(z, "compression failure", resultCode);
} }
if (z.next_out_index != 0) { if (z.next_out_index != 0) {
@ -216,7 +216,7 @@ public class ZlibEncoder extends OneToOneEncoder {
if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) { if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
future = Channels.failedFuture( future = Channels.failedFuture(
ctx.getChannel(), ctx.getChannel(),
exception(z, "compression failure", resultCode)); ZlibUtil.exception(z, "compression failure", resultCode));
} else if (z.next_out_index != 0) { } else if (z.next_out_index != 0) {
future = Channels.future(ctx.getChannel()); future = Channels.future(ctx.getChannel());
Channels.write( Channels.write(
@ -250,13 +250,4 @@ public class ZlibEncoder extends OneToOneEncoder {
z.next_out = null; z.next_out = null;
} }
} }
static void fail(ZStream z, String message, int resultCode) throws ZStreamException {
throw exception(z, message, resultCode);
}
static ZStreamException exception(ZStream z, String message, int resultCode) {
return new ZStreamException(message + " (" + resultCode + ")" +
(z.msg != null? ": " + z.msg : ""));
}
} }

View File

@ -0,0 +1,42 @@
/*
* Copyright 2009 Red Hat, Inc.
*
* Red Hat 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.jboss.netty.handler.codec.compression;
import com.jcraft.jzlib.ZStream;
import com.jcraft.jzlib.ZStreamException;
/**
* Utility methods used by {@link ZlibEncoder} and {@link ZlibDecoder}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
class ZlibUtil {
static void fail(ZStream z, String message, int resultCode) throws ZStreamException {
throw exception(z, message, resultCode);
}
static ZStreamException exception(ZStream z, String message, int resultCode) {
return new ZStreamException(message + " (" + resultCode + ")" +
(z.msg != null? ": " + z.msg : ""));
}
private ZlibUtil() {
super();
}
}