Class StreamManipulator
This class allows us to retrieve a specified number of bits from the input buffer, as well as copy big byte blocks.
It uses an int buffer to store up to 31 bits for direct manipulation. This guarantees that we can get at least 16 bits, but we only need at most 15, so this is all safe.
There are some optimizations in this class, for example, you must never peek more than 8 bits more than needed, and you must first peek bits before you may drop them. This is not a general purpose class but optimized for the behaviour of the Inflater.
authors of the original java version : John Leuner, Jochen Hoenicke
Inheritance
Inherited Members
Namespace: ICSharpCode.SharpZipLib.Zip.Compression.Streams
Assembly: ICSharpCode.SharpZipLib.dll
Syntax
public class StreamManipulator
Properties
| Improve this Doc View SourceAvailableBits
Gets the number of bits available in the bit buffer. This must be only called when a previous PeekBits() returned -1.
Declaration
public int AvailableBits { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | the number of bits available. |
AvailableBytes
Gets the number of bytes available.
Declaration
public int AvailableBytes { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The number of bytes available. |
IsNeedingInput
Returns true when SetInput can be called
Declaration
public bool IsNeedingInput { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Methods
| Improve this Doc View SourceCopyBytes(Byte[], Int32, Int32)
Copies bytes from input buffer to output buffer starting at output[offset]. You have to make sure, that the buffer is byte aligned. If not enough bytes are available, copies fewer bytes.
Declaration
public int CopyBytes(byte[] output, int offset, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | output | The buffer to copy bytes to. |
| System.Int32 | offset | The offset in the buffer at which copying starts |
| System.Int32 | length | The length to copy, 0 is allowed. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of bytes copied, 0 if no bytes were available. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | Length is less than zero |
| System.InvalidOperationException | Bit buffer isnt byte aligned |
DropBits(Int32)
Drops the next n bits from the input. You should have called PeekBits with a bigger or equal n before, to make sure that enough bits are in the bit buffer.
Declaration
public void DropBits(int bitCount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | bitCount | The number of bits to drop. |
GetBits(Int32)
Gets the next n bits and increases input pointer. This is equivalent to PeekBits(Int32) followed by DropBits(Int32), except for correct error handling.
Declaration
public int GetBits(int bitCount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | bitCount | The number of bits to retrieve. |
Returns
| Type | Description |
|---|---|
| System.Int32 | the value of the bits, or -1 if not enough bits available. |
PeekBits(Int32)
Get the next sequence of bits but don't increase input pointer. bitCount must be less or equal 16 and if this call succeeds, you must drop at least n - 8 bits in the next call.
Declaration
public int PeekBits(int bitCount)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | bitCount | The number of bits to peek. |
Returns
| Type | Description |
|---|---|
| System.Int32 | the value of the bits, or -1 if not enough bits available. */ |
Reset()
Resets state and empties internal buffers
Declaration
public void Reset()
SetInput(Byte[], Int32, Int32)
Add more input for consumption. Only call when IsNeedingInput returns true
Declaration
public void SetInput(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | buffer | data to be input |
| System.Int32 | offset | offset of first byte of input |
| System.Int32 | count | number of bytes of input to add. |
SkipToByteBoundary()
Skips to the next byte boundary.
Declaration
public void SkipToByteBoundary()
TryGetBits(Int32, ref Byte[], Int32)
Tries to grab the next bitCount bits from the input and
sets index of array to the value.
Declaration
public bool TryGetBits(int bitCount, ref byte[] array, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | bitCount | |
| System.Byte[] | array | |
| System.Int32 | index |
Returns
| Type | Description |
|---|---|
| System.Boolean | true if enough bits could be read, otherwise false |
TryGetBits(Int32, ref Int32, Int32)
Tries to grab the next bitCount bits from the input and
sets output to the value, adding outputOffset.
Declaration
public bool TryGetBits(int bitCount, ref int output, int outputOffset = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | bitCount | |
| System.Int32 | output | |
| System.Int32 | outputOffset |
Returns
| Type | Description |
|---|---|
| System.Boolean | true if enough bits could be read, otherwise false |