public static int maskedCrc32c(byte[] data) { return maskedCrc32c(data, 0, data.length); }
public static int maskedCrc32c(byte[] data, int offset, int length) { Crc32C crc32c = new Crc32C(); crc32c.update(data, offset, length); return crc32c.getMaskedValue(); }
public int getMaskedValue() { return mask(getIntValue()); }
/** * Create a new PureJavaCrc32 object. */ public Crc32C() { reset(); }
/** * Create a new PureJavaCrc32 object. */ public Crc32C() { reset(); }
public static int maskedCrc32c(byte[] data) { return maskedCrc32c(data, 0, data.length); }
public static int maskedCrc32c(byte[] data, int offset, int length) { Crc32C crc32c = new Crc32C(); crc32c.update(data, offset, length); return crc32c.getMaskedValue(); }
public int getMaskedValue() { return mask(getIntValue()); }
/** * {@link Crc32C#maskedCrc32c(byte[], int, int) Calculates} the crc, compresses * the data, determines if the compression ratio is acceptable and calls * {@link #writeBlock(OutputStream, byte[], int, int, boolean, int)} to * actually write the frame. * * @param input The byte[] containing the raw data to be compressed. * @param offset The offset into <i>input</i> where the data starts. * @param length The amount of data in <i>input</i>. */ private void writeCompressed(byte[] input, int offset, int length) throws IOException { // crc is based on the user supplied input data int crc32c = writeChecksums ? Crc32C.maskedCrc32c(input, offset, length) : 0; int compressed = compressor.compress(input, offset, length, outputBuffer, 0, outputBuffer.length); // only use the compressed data if compression ratio is <= the minCompressionRatio if (((double) compressed / (double) length) <= minCompressionRatio) { writeBlock(out, outputBuffer, 0, compressed, true, crc32c); } else { // otherwise use the uncompressed data. writeBlock(out, input, offset, length, false, crc32c); } }
/** * {@link Crc32C#maskedCrc32c(byte[], int, int) Calculates} the crc, compresses * the data, determines if the compression ratio is acceptable and calls * {@link #writeBlock(OutputStream, byte[], int, int, boolean, int)} to * actually write the frame. * * @param input The byte[] containing the raw data to be compressed. * @param offset The offset into <i>input</i> where the data starts. * @param length The amount of data in <i>input</i>. */ private void writeCompressed(byte[] input, int offset, int length) throws IOException { // crc is based on the user supplied input data int crc32c = writeChecksums ? Crc32C.maskedCrc32c(input, offset, length) : 0; int compressed = compressor.compress(input, offset, length, outputBuffer, 0, outputBuffer.length); // only use the compressed data if compression ratio is <= the minCompressionRatio if (((double) compressed / (double) length) <= minCompressionRatio) { writeBlock(out, outputBuffer, 0, compressed, true, crc32c); } else { // otherwise use the uncompressed data. writeBlock(out, input, offset, length, false, crc32c); } }
@Test public void testLargerFrames_raw_() throws IOException { byte[] random = getRandom(0.5, 100000); byte[] stream = new byte[SnappyFramed.HEADER_BYTES.length + 8 + random.length]; System.arraycopy(SnappyFramed.HEADER_BYTES, 0, stream, 0, SnappyFramed.HEADER_BYTES.length); stream[10] = SnappyFramed.UNCOMPRESSED_DATA_FLAG; int length = random.length + 4; stream[11] = (byte) length; stream[12] = (byte) (length >>> 8); stream[13] = (byte) (length >>> 16); int crc32c = Crc32C.maskedCrc32c(random); stream[14] = (byte) crc32c; stream[15] = (byte) (crc32c >>> 8); stream[16] = (byte) (crc32c >>> 16); stream[17] = (byte) (crc32c >>> 24); System.arraycopy(random, 0, stream, 18, random.length); byte[] uncompressed = uncompress(stream); assertEquals(random, uncompressed); }
@Test public void testLargerFrames_compressed_() throws IOException { byte[] random = getRandom(0.5, 500000); byte[] compressed = blockCompress(random); byte[] stream = new byte[SnappyFramed.HEADER_BYTES.length + 8 + compressed.length]; System.arraycopy(SnappyFramed.HEADER_BYTES, 0, stream, 0, SnappyFramed.HEADER_BYTES.length); stream[10] = SnappyFramed.COMPRESSED_DATA_FLAG; int length = compressed.length + 4; stream[11] = (byte) length; stream[12] = (byte) (length >>> 8); stream[13] = (byte) (length >>> 16); int crc32c = Crc32C.maskedCrc32c(random); stream[14] = (byte) crc32c; stream[15] = (byte) (crc32c >>> 8); stream[16] = (byte) (crc32c >>> 16); stream[17] = (byte) (crc32c >>> 24); System.arraycopy(compressed, 0, stream, 18, compressed.length); byte[] uncompressed = uncompress(stream); assertEquals(random, uncompressed); }
@Test public void testLargerFrames_compressed_smaller_raw_larger() throws IOException { byte[] random = getRandom(0.5, 100000); byte[] compressed = blockCompress(random); byte[] stream = new byte[SnappyFramed.HEADER_BYTES.length + 8 + compressed.length]; System.arraycopy(SnappyFramed.HEADER_BYTES, 0, stream, 0, SnappyFramed.HEADER_BYTES.length); stream[10] = SnappyFramed.COMPRESSED_DATA_FLAG; int length = compressed.length + 4; stream[11] = (byte) length; stream[12] = (byte) (length >>> 8); stream[13] = (byte) (length >>> 16); int crc32c = Crc32C.maskedCrc32c(random); stream[14] = (byte) crc32c; stream[15] = (byte) (crc32c >>> 8); stream[16] = (byte) (crc32c >>> 16); stream[17] = (byte) (crc32c >>> 24); System.arraycopy(compressed, 0, stream, 18, compressed.length); byte[] uncompressed = uncompress(stream); assertEquals(random, uncompressed); }
int actualCrc32c = Crc32C.maskedCrc32c(buffer, position, valid - position); if (frameData.checkSum != actualCrc32c) { throw new IOException("Corrupt input: invalid checksum");
int actualCrc32c = Crc32C.maskedCrc32c(buffer, position, valid - position); if (frameData.checkSum != actualCrc32c) { throw new IOException("Corrupt input: invalid checksum");