下面列出了 io.netty.handler.codec.compression.CompressionException #com.jcraft.jzlib.JZlib 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
JZlib.WrapperType convertedWrapperType;
switch (wrapper) {
case NONE:
convertedWrapperType = JZlib.W_NONE;
break;
case ZLIB:
convertedWrapperType = JZlib.W_ZLIB;
break;
case GZIP:
convertedWrapperType = JZlib.W_GZIP;
break;
case ZLIB_OR_NONE:
convertedWrapperType = JZlib.W_ANY;
break;
default:
throw new Error();
}
return convertedWrapperType;
}
static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
JZlib.WrapperType convertedWrapperType;
switch (wrapper) {
case NONE:
convertedWrapperType = JZlib.W_NONE;
break;
case ZLIB:
convertedWrapperType = JZlib.W_ZLIB;
break;
case GZIP:
convertedWrapperType = JZlib.W_GZIP;
break;
case ZLIB_OR_NONE:
convertedWrapperType = JZlib.W_ANY;
break;
default:
throw new Error();
}
return convertedWrapperType;
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
/**
* Provide a more human-readable form of the underlying JZlib compression errors.<p>
*
* Should be made even more human-readable sometime later -- HR.
*/
protected String getJZlibErrorStr(int errNum) {
switch (errNum) {
case JZlib.Z_STREAM_ERROR:
return "stream error";
case JZlib.Z_DATA_ERROR:
return "data error";
case JZlib.Z_MEM_ERROR:
return "memory error";
case JZlib.Z_BUF_ERROR:
return "buffer error";
case JZlib.Z_VERSION_ERROR:
return "version error";
default:
return "unknown error";
}
}
public byte[] compress(byte[] buf, int start, int len) throws IOException {
compressOut.reset();
stream.next_in = buf;
stream.next_in_index = start;
stream.avail_in = len - start;
int status;
do {
stream.next_out = tmpbuf;
stream.next_out_index = 0;
stream.avail_out = BUF_SIZE;
status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
switch (status) {
case JZlib.Z_OK:
compressOut.write(tmpbuf, 0, BUF_SIZE - stream.avail_out);
break;
default:
throw new IOException("compress: deflate returnd " + status);
}
} while (stream.avail_out == 0);
return compressOut.toByteArray();
}
public void test(TestHarness th) {
String value = "Hello, world!";
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZOutputStream zOut = new ZOutputStream(out, JZlib.Z_BEST_COMPRESSION);
DataOutputStream dataOut = new DataOutputStream(zOut);
dataOut.writeUTF(value);
zOut.close();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ZInputStream zIn = new ZInputStream(in);
DataInputStream dataIn = new DataInputStream(zIn);
th.check(dataIn.readUTF(), value);
} catch (IOException e) {
th.fail("Unexpected exception: " + e);
}
}
SpdyHeaderBlockJZlibEncoder(
SpdyVersion version, int compressionLevel, int windowBits, int memLevel) {
super(version);
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);
}
}
}
/**
* Creates a new instance with the specified wrapper.使用指定的包装器创建一个新实例。
*
* @throws DecompressionException if failed to initialize zlib
*/
public JZlibDecoder(ZlibWrapper wrapper) {
if (wrapper == null) {
throw new NullPointerException("wrapper");
}
int resultCode = z.init(ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
}
/**
* Creates a new instance with the specified preset dictionary. The wrapper
* is always {@link ZlibWrapper#ZLIB} because it is the only format that
* supports the preset dictionary.使用指定的预置字典创建一个新实例。包装器总是ZlibWrapper。ZLIB是因为它是唯一支持预设字典的格式。
*
* @throws DecompressionException if failed to initialize zlib
*/
public JZlibDecoder(byte[] dictionary) {
if (dictionary == null) {
throw new NullPointerException("dictionary");
}
this.dictionary = dictionary;
int resultCode;
resultCode = z.inflateInit(JZlib.W_ZLIB);
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
}
/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel}, and
* the specified wrapper.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
*
* @throws CompressionException if failed to initialize zlib
* 使用指定的压缩级别、指定的windowbit、指定的memLevel和指定的包装器创建新的zlib编码器。
*/
public JZlibEncoder(ZlibWrapper wrapper, 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)");
}
if (wrapper == null) {
throw new NullPointerException("wrapper");
}
if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
throw new IllegalArgumentException(
"wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
"allowed for compression.");
}
int resultCode = z.init(
compressionLevel, windowBits, memLevel,
ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
wrapperOverhead = ZlibUtil.wrapperOverhead(wrapper);
}
/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel},
* and the specified preset dictionary. The wrapper is always
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
* the preset dictionary.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
* @param dictionary the preset dictionary
*
* @throws CompressionException if failed to initialize zlib
* 使用指定的压缩级别、指定的windowBits、指定的memLevel和指定的预设置字典创建一个新的zlib编码器。包装器总是ZlibWrapper。ZLIB是因为它是唯一支持预设字典的格式。
*/
public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
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)");
}
if (dictionary == null) {
throw new NullPointerException("dictionary");
}
int resultCode;
resultCode = z.deflateInit(
compressionLevel, windowBits, memLevel,
JZlib.W_ZLIB); // Default: ZLIB format
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
} else {
resultCode = z.deflateSetDictionary(dictionary, dictionary.length);
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
}
}
wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB);
}
private void CHECK_ERR(ZStream z, int err, String msg) {
if (err != JZlib.Z_OK) {
if(z.msg != null) {
logger.error(z.msg);
}
logger.error("{} error: {}", msg, err);
throw new RuntimeException("Error on decode: " + msg + " error: " + err);
}
}
/**
* Compress the input. The result will be put in a new buffer.
*
* @param inBuffer the buffer to be compressed. The contents are transferred
* into a local byte array and the buffer is flipped and returned intact.
* @return the buffer with the compressed data
* @throws IOException if the compression of teh buffer failed for some reason
* @throws IllegalStateException if the mode is not <code>MODE_DEFLATER</code>
*/
public IoBuffer deflate(IoBuffer inBuffer) throws IOException {
if (mode == MODE_INFLATER) {
throw new IllegalStateException("not initialized as DEFLATER");
}
byte[] inBytes = new byte[inBuffer.remaining()];
inBuffer.get(inBytes).flip();
// according to spec, destination buffer should be 0.1% larger
// than source length plus 12 bytes. We add a single byte to safeguard
// against rounds that round down to the smaller value
int outLen = (int) Math.round(inBytes.length * 1.001) + 1 + 12;
byte[] outBytes = new byte[outLen];
synchronized (zStream) {
zStream.next_in = inBytes;
zStream.next_in_index = 0;
zStream.avail_in = inBytes.length;
zStream.next_out = outBytes;
zStream.next_out_index = 0;
zStream.avail_out = outBytes.length;
int retval = zStream.deflate(JZlib.Z_SYNC_FLUSH);
if (retval != JZlib.Z_OK) {
outBytes = null;
inBytes = null;
throw new IOException("Compression failed with return value : " + retval);
}
IoBuffer outBuf = IoBuffer.wrap(outBytes, 0, zStream.next_out_index);
return outBuf;
}
}
SpdyHeaderBlockJZlibEncoder(
SpdyVersion version, int compressionLevel, int windowBits, int memLevel) {
super(version);
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);
}
}
}
/**
* Creates a new instance with the specified wrapper.
*
* @throws DecompressionException if failed to initialize zlib
*/
public JZlibDecoder(ZlibWrapper wrapper) {
if (wrapper == null) {
throw new NullPointerException("wrapper");
}
int resultCode = z.init(ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
}
/**
* Creates a new instance with the specified preset dictionary. The wrapper
* is always {@link ZlibWrapper#ZLIB} because it is the only format that
* supports the preset dictionary.
*
* @throws DecompressionException if failed to initialize zlib
*/
public JZlibDecoder(byte[] dictionary) {
if (dictionary == null) {
throw new NullPointerException("dictionary");
}
this.dictionary = dictionary;
int resultCode;
resultCode = z.inflateInit(JZlib.W_ZLIB);
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
}
/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel}, and
* the specified wrapper.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
*
* @throws CompressionException if failed to initialize zlib
*/
public JZlibEncoder(ZlibWrapper wrapper, 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)");
}
if (wrapper == null) {
throw new NullPointerException("wrapper");
}
if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
throw new IllegalArgumentException(
"wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
"allowed for compression.");
}
int resultCode = z.init(
compressionLevel, windowBits, memLevel,
ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
wrapperOverhead = ZlibUtil.wrapperOverhead(wrapper);
}
/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel},
* and the specified preset dictionary. The wrapper is always
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
* the preset dictionary.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
* @param dictionary the preset dictionary
*
* @throws CompressionException if failed to initialize zlib
*/
public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
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)");
}
if (dictionary == null) {
throw new NullPointerException("dictionary");
}
int resultCode;
resultCode = z.deflateInit(
compressionLevel, windowBits, memLevel,
JZlib.W_ZLIB); // Default: ZLIB format
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
} else {
resultCode = z.deflateSetDictionary(dictionary, dictionary.length);
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
}
}
wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB);
}
public RpcGZIPOutputStream(OutputStream out) throws IOException {
super(out);
this.jzOutputSream = new ZStream();
this.jzOutputSream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION, ZBITS, true);
this.jzBytes = new byte[ZBUF_SIZE];
this.jzOutputSream.next_out = this.jzBytes;
this.jzOutputSream.next_out_index = 0;
this.jzOutputSream.avail_out = this.jzBytes.length;
}
/**
* Deflate (compress) the passed-in bytes and -- if appropriate --
* send the compressed bytes downstream to the filter's output stream.<p>
*
* This write method does not necessarily cause a write to the
* server -- a write will only occur when the jzBytes buffer
* is full, or on a later flush. This is a consequence of the
* way GZIP streaming works here, and means you must ensure that
* a suitable flush is done at a suitable (packet) boundary. See
* the comments for flush() below.
*
* @see java.io.FilterOutputStream#write(byte[], int, int)
*/
@Override
public void write(byte[] bytes, int offset, int len) throws IOException {
if (bytes == null) {
throw new NullPointerError(
"null byte array passed to RpcGZIPOutputStream.write()");
}
if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) {
throw new P4JavaError(
"bad length or offset in RpcGZIPOutputStream.write()");
}
this.jzOutputSream.next_in = bytes;
this.jzOutputSream.avail_in = len;
this.jzOutputSream.next_in_index = offset;
while (this.jzOutputSream.avail_in != 0) {
if (this.jzOutputSream.avail_out == 0) {
this.out.write(this.jzBytes);
this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe...
this.jzOutputSream.avail_out = this.jzBytes.length;
this.jzOutputSream.next_out_index = 0;
}
int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH);
if (jzErr != JZlib.Z_OK) {
throw new IOException("connection compression error: "
+ getJZlibErrorStr(jzErr));
}
}
}
/**
* Flush the results of previous byte deflation (compression) downstream.<p>
*
* As a consequence of the way GZIP streaming works, this flush is often the only
* place where bytes are actually written downstream towards the server (the earlier
* writes may only write to the internal buffer here). Using flush causes a compression
* boundary, so it should only be used after a complete packet has been put onto
* this stream -- i.e. users of this stream must call flush appropriately, or the
* server may not see packets at all.
*
* @see java.io.FilterOutputStream#flush()
*/
@Override
public void flush() throws IOException {
this.jzOutputSream.avail_in = 0;
boolean done = false;
while (true) {
if ((this.jzOutputSream.avail_out == 0) || done) {
out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out);
this.jzOutputSream.next_out = this.jzBytes;
this.jzOutputSream.avail_out = this.jzBytes.length;
this.jzOutputSream.next_out_index = 0;
}
if (done) {
break;
}
int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH);
if (jzErr != JZlib.Z_OK) {
throw new IOException("Perforce connection compression error: "
+ getJZlibErrorStr(jzErr));
}
if (this.jzOutputSream.avail_out != 0) {
done = true;
}
}
}
public RpcGZIPOutputStream(OutputStream out) throws IOException {
super(out);
this.jzOutputSream = new ZStream();
this.jzOutputSream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION, ZBITS, true);
this.jzBytes = new byte[ZBUF_SIZE];
this.jzOutputSream.next_out = this.jzBytes;
this.jzOutputSream.next_out_index = 0;
this.jzOutputSream.avail_out = this.jzBytes.length;
}
/**
* Deflate (compress) the passed-in bytes and -- if appropriate --
* send the compressed bytes downstream to the filter's output stream.<p>
*
* This write method does not necessarily cause a write to the
* server -- a write will only occur when the jzBytes buffer
* is full, or on a later flush. This is a consequence of the
* way GZIP streaming works here, and means you must ensure that
* a suitable flush is done at a suitable (packet) boundary. See
* the comments for flush() below.
*
* @see java.io.FilterOutputStream#write(byte[], int, int)
*/
@Override
public void write(byte[] bytes, int offset, int len) throws IOException {
if (bytes == null) {
throw new NullPointerError(
"null byte array passed to RpcGZIPOutputStream.write()");
}
if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) {
throw new P4JavaError(
"bad length or offset in RpcGZIPOutputStream.write()");
}
this.jzOutputSream.next_in = bytes;
this.jzOutputSream.avail_in = len;
this.jzOutputSream.next_in_index = offset;
while (this.jzOutputSream.avail_in != 0) {
if (this.jzOutputSream.avail_out == 0) {
this.out.write(this.jzBytes);
this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe...
this.jzOutputSream.avail_out = this.jzBytes.length;
this.jzOutputSream.next_out_index = 0;
}
int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH);
if (jzErr != JZlib.Z_OK) {
throw new IOException("connection compression error: "
+ getJZlibErrorStr(jzErr));
}
}
}
/**
* Flush the results of previous byte deflation (compression) downstream.<p>
*
* As a consequence of the way GZIP streaming works, this flush is often the only
* place where bytes are actually written downstream towards the server (the earlier
* writes may only write to the internal buffer here). Using flush causes a compression
* boundary, so it should only be used after a complete packet has been put onto
* this stream -- i.e. users of this stream must call flush appropriately, or the
* server may not see packets at all.
*
* @see java.io.FilterOutputStream#flush()
*/
@Override
public void flush() throws IOException {
this.jzOutputSream.avail_in = 0;
boolean done = false;
while (true) {
if ((this.jzOutputSream.avail_out == 0) || done) {
out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out);
this.jzOutputSream.next_out = this.jzBytes;
this.jzOutputSream.avail_out = this.jzBytes.length;
this.jzOutputSream.next_out_index = 0;
}
if (done) {
break;
}
int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH);
if (jzErr != JZlib.Z_OK) {
throw new IOException("Perforce connection compression error: "
+ getJZlibErrorStr(jzErr));
}
if (this.jzOutputSream.avail_out != 0) {
done = true;
}
}
}
public RpcGZIPOutputStream(OutputStream out) throws IOException {
super(out);
this.jzOutputSream = new ZStream();
this.jzOutputSream.deflateInit(JZlib.Z_DEFAULT_COMPRESSION, ZBITS, true);
this.jzBytes = new byte[ZBUF_SIZE];
this.jzOutputSream.next_out = this.jzBytes;
this.jzOutputSream.next_out_index = 0;
this.jzOutputSream.avail_out = this.jzBytes.length;
}
/**
* Deflate (compress) the passed-in bytes and -- if appropriate --
* send the compressed bytes downstream to the filter's output stream.<p>
*
* This write method does not necessarily cause a write to the
* server -- a write will only occur when the jzBytes buffer
* is full, or on a later flush. This is a consequence of the
* way GZIP streaming works here, and means you must ensure that
* a suitable flush is done at a suitable (packet) boundary. See
* the comments for flush() below.
*
* @see java.io.FilterOutputStream#write(byte[], int, int)
*/
@Override
public void write(byte[] bytes, int offset, int len) throws IOException {
if (bytes == null) {
throw new NullPointerError(
"null byte array passed to RpcGZIPOutputStream.write()");
}
if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) {
throw new P4JavaError(
"bad length or offset in RpcGZIPOutputStream.write()");
}
this.jzOutputSream.next_in = bytes;
this.jzOutputSream.avail_in = len;
this.jzOutputSream.next_in_index = offset;
while (this.jzOutputSream.avail_in != 0) {
if (this.jzOutputSream.avail_out == 0) {
this.out.write(this.jzBytes);
this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe...
this.jzOutputSream.avail_out = this.jzBytes.length;
this.jzOutputSream.next_out_index = 0;
}
int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH);
if (jzErr != JZlib.Z_OK) {
throw new IOException("connection compression error: "
+ getJZlibErrorStr(jzErr));
}
}
}