com.jcraft.jzlib.JZlib Java Examples

The following examples show how to use com.jcraft.jzlib.JZlib. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJZlib.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: ZLibCompression.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 6 votes vote down vote up
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();
	}
 
Example #3
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #4
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #5
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #6
Source File: ZlibUtil.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #8
Source File: ZlibUtil.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #9
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #10
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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";
	}
}
 
Example #11
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example #12
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #13
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #14
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #15
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example #16
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #17
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #18
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example #19
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #20
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #21
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example #22
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
		}
	}
}
 
Example #24
Source File: RpcGZIPOutputStream.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
		}
	}
}
 
Example #25
Source File: ZLibCompression.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 5 votes vote down vote up
public byte[] uncompress(byte[] buffer, int start, int length)
		throws IOException {

	// int inflated_end = 0;
	uncompressOut.reset();

	stream.next_in = buffer;
	stream.next_in_index = start;
	stream.avail_in = length;

	while (true) {
		stream.next_out = inflated_buf;
		stream.next_out_index = 0;
		stream.avail_out = BUF_SIZE;
		int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH);
		switch (status) {
		case JZlib.Z_OK:
			uncompressOut.write(inflated_buf, 0, BUF_SIZE
					- stream.avail_out);
			break;
		case JZlib.Z_BUF_ERROR:
			return uncompressOut.toByteArray();
		default:
			throw new IOException("uncompress: inflate returnd " + status);
		}
	}
}
 
Example #26
Source File: SpdyHeaderBlockJZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #27
Source File: JZlibDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #28
Source File: JZlibDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #29
Source File: JZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #30
Source File: JZlibEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}