java.util.zip.Adler32 Java Examples

The following examples show how to use java.util.zip.Adler32. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LZ4BlockOutputStream.java    From reladomo with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link java.io.OutputStream} with configurable block size. Large
 * blocks require more memory at compression and decompression time but
 * should improve the compression ratio.
 *
 * @param out        the {@link java.io.OutputStream} to feed
 * @param blockSize  the maximum number of bytes to try to compress at once,
 *                   must be >= 64 and <= 32 M
 * @param syncFlush  true if pending data should also be flushed on {@link #flush()}
 */
public LZ4BlockOutputStream(OutputStream out, int blockSize, boolean syncFlush)
{
    super(out);
    this.blockSize = blockSize;
    this.compressor = new LZ4HCJavaSafeCompressor();
    this.checksum = new Adler32();
    this.compressionLevel = compressionLevel(blockSize);
    this.buffer = new byte[blockSize];
    final int compressedBlockSize = HEADER_LENGTH + compressor.maxCompressedLength(blockSize);
    this.compressedBuffer = new byte[compressedBlockSize];
    this.syncFlush = syncFlush;
    o = 0;
    finished = false;
    System.arraycopy(MAGIC, 0, compressedBuffer, 0, MAGIC_LENGTH);
}
 
Example #2
Source File: m.java    From letv with Apache License 2.0 6 votes vote down vote up
private static int a(String str, int i) {
    if (TextUtils.isEmpty(str)) {
        z.b();
        return 0;
    }
    try {
        return Integer.valueOf(str).intValue();
    } catch (Exception e) {
        z.d();
        Adler32 adler32 = new Adler32();
        adler32.update(str.getBytes());
        int value = (int) adler32.getValue();
        if (value < 0) {
            value = Math.abs(value);
        }
        value += 13889152 * i;
        return value < 0 ? Math.abs(value) : value;
    }
}
 
Example #3
Source File: CrcUtils.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
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);
      }
    }
  }
  long crc = checksum.getValue();
  LOGGER.info("Computed crc = {}, based on files {}", crc, _files);
  return crc;
}
 
Example #4
Source File: Adler32Test.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Adler32#update(byte[])
 */
public void test_update$B() {
    // test method of java.util.zip.update(byte[])
    byte byteArray[] = { 1, 2 };
    Adler32 adl = new Adler32();
    adl.update(byteArray);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 393220
    assertEquals("update(byte[]) failed to update the checksum to the correct value ",
            393220, adl.getValue());

    adl.reset();
    byte byteEmpty[] = new byte[10000];
    adl.update(byteEmpty);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 655360001
    assertEquals("update(byte[]) failed to update the checksum to the correct value ",
            655360001L, adl.getValue());

}
 
Example #5
Source File: NetDummyMode.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * NET: Send replay data<br>
 * Game modes should implement this. However, some basic codes are already implemented in NetDummyMode.
 * @param engine GameEngine
 */
protected void netSendReplay(GameEngine engine) {
	if(netIsNetRankingSendOK(engine)) {
		NetSPRecord record = new NetSPRecord();
		record.setReplayProp(owner.replayProp);
		record.stats = new Statistics(engine.statistics);
		record.gameType = netGetGoalType();

		String strData = NetUtil.compressString(record.exportString());

		Adler32 checksumObj = new Adler32();
		checksumObj.update(NetUtil.stringToBytes(strData));
		long sChecksum = checksumObj.getValue();

		netLobby.netPlayerClient.send("spsend\t" + sChecksum + "\t" + strData + "\n");
	} else {
		netReplaySendStatus = 2;
	}
}
 
