class Snappy
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
Snappy.State |
Modifier and Type | Field and Description |
---|---|
private static int |
COPY_1_BYTE_OFFSET |
private static int |
COPY_2_BYTE_OFFSET |
private static int |
COPY_4_BYTE_OFFSET |
private static int |
LITERAL |
private static int |
MAX_HT_SIZE |
private static int |
MIN_COMPRESSIBLE_BYTES |
private static int |
NOT_ENOUGH_INPUT |
private static int |
PREAMBLE_NOT_FULL |
private Snappy.State |
state |
private byte |
tag |
private int |
written |
Constructor and Description |
---|
Snappy() |
Modifier and Type | Method and Description |
---|---|
private static int |
bitsToEncode(int value)
Calculates the minimum number of bits required to encode a value.
|
static int |
calculateChecksum(ByteBuf data)
Computes the CRC32C checksum of the supplied data and performs the "mask" operation
on the computed checksum
|
static int |
calculateChecksum(ByteBuf data,
int offset,
int length)
Computes the CRC32C checksum of the supplied data and performs the "mask" operation
on the computed checksum
|
void |
decode(ByteBuf in,
ByteBuf out) |
private static int |
decodeCopyWith1ByteOffset(byte tag,
ByteBuf in,
ByteBuf out,
int writtenSoFar)
Reads a compressed reference offset and length from the supplied input
buffer, seeks back to the appropriate place in the input buffer and
writes the found data to the supplied output stream.
|
private static int |
decodeCopyWith2ByteOffset(byte tag,
ByteBuf in,
ByteBuf out,
int writtenSoFar)
Reads a compressed reference offset and length from the supplied input
buffer, seeks back to the appropriate place in the input buffer and
writes the found data to the supplied output stream.
|
private static int |
decodeCopyWith4ByteOffset(byte tag,
ByteBuf in,
ByteBuf out,
int writtenSoFar)
Reads a compressed reference offset and length from the supplied input
buffer, seeks back to the appropriate place in the input buffer and
writes the found data to the supplied output stream.
|
(package private) static int |
decodeLiteral(byte tag,
ByteBuf in,
ByteBuf out)
Reads a literal from the input buffer directly to the output buffer.
|
void |
encode(ByteBuf in,
ByteBuf out,
int length) |
private static void |
encodeCopy(ByteBuf out,
int offset,
int length)
Encodes a series of copies, each at most 64 bytes in length.
|
private static void |
encodeCopyWithOffset(ByteBuf out,
int offset,
int length) |
(package private) static void |
encodeLiteral(ByteBuf in,
ByteBuf out,
int length)
Writes a literal to the supplied output buffer by directly copying from
the input buffer.
|
private static int |
findMatchingLength(ByteBuf in,
int minIndex,
int inIndex,
int maxIndex)
Iterates over the supplied input buffer between the supplied minIndex and
maxIndex to find how long our matched copy overlaps with an already-written
literal value.
|
private static short[] |
getHashTable(int inputSize)
Creates an appropriately sized hashtable for the given input size
|
private static int |
hash(ByteBuf in,
int index,
int shift)
Hashes the 4 bytes located at index, shifting the resulting hash into
the appropriate range for our hash table.
|
(package private) static int |
maskChecksum(int checksum)
From the spec:
"Checksums are not stored directly, but masked, as checksumming data and
then its own checksum can be problematic.
|
private static int |
readPreamble(ByteBuf in)
Reads the length varint (a series of bytes, where the lower 7 bits
are data and the upper bit is a flag to indicate more bytes to be
read).
|
void |
reset() |
(package private) static void |
validateChecksum(int expectedChecksum,
ByteBuf data)
Computes the CRC32C checksum of the supplied data, performs the "mask" operation
on the computed checksum, and then compares the resulting masked checksum to the
supplied checksum.
|
(package private) static void |
validateChecksum(int expectedChecksum,
ByteBuf data,
int offset,
int length)
Computes the CRC32C checksum of the supplied data, performs the "mask" operation
on the computed checksum, and then compares the resulting masked checksum to the
supplied checksum.
|
private static void |
validateOffset(int offset,
int chunkSizeSoFar)
Validates that the offset extracted from a compressed reference is within
the permissible bounds of an offset (4 <= offset <= 32768), and does not
exceed the length of the chunk currently read so far.
|
private static final int MAX_HT_SIZE
private static final int MIN_COMPRESSIBLE_BYTES
private static final int PREAMBLE_NOT_FULL
private static final int NOT_ENOUGH_INPUT
private static final int LITERAL
private static final int COPY_1_BYTE_OFFSET
private static final int COPY_2_BYTE_OFFSET
private static final int COPY_4_BYTE_OFFSET
private Snappy.State state
private byte tag
private int written
public void reset()
private static int hash(ByteBuf in, int index, int shift)
in
- The input buffer to read 4 bytes fromindex
- The index to read atshift
- The shift value, for ensuring that the resulting value is
withing the range of our hash table sizeprivate static short[] getHashTable(int inputSize)
inputSize
- The size of our input, ie. the number of bytes we need to encodeprivate static int findMatchingLength(ByteBuf in, int minIndex, int inIndex, int maxIndex)
in
- The input buffer to scan overminIndex
- The index in the input buffer to start scanning frominIndex
- The index of the start of our copymaxIndex
- The length of our input bufferprivate static int bitsToEncode(int value)
value
- The value to calculate the minimum number of bits required to encodestatic void encodeLiteral(ByteBuf in, ByteBuf out, int length)
in
- The input buffer to copy fromout
- The output buffer to copy tolength
- The length of the literal to copyprivate static void encodeCopyWithOffset(ByteBuf out, int offset, int length)
private static void encodeCopy(ByteBuf out, int offset, int length)
out
- The output buffer to write the copy pointer tooffset
- The offset at which the original instance lieslength
- The length of the original instanceprivate static int readPreamble(ByteBuf in)
in
- The input buffer to read the preamble fromstatic int decodeLiteral(byte tag, ByteBuf in, ByteBuf out)
tag
- The tag that identified this segment as a literal is also
used to encode part of the length of the datain
- The input buffer to read the literal fromout
- The output buffer to write the literal toprivate static int decodeCopyWith1ByteOffset(byte tag, ByteBuf in, ByteBuf out, int writtenSoFar)
tag
- The tag used to identify this as a copy is also used to encode
the length and part of the offsetin
- The input buffer to read fromout
- The output buffer to write toDecompressionException
- If the read offset is invalidprivate static int decodeCopyWith2ByteOffset(byte tag, ByteBuf in, ByteBuf out, int writtenSoFar)
tag
- The tag used to identify this as a copy is also used to encode
the length and part of the offsetin
- The input buffer to read fromout
- The output buffer to write toDecompressionException
- If the read offset is invalidprivate static int decodeCopyWith4ByteOffset(byte tag, ByteBuf in, ByteBuf out, int writtenSoFar)
tag
- The tag used to identify this as a copy is also used to encode
the length and part of the offsetin
- The input buffer to read fromout
- The output buffer to write toDecompressionException
- If the read offset is invalidprivate static void validateOffset(int offset, int chunkSizeSoFar)
offset
- The offset extracted from the compressed referencechunkSizeSoFar
- The number of bytes read so far from this chunkDecompressionException
- if the offset is invalidpublic static int calculateChecksum(ByteBuf data)
data
- The input data to calculate the CRC32C checksum ofpublic static int calculateChecksum(ByteBuf data, int offset, int length)
data
- The input data to calculate the CRC32C checksum ofstatic void validateChecksum(int expectedChecksum, ByteBuf data)
expectedChecksum
- The checksum decoded from the stream to compare againstdata
- The input data to calculate the CRC32C checksum ofDecompressionException
- If the calculated and supplied checksums do not matchstatic void validateChecksum(int expectedChecksum, ByteBuf data, int offset, int length)
expectedChecksum
- The checksum decoded from the stream to compare againstdata
- The input data to calculate the CRC32C checksum ofDecompressionException
- If the calculated and supplied checksums do not matchstatic int maskChecksum(int checksum)
checksum
- The actual checksum of the data