Refine search
/** * Compute the CRC32C (Castagnoli) of the segment of the byte array given by the specified size and offset * * @param bytes The bytes to checksum * @param offset the offset at which to begin the checksum computation * @param size the number of bytes to checksum * @return The CRC32C */ public static long compute(byte[] bytes, int offset, int size) { Checksum crc = create(); crc.update(bytes, offset, size); return crc.getValue(); }
public long computeCrc() throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; Checksum checksum = new Adler32(); for (File file : _files) { try (InputStream input = new FileInputStream(file)) { int len; while ((len = input.read(buffer)) > 0) { checksum.update(buffer, 0, len); } } } return checksum.getValue(); }
/** * Reads from the stream into a byte array. * @throws IOException if the underlying stream throws or the * stream is exhausted and the Checksum doesn't match the expected * value */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { final int ret = in.read(b, off, len); if (ret >= 0) { checksum.update(b, off, ret); bytesRemaining -= ret; } if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) { throw new IOException("Checksum verification failed"); } return ret; }
/** * Calculate checksum on all the data read from input stream. * * This should be more efficient than the equivalent code * {@code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))} */ public static long calculateChecksum(InputStream stream) throws IOException { Checksum sum = new CRC32(); byte[] buf = new byte[4096]; int count; while ((count = stream.read(buf)) != -1) { if (count > 0) { sum.update(buf, 0, count); } } return sum.getValue(); }
/** * Reads a single byte from the stream * @throws IOException if the underlying stream throws or the * stream is exhausted and the Checksum doesn't match the expected * value */ @Override public int read() throws IOException { if (bytesRemaining <= 0) { return -1; } final int ret = in.read(); if (ret >= 0) { checksum.update(ret); --bytesRemaining; } if (bytesRemaining == 0 && expectedChecksum != checksum.getValue()) { throw new IOException("Checksum verification failed"); } return ret; }
private void encodeOrThrow(OutputStream os) throws IOException { ByteArrayOutputStream headersAndPayload = new ByteArrayOutputStream(); { DataOutputStream dos = new DataOutputStream(headersAndPayload); for (Entry<String, HeaderValue> entry : headers.entrySet()) { Header.encode(entry, dos); } dos.write(payload); dos.flush(); } int totalLength = Prelude.LENGTH_WITH_CRC + headersAndPayload.size() + 4; { byte[] preludeBytes = getPrelude(totalLength); Checksum crc = new CRC32(); crc.update(preludeBytes, 0, preludeBytes.length); DataOutputStream dos = new DataOutputStream(os); dos.write(preludeBytes); long value = crc.getValue(); int value1 = (int) value; dos.writeInt(value1); dos.flush(); } headersAndPayload.writeTo(os); }
@Test public void testUpdate() { final byte[] bytes = "Any String you want".getBytes(); final int len = bytes.length; Checksum crc1 = Crc32C.create(); Checksum crc2 = Crc32C.create(); Checksum crc3 = Crc32C.create(); crc1.update(bytes, 0, len); for (int i = 0; i < len; i++) crc2.update(bytes[i]); crc3.update(bytes, 0, len / 2); crc3.update(bytes, len / 2, len - len / 2); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); assertEquals("Crc values should be the same", crc1.getValue(), crc3.getValue()); }
@Test public void testUpdate() { final byte[] bytes = "Any String you want".getBytes(); final int len = bytes.length; Checksum crc1 = Crc32C.create(); Checksum crc2 = Crc32C.create(); Checksum crc3 = Crc32C.create(); crc1.update(bytes, 0, len); for (int i = 0; i < len; i++) crc2.update(bytes[i]); crc3.update(bytes, 0, len / 2); crc3.update(bytes, len / 2, len - len / 2); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); assertEquals("Crc values should be the same", crc1.getValue(), crc3.getValue()); }
@Test public void testUpdateInt() { final int value = 1000; final ByteBuffer buffer = ByteBuffer.allocate(4); buffer.putInt(value); Checksum crc1 = Crc32C.create(); Checksum crc2 = Crc32C.create(); Checksums.updateInt(crc1, value); crc2.update(buffer.array(), buffer.arrayOffset(), 4); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); }
@Test public void testUpdateLong() { final long value = Integer.MAX_VALUE + 1; final ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(value); Checksum crc1 = new Crc32(); Checksum crc2 = new Crc32(); Checksums.updateLong(crc1, value); crc2.update(buffer.array(), buffer.arrayOffset(), 8); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); }
private boolean checkCRCChecksum(byte[] data, final ChunkInfo ci, boolean hasZF) { if (data == null || data.length == 0) return false; final Checksum checksum = new CRC32(); checksum.update(data, 0, data.length); final long currentChecksum = checksum.getValue(); final long expectedChecksum = ci.getChecksum(); if (log.isDebugEnabled()) log.debug("CurrentChecksum : " + currentChecksum + "; ExpectedChecksum : " + expectedChecksum + " for key : " + ci.getKey()); if (currentChecksum != expectedChecksum) { if (!hasZF) { if (log.isWarnEnabled()) log.warn("CHECKSUM_ERROR : Chunks : " + ci.getChunks() + " ; " + "currentChecksum : " + currentChecksum + "; expectedChecksum : " + expectedChecksum + " for key : " + ci.getKey()); EVCacheMetricsFactory.increment(appName + "-CHECK_SUM_ERROR"); } return false; } return true; }
@Override public void run() { final long st = System.nanoTime(); crc.reset(); for (int i = 0; i < trials; i++) { crc.update(bytes, 0, size); } final long et = System.nanoTime(); final double secsElapsed = (et - st) / 1000000000.0d; results[index] = new BenchResult(crc.getValue(), mbProcessed/secsElapsed); } };
@Test public void testChecksumCRC32() throws Exception { // create a test file final String text = "Imagination is more important than knowledge - Einstein"; final File file = new File(getTestDirectory(), "checksum-test.txt"); FileUtils.writeStringToFile(file, text, "US-ASCII"); // compute the expected checksum final Checksum expectedChecksum = new CRC32(); expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length()); final long expectedValue = expectedChecksum.getValue(); // compute the checksum of the file final long resultValue = FileUtils.checksumCRC32(file); assertEquals(expectedValue, resultValue); }
@Test public void testChecksum() throws Exception { // create a test file final String text = "Imagination is more important than knowledge - Einstein"; final File file = new File(getTestDirectory(), "checksum-test.txt"); FileUtils.writeStringToFile(file, text, "US-ASCII"); // compute the expected checksum final Checksum expectedChecksum = new CRC32(); expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length()); final long expectedValue = expectedChecksum.getValue(); // compute the checksum of the file final Checksum testChecksum = new CRC32(); final Checksum resultChecksum = FileUtils.checksum(file, testChecksum); final long resultValue = resultChecksum.getValue(); assertSame(testChecksum, resultChecksum); assertEquals(expectedValue, resultValue); }