Example #6
Source File: ArrayUtil.java    From PE-HFT-Java with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] decompressBytes(byte[] bytesArray) throws ClientException  { 
	
	byte[] checkSumBuf = new byte[8];
	checkSumBuf[0] = bytesArray[bytesArray.length-8];
	checkSumBuf[1] = bytesArray[bytesArray.length-7];
	checkSumBuf[2] = bytesArray[bytesArray.length-6];
	checkSumBuf[3] = bytesArray[bytesArray.length-5];
	checkSumBuf[4] = bytesArray[bytesArray.length-4];
	checkSumBuf[5] = bytesArray[bytesArray.length-3];
	checkSumBuf[6] = bytesArray[bytesArray.length-2];
	checkSumBuf[7] = bytesArray[bytesArray.length-1];
	
	
	ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
    buffer.put(checkSumBuf);
    buffer.flip();//need flip 
    long checkSum = buffer.getLong();
    
    Adler32 adler32 = new Adler32();
	adler32.update(bytesArray, 0, bytesArray.length-8);
	if(checkSum !=adler32.getValue())
		throw new ClientException("Data corruption detected - checksum failure. Please, try again.");
    
	return Snappy.uncompress(bytesArray, 0, bytesArray.length -8 );
}
 
Example #7
Source File: DexWriter.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
private void updateChecksum(@Nonnull DexDataStore dataStore) throws IOException {
    Adler32 a32 = new Adler32();

    byte[] buffer = new byte[4 * 1024];
    InputStream input = dataStore.readAt(HeaderItem.CHECKSUM_DATA_START_OFFSET);
    int bytesRead = input.read(buffer);
    while (bytesRead >= 0) {
        a32.update(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
    }

    // write checksum, utilizing logic in DexWriter to write the integer value properly
    OutputStream output = dataStore.outputAt(HeaderItem.CHECKSUM_OFFSET);
    DexDataWriter.writeInt(output, (int)a32.getValue());
    output.close();
}
 
Example #8
Source File: DexFileWriter.java    From dexdiff with Apache License 2.0 6 votes vote down vote up
public static void updateChecksum(ByteBuffer buffer, int size) {
    byte[] data = buffer.array();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError();
    }

    digest.update(data, 32, size - 32);
    byte[] sha1 = digest.digest();
    System.arraycopy(sha1, 0, data, 12, sha1.length);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 12, size - 12);
    int v = (int) adler32.getValue();
    buffer.position(8);
    buffer.putInt(v);
}
 
Example #9
Source File: MnistDataFetcher.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private void validateFiles(String[] files, long[] checksums){
    //Validate files:
    try {
        for (int i = 0; i < files.length; i++) {
            File f = new File(files[i]);
            Checksum adler = new Adler32();
            long checksum = f.exists() ? FileUtils.checksum(f, adler).getValue() : -1;
            if (!f.exists() || checksum != checksums[i]) {
                throw new IllegalStateException("Failed checksum: expected " + checksums[i] +
                        ", got " + checksum + " for file: " + f);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: ReplicationHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public DirectoryFileStream(SolrParams solrParams) {
  params = solrParams;
  delPolicy = core.getDeletionPolicy();

  fileName = validateFilenameOrError(params.get(FILE));
  cfileName = validateFilenameOrError(params.get(CONF_FILE_SHORT));
  tlogFileName = validateFilenameOrError(params.get(TLOG_FILE));
  
  sOffset = params.get(OFFSET);
  sLen = params.get(LEN);
  compress = params.get(COMPRESSION);
  useChecksum = params.getBool(CHECKSUM, false);
  indexGen = params.getLong(GENERATION);
  if (useChecksum) {
    checksum = new Adler32();
  }
  //No throttle if MAX_WRITE_PER_SECOND is not specified
  double maxWriteMBPerSec = params.getDouble(MAX_WRITE_PER_SECOND, Double.MAX_VALUE);
  rateLimiter = new RateLimiter.SimpleRateLimiter(maxWriteMBPerSec);
}
 
Example #11
Source File: Adler32Test.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Adler32#update(ByteBuffer)
 */
public void test_update$ByteBuffer() {
    // test methods of java.util.zip.update(ByteBuffer)
    // Heap ByteBuffer
    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] {1,2,3,4});
    byteBuffer.position(2);
    assertChecksumFromByteBuffer(0xc0008, byteBuffer);

    // Direct ByteBuffer
    byteBuffer.flip();
    byteBuffer = ByteBuffer.allocateDirect(4).put(byteBuffer);
    byteBuffer.flip();
    byteBuffer.position(2);
    assertChecksumFromByteBuffer(0xc0008, byteBuffer);

    Adler32 checksum = new Adler32();
    try {            
        checksum.update((ByteBuffer)null);
        fail();
    } catch (NullPointerException expected) {}
}
 
Example #12
Source File: TestChecksumUtility.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that the computed checksum is the same whether the buffer is
 * backed by an array or not.
 */
public void test_checksum04() {
    
    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 0,
            data.length));
    
    ByteBuffer direct = ByteBuffer.allocate(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 0,
            data.length));
    
}
 
