public final class ZlibCodec extends Object
Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
RFC 1950 - ZLIB
and RFC 1951 - DEFLATE
.
Modifier and Type | Field and Description |
---|---|
int |
AvailableBytesIn
The number of bytes available in the InputBuffer, starting at NextIn.
|
int |
AvailableBytesOut
The number of bytes available in the OutputBuffer, starting at NextOut.
|
int |
CompressLevel
The compression level to use in this codec.
|
byte[] |
InputBuffer
The buffer from which data is taken.
|
String |
Message
used for diagnostics, when something goes wrong!
|
int |
NextIn
An index into the InputBuffer array, indicating where to start reading.
|
int |
NextOut
An index into the OutputBuffer array, indicating where to start writing.
|
byte[] |
OutputBuffer
Buffer to store output data.
|
int |
Strategy
The compression strategy to use.
|
long |
TotalBytesIn
Total number of bytes read so far, through all calls to Inflate()/Deflate().
|
long |
TotalBytesOut
Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
|
int |
WindowBits
The number of Window Bits to use.
|
Constructor and Description |
---|
ZlibCodec()
Create a ZlibCodec.
|
ZlibCodec(int mode)
Create a ZlibCodec that either compresses or decompresses.
|
Modifier and Type | Method and Description |
---|---|
int |
deflate(int flush)
Deflate one batch of data.
|
int |
endDeflate()
End a deflation session.
|
int |
endInflate()
Ends an inflation session.
|
long |
getAdler32()
The Adler32 checksum on the data transferred through the codec so far.
|
int |
inflate(int flush)
Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
|
int |
initializeDeflate()
Initialize the ZlibCodec for deflation operation.
|
int |
initializeDeflate(int level)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
|
int |
initializeDeflate(int level,
boolean wantRfc1950Header)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
and the explicit flag governing whether to emit an RFC1950 header byte pair.
|
int |
initializeDeflate(int level,
int bits)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
and the specified number of window bits.
|
int |
initializeDeflate(int level,
int bits,
boolean wantRfc1950Header)
Initialize the ZlibCodec for deflation operation, using the specified
CompressionLevel, the specified number of window bits, and the explicit flag
governing whether to emit an RFC1950 header byte pair.
|
int |
initializeInflate()
Initialize the inflation state.
|
int |
initializeInflate(boolean expectRfc1950Header)
Initialize the inflation state with an explicit flag to
govern the handling of RFC1950 header bytes.
|
int |
initializeInflate(int windowBits)
Initialize the ZlibCodec for inflation, with the specified number of window bits.
|
int |
initializeInflate(int windowBits,
boolean expectRfc1950Header)
Initialize the inflation state with an explicit flag to govern the handling of
RFC1950 header bytes.
|
void |
resetDeflate()
Reset a codec for another deflation session.
|
int |
setDeflateParams(int level,
int strategy)
Set the CompressionStrategy and CompressionLevel for a deflation session.
|
int |
setDictionary(byte[] dictionary)
Set the dictionary to be used for either Inflation or Deflation.
|
int |
syncInflate()
I don't know what this does!
|
public byte[] InputBuffer
The buffer from which data is taken.
public int NextIn
An index into the InputBuffer array, indicating where to start reading.
public int AvailableBytesIn
The number of bytes available in the InputBuffer, starting at NextIn.
public long TotalBytesIn
Total number of bytes read so far, through all calls to Inflate()/Deflate().
public byte[] OutputBuffer
Buffer to store output data.
public int NextOut
An index into the OutputBuffer array, indicating where to start writing.
public int AvailableBytesOut
The number of bytes available in the OutputBuffer, starting at NextOut.
public long TotalBytesOut
Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
public String Message
used for diagnostics, when something goes wrong!
public int CompressLevel
The compression level to use in this codec. Useful only in compression mode.
public int WindowBits
The number of Window Bits to use.
public int Strategy
The compression strategy to use.
public ZlibCodec()
Create a ZlibCodec.
public ZlibCodec(int mode)
Create a ZlibCodec that either compresses or decompresses.
mode
- Indicates whether the codec should compress (deflate) or decompress (inflate).public long getAdler32()
The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
public int initializeInflate()
Initialize the inflation state.
public int initializeInflate(boolean expectRfc1950Header)
Initialize the inflation state with an explicit flag to govern the handling of RFC1950 header bytes.
expectRfc1950Header
- whether to expect an RFC1950 header byte
pair when reading the stream of data to be inflated.
RFC 1950
is expected. If
you want to read a zlib stream you should specify true for
expectRfc1950Header. If you have a deflate stream, you will want to specify
false. It is only necessary to invoke this initializer explicitly if you
want to specify false.
public int initializeInflate(int windowBits)
Initialize the ZlibCodec for inflation, with the specified number of window bits.
windowBits
- The number of window bits to use. If you need to ask what that is,
then you shouldn't be calling this initializer.public int initializeInflate(int windowBits, boolean expectRfc1950Header)
Initialize the inflation state with an explicit flag to govern the handling of RFC1950 header bytes.
expectRfc1950Header
- whether to expect an RFC1950 header byte pair when reading
the stream of data to be inflated.windowBits
- The number of window bits to use. If you need to ask what that is,
then you shouldn't be calling this initializer.
RFC
1950
, in the compressed stream. If you will be reading a DEFLATE or
GZIP stream, which does not have such a header, you will want to specify
false.
public int inflate(int flush)
Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
private void InflateBuffer() { int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; ZlibCodec decompressor = new ZlibCodec(); Console.WriteLine("\n============================================"); Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length); MemoryStream ms = new MemoryStream(DecompressedBytes); int rc = decompressor.InitializeInflate(); decompressor.InputBuffer = CompressedBytes; decompressor.NextIn = 0; decompressor.AvailableBytesIn = CompressedBytes.Length; decompressor.OutputBuffer = buffer; // pass 1: inflate do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = buffer.Length; rc = decompressor.Inflate(ZlibConstants.Z_NO_FLUSH); if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) throw new Exception("inflating: " + decompressor.Message); ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut); } while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); // pass 2: finish and flush do { decompressor.NextOut = 0; decompressor.AvailableBytesOut = buffer.Length; rc = decompressor.Inflate(ZlibConstants.Z_FINISH); if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) throw new Exception("inflating: " + decompressor.Message); if (buffer.Length - decompressor.AvailableBytesOut > 0) ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut); } while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); decompressor.EndInflate(); }
flush
- The flush to use when inflating.
public int endInflate()
Ends an inflation session.
public int syncInflate()
I don't know what this does!
public int initializeDeflate()
Initialize the ZlibCodec for deflation operation.
int bufferSize = 40000; byte[] CompressedBytes = new byte[bufferSize]; byte[] DecompressedBytes = new byte[bufferSize]; ZlibCodec compressor = new ZlibCodec(); compressor.InitializeDeflate(CompressionLevel.Default); compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress); compressor.NextIn = 0; compressor.AvailableBytesIn = compressor.InputBuffer.Length; compressor.OutputBuffer = CompressedBytes; compressor.NextOut = 0; compressor.AvailableBytesOut = CompressedBytes.Length; while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize) { compressor.Deflate(ZlibConstants.Z_NO_FLUSH); } while (true) { int rc= compressor.Deflate(ZlibConstants.Z_FINISH); if (rc == ZlibConstants.Z_STREAM_END) break; } compressor.EndDeflate();
public int initializeDeflate(int level)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
level
- The compression level for the codec.
public int initializeDeflate(int level, boolean wantRfc1950Header)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, and the explicit flag governing whether to emit an RFC1950 header byte pair.
level
- The compression level for the codec.wantRfc1950Header
- whether to emit an initial RFC1950 byte pair in the compressed stream.
RFC
1950
, in the compressed stream.
public int initializeDeflate(int level, int bits)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, and the specified number of window bits.
level
- The compression level for the codec.bits
- the number of window bits to use. If you don't know what this means, don't use this method.
public int initializeDeflate(int level, int bits, boolean wantRfc1950Header)
Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, the specified number of window bits, and the explicit flag governing whether to emit an RFC1950 header byte pair.
level
- The compression level for the codec.wantRfc1950Header
- whether to emit an initial RFC1950 byte pair in the compressed stream.bits
- the number of window bits to use. If you don't know what this means, don't use this method.public int deflate(int flush)
Deflate one batch of data.
private void DeflateBuffer(CompressionLevel level) { int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; ZlibCodec compressor = new ZlibCodec(); Console.WriteLine("\n============================================"); Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length); MemoryStream ms = new MemoryStream(); int rc = compressor.InitializeDeflate(level); compressor.InputBuffer = UncompressedBytes; compressor.NextIn = 0; compressor.AvailableBytesIn = UncompressedBytes.Length; compressor.OutputBuffer = buffer; // pass 1: deflate do { compressor.NextOut = 0; compressor.AvailableBytesOut = buffer.Length; rc = compressor.Deflate(ZlibConstants.Z_NO_FLUSH); if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) throw new Exception("deflating: " + compressor.Message); ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut); } while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); // pass 2: finish and flush do { compressor.NextOut = 0; compressor.AvailableBytesOut = buffer.Length; rc = compressor.Deflate(ZlibConstants.Z_FINISH); if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) throw new Exception("deflating: " + compressor.Message); if (buffer.Length - compressor.AvailableBytesOut > 0) ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut); } while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); compressor.EndDeflate(); ms.Seek(0, SeekOrigin.Begin); CompressedBytes = new byte[compressor.TotalBytesOut]; ms.Read(CompressedBytes, 0, CompressedBytes.Length); }
flush
- whether to flush all data as you deflate. Generally you will want to
use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to
flush everything.
public int endDeflate()
End a deflation session.
public void resetDeflate()
Reset a codec for another deflation session.
public int setDeflateParams(int level, int strategy)
Set the CompressionStrategy and CompressionLevel for a deflation session.
level
- the level of compression to use.strategy
- the strategy to use for compression.public int setDictionary(byte[] dictionary)
Set the dictionary to be used for either Inflation or Deflation.
dictionary
- The dictionary bytes to use.Copyright © 2016 Aspose. All Rights Reserved.