Example #13
Source File: TestChecksumUtility.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that the computed checksum is the same whether the buffer is
 * backed by an array or not when the checksum is computed for only a region
 * of the buffer (java heap buffer version).
 */
public void test_checksum05() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 20, 100 - 10 - 20);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 20,
            data.length - 10));

    ByteBuffer direct = ByteBuffer.allocate(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 20,
            data.length - 10));

}
 
Example #14
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private void updateChecksum(@Nonnull DexDataStore dataStore) throws IOException {
    Adler32 a32 = new Adler32();

    byte[] buffer = new byte[4 * 1024];
    InputStream input = dataStore.readAt(HeaderItem.CHECKSUM_DATA_START_OFFSET);
    int bytesRead = input.read(buffer);
    while (bytesRead >= 0) {
        a32.update(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
    }

    // write checksum, utilizing logic in DexWriter to write the integer value properly
    OutputStream output = dataStore.outputAt(HeaderItem.CHECKSUM_OFFSET);
    DexDataWriter.writeInt(output, (int)a32.getValue());
    output.close();
}
 
Example #15
Source File: Checksums.java    From tchannel-java with MIT License 6 votes vote down vote up
public static long calculateChecksum(CallFrame msg, long digestSeed) {

        // TODO: this is bad
        ByteBuf payloadCopy = msg.getPayload().slice();
        byte[] payloadBytes = new byte[msg.getPayloadSize()];
        payloadCopy.readBytes(payloadBytes);

        switch (msg.getChecksumType()) {

            case Adler32:
                Adler32 f = new Adler32();
                f.update((int) digestSeed);
                f.update(payloadBytes);
                return f.getValue();
            case FarmhashFingerPrint32:
            case NoChecksum:
            case CRC32C:
            default:
                return 0;
        }

    }
 
Example #16
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private void updateChecksum(@Nonnull DexDataStore dataStore) throws IOException {
    Adler32 a32 = new Adler32();

    byte[] buffer = new byte[4 * 1024];
    InputStream input = dataStore.readAt(HeaderItem.CHECKSUM_DATA_START_OFFSET);
    int bytesRead = input.read(buffer);
    while (bytesRead >= 0) {
        a32.update(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
    }

    // write checksum, utilizing logic in DexWriter to write the integer value properly
    OutputStream output = dataStore.outputAt(HeaderItem.CHECKSUM_OFFSET);
    DexDataWriter.writeInt(output, (int)a32.getValue());
    output.close();
}
 
Example #17
Source File: Adler32Test.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Adler32#update(int)
 */
public void test_updateI() {
    // test methods of java.util.zip.update(int)
    Adler32 adl = new Adler32();
    adl.update(1);
    // The value of the adl should be 131074
    assertEquals("update(int) failed to update the checksum to the correct value ",
            131074, adl.getValue());

    adl.reset();
    adl.update(Integer.MAX_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 16777472
    assertEquals("update(max) failed to update the checksum to the correct value ",
            16777472L, adl.getValue());

    adl.reset();
    adl.update(Integer.MIN_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 65537
    assertEquals("update(min) failed to update the checksum to the correct value ",
            65537L, adl.getValue());

}
 
Example #18
Source File: InflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Inflater#getAdler()
 */
public void test_getAdler() {
    // test method of java.util.zip.inflater.getAdler()
    byte dictionaryArray[] = { 'e', 'r', 't', 'a', 'b', 2, 3 };

    Inflater inflateDiction = new Inflater();
    inflateDiction.setInput(outPutDiction);
    if (inflateDiction.needsDictionary() == true) {
        // getting the checkSum value through the Adler32 class
        Adler32 adl = new Adler32();
        adl.update(dictionaryArray);
        long checkSumR = adl.getValue();
        assertEquals(
                "the checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance",
                inflateDiction.getAdler(), checkSumR);
    }
    inflateDiction.end();
}
 
Example #19
Source File: EntryEncodingUtil.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
/**
 * Decode a message from the passed input stream, and compute and verify its CRC. This method reads
 * data written by the method {@link EntryEncodingUtil#encodeWithLengthAndCrc}.
 *
 * @param inputStream Input stream, opened for reading and positioned just before the length-prepended header
 * @return The deserialized, constructed, validated message
 * @throws IOException                if a problem is encountered while reading or parsing
 * @throws EntryEncodingUtil.CrcError if the recorded CRC of the message does not match its computed CRC.
 */
public static <T> T decodeAndCheckCrc(InputStream inputStream, Schema<T> schema)
    throws IOException, CrcError {
  // TODO this should check the length first and compare it with a passed-in maximum length
  final T message = schema.newMessage();
  final CrcInputStream crcStream = new CrcInputStream(inputStream, new Adler32());
  ProtobufIOUtil.mergeDelimitedFrom(crcStream, message, schema);

  final long computedCrc = crcStream.getValue();
  final long diskCrc = readCrc(inputStream);
  if (diskCrc != computedCrc) {
    throw new CrcError("CRC mismatch on deserialized message " + message.toString());
  }

  return message;
}
 
Example #20
Source File: Adler32Test.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Adler32#getValue()
 */
public void test_getValue() {
    // test methods of java.util.zip.getValue()
    Adler32 adl = new Adler32();
    assertEquals("GetValue should return a zero as a result of construction an object of Adler32",
            1, adl.getValue());

    adl.reset();
    adl.update(1);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 131074
    assertEquals("update(int) failed to update the checksum to the correct value ",
            131074, adl.getValue());
    adl.reset();
    assertEquals("reset failed to reset the checksum value to zero", 1, adl
            .getValue());

    adl.reset();
    adl.update(Integer.MIN_VALUE);
    // System.out.print("value of adl " + adl.getValue());
    // The value of the adl should be 65537
    assertEquals("update(min) failed to update the checksum to the correct value ",
            65537L, adl.getValue());
}
 
Example #21
Source File: Dex.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the checksum of all but the first 12 bytes of {@code dex}.
 */
public int computeChecksum() throws IOException {
    Adler32 adler32 = new Adler32();
    byte[] buffer = new byte[8192];
    ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
    data.limit(data.capacity());
    data.position(CHECKSUM_OFFSET + CHECKSUM_SIZE);
    while (data.hasRemaining()) {
        int count = Math.min(buffer.length, data.remaining());
        data.get(buffer, 0, count);
        adler32.update(buffer, 0, count);
    }
    return (int) adler32.getValue();
}
 
Example #22
Source File: TestChecksumUtility.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test verifies that the checksum of the buffer is being computed
 * correctly.
 */
public void test_checksum01() {
    
    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 0,
            data.length));

}
 
Example #23
Source File: PerformanceTest.java    From j2ssh-maverick with GNU Lesser General Public License v3.0 5 votes vote down vote up
static void createTestFile() throws Throwable {

		/**
		 * Generate a temporary file for uploading/downloading
		 */
		sourceFile = new File(System.getProperty("user.home"), "sftp-file");
		java.util.Random rnd = new java.util.Random();

		FileOutputStream out = new FileOutputStream(sourceFile);
		byte[] buf = new byte[1024000];
		for (int i = 0; i < 100; i++) {
			rnd.nextBytes(buf);
			out.write(buf);
		}
		out.close();

		CheckedInputStream cis = new CheckedInputStream(new FileInputStream(
				sourceFile), new Adler32());

		try {
			byte[] tempBuf = new byte[16384];
			while (cis.read(tempBuf) >= 0) {
			}
			sourceFileChecksum = cis.getChecksum().getValue();
		} catch (IOException e) {
		} finally {
			cis.close();
		}
	}
 
Example #24
Source File: Adler32Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void assertChecksumFromByteBuffer(long expectedChecksum, ByteBuffer byteBuffer) {
    Adler32 checksum = new Adler32();
    checksum.update(byteBuffer);
    assertEquals("update(ByteBuffer) failed to update the checksum to the correct value ",
                 expectedChecksum, checksum.getValue());
    assertEquals(0, byteBuffer.remaining());
}
 
Example #25
Source File: Adler32Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.zip.Adler32#reset()
 */
public void test_reset() {
    // test methods of java.util.zip.reset()
    Adler32 adl = new Adler32();
    adl.update(1);
    // System.out.print("value of adl"+adl.getValue());
    // The value of the adl should be 131074
    assertEquals("update(int) failed to update the checksum to the correct value ",
            131074, adl.getValue());
    adl.reset();
    assertEquals("reset failed to reset the checksum value to zero", 1, adl
            .getValue());
}
 
Example #26
Source File: OldAndroidChecksumTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void wrongChecksumWithAdler32Test() {
    byte[] bytes = {1, 0, 5, 0, 15, 0, 1, 11, 0, 1};
    Adler32 adler = new Adler32();
    adler.update(bytes);
    long arrayChecksum = adler.getValue();
    adler.reset();
    for (int i = 0; i < bytes.length; i++) {
        adler.update(bytes[i]);
    }
    assertEquals("Checksums not equal: expected: " + arrayChecksum +
            " actual: " + adler.getValue(), arrayChecksum, adler.getValue());
}
 
Example #27
Source File: GenericContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
@VisibleForTesting
Checksum hashCopiedFiles() {
    Checksum checksum = new Adler32();
    copyToFileContainerPathMap.entrySet().stream().sorted(Entry.comparingByValue()).forEach(entry -> {
        byte[] pathBytes = entry.getValue().getBytes();
        // Add path to the hash
        checksum.update(pathBytes, 0, pathBytes.length);

        File file = new File(entry.getKey().getResolvedPath());
        checksumFile(file, checksum);
    });
    return checksum;
}
 
Example #28
Source File: CaptureTile.java    From Dayon with GNU General Public License v3.0 5 votes vote down vote up
public static long computeChecksum(byte[] data, int offset, int len) {
	final Checksum checksum = new Adler32();
	// final Checksum checksum = new CRC32(); -- more CPU - Adler32 seems
	// quite good until now ...
	checksum.update(data, offset, len);
	return checksum.getValue();
}
 
Example #29
Source File: Dex.java    From aapt with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the checksum of all but the first 12 bytes of {@code dex}.
 */
public int computeChecksum() throws IOException {
    Adler32 adler32 = new Adler32();
    byte[] buffer = new byte[8192];
    ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
    data.limit(data.capacity());
    data.position(CHECKSUM_OFFSET + CHECKSUM_SIZE);
    while (data.hasRemaining()) {
        int count = Math.min(buffer.length, data.remaining());
        data.get(buffer, 0, count);
        adler32.update(buffer, 0, count);
    }
    return (int) adler32.getValue();
}
 
Example #30
Source File: DexFile.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the checksum for the {@code .dex} file in the
 * given array, and modify the array to contain it.
 *
 * @param bytes {@code non-null;} the bytes of the file
 */
private static void calcChecksum(byte[] bytes) {
    Adler32 a32 = new Adler32();

    a32.update(bytes, 12, bytes.length - 12);

    int sum = (int) a32.getValue();

    bytes[8]  = (byte) sum;
    bytes[9]  = (byte) (sum >> 8);
    bytes[10] = (byte) (sum >> 16);
    bytes[11] = (byte) (sum >> 24);
}