Java Code Examples for java.util.zip.Adler32#getValue()

The following examples show how to use java.util.zip.Adler32#getValue() . 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: 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 (native heap buffer version).
 */
public void test_checksum05_direct() {
    
    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.allocateDirect(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 20,
            data.length-10));
    
}
 
Example 2
Source File: PackerUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 修改dex头,CheckSum 校验码
 * @param dexBytes
 */
private static void fixCheckSumHeader(byte[] dexBytes) {
    Adler32 adler = new Adler32();
    adler.update(dexBytes, 12, dexBytes.length - 12);//从12到文件末尾计算校验码
    long value = adler.getValue();
    int va = (int) value;
    byte[] newcs = intToByte(va);
    //高位在前,低位在前掉个个
    byte[] recs = new byte[4];
    for (int i = 0; i < 4; i++) {
        recs[i] = newcs[newcs.length - 1 - i];
        System.out.println(Integer.toHexString(newcs[i]));
    }
    System.arraycopy(recs, 0, dexBytes, 8, 4);//效验码赋值(8-11)
    System.out.println(Long.toHexString(value));
    System.out.println();
}
 
Example 3
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 4
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 5
Source File: Dex.java    From atlas 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 6
Source File: DexFile.java    From J2ME-Loader 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);
}
 
Example 7
Source File: DexFile.java    From javaide with GNU General Public License v3.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);
}
 
Example 8
Source File: Dex.java    From javaide with GNU General Public License v3.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 9
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);
}
 
Example 10
Source File: DexFile.java    From atlas 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);
}
 
Example 11
Source File: Dex.java    From Box 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 12
Source File: FileUtils.java    From update4j with Apache License 2.0 5 votes vote down vote up
public static long getChecksum(Path path) throws IOException {
    try (InputStream input = Files.newInputStream(path)) {
        Adler32 checksum = new Adler32();
        byte[] buf = new byte[1024 * 8];

        int read;
        while ((read = input.read(buf, 0, buf.length)) > -1)
            checksum.update(buf, 0, read);

        return checksum.getValue();
    }
}
 
Example 13
Source File: JobInfoScheduler.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getJobId(TransportContext transportContext) {
  Adler32 checksum = new Adler32();
  checksum.update(context.getPackageName().getBytes(Charset.forName("UTF-8")));
  checksum.update(transportContext.getBackendName().getBytes(Charset.forName("UTF-8")));
  checksum.update(
      ByteBuffer.allocate(4)
          .putInt(PriorityMapping.toInt(transportContext.getPriority()))
          .array());
  if (transportContext.getExtras() != null) {
    checksum.update(transportContext.getExtras());
  }
  return (int) checksum.getValue();
}
 
Example 14
Source File: ChecksumUtil.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
public static byte[] genAdler32ChecksumBytes(byte[] bytes) {
    Adler32 a32 = new Adler32();
    a32.update(bytes, 0, bytes.length);
    int sum = (int) a32.getValue();
    byte[] checksum = new byte[4];
    checksum[0] = (byte) (sum >> 24);
    checksum[1] = (byte) (sum >> 16);
    checksum[2] = (byte) (sum >> 8);
    checksum[3] = (byte) (sum);
    return checksum;
}
 
Example 15
Source File: TestChecksumUtility.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test verifies that only the specified region of the buffer is used to
 * compute the checksum.
 */
public void test_checksum02() {

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

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

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 10,
            data.length));
    
}
 
Example 16
Source File: ArrayUtil.java    From PE-HFT-Java with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] compressBytes(byte[] bytesArray) { 
	
	
	int decompressedLength = bytesArray.length;	
	int maxCompressedLength = Snappy.maxCompressedLength(decompressedLength);
	byte[] compressed = new byte[maxCompressedLength];
	int compressedLength = Snappy.compress(bytesArray, 0, decompressedLength, compressed, 0);
	byte[] truncated = Arrays.copyOfRange(compressed, 0, compressedLength );
	byte[] checkSumExt = Arrays.copyOf(truncated, truncated.length+8);
	
	Adler32 adler32 = new Adler32();
	adler32.update(truncated);
	long checkSum =adler32.getValue();
	ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
	byte[] checkSumBuf  =buffer.putLong(checkSum).array();
	
	checkSumExt[truncated.length] = checkSumBuf[0];
	checkSumExt[truncated.length+1] = checkSumBuf[1];
	checkSumExt[truncated.length+2] = checkSumBuf[2];
	checkSumExt[truncated.length+3] = checkSumBuf[3];
	checkSumExt[truncated.length+4] = checkSumBuf[4];
	checkSumExt[truncated.length+5] = checkSumBuf[5];
	checkSumExt[truncated.length+6] = checkSumBuf[6];
	checkSumExt[truncated.length+7] = checkSumBuf[7];
	
	
	return checkSumExt;
}
 
Example 17
Source File: DexFile.java    From Box 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
 * @param len length of {@code .dex} file encoded in the array
 */
private static void calcChecksum(byte[] bytes, int len) {
    Adler32 a32 = new Adler32();

    a32.update(bytes, 12, len - 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);
}
 
Example 18
Source File: SimpleLog.java    From jzab with Apache License 2.0 4 votes vote down vote up
/**
 * Goes to the next transaction record.
 *
 * @return the next transaction record
 * @throws java.io.EOFException if it reaches the end of file before reading
 *                              the entire transaction.
 * @throws IOException in case of IO failure
 * @throws NoSuchElementException
 * if there's no more elements to get
 */
@Override
public Transaction next() throws IOException {
  if(!hasNext()) {
    throw new NoSuchElementException();
  }
  DataInputStream in = new DataInputStream(logStream);
  if (in.available() < CHECKSUM_LENGTH + LENGTH_LENGTH) {
    LOG.error("Not enough bytes for checksum field and length field.");
    throw new RuntimeException("Corrupted file.");
  }
  // Gets the checksum value.
  int checksumValue = in.readInt();
  int length = in.readInt();
  long epoch, xid;
  int type;
  if (length < ZXID_LENGTH + TYPE_LENGTH) {
    LOG.error("The length field is invalid. Previous txn is {}", prevZxid);
    throw new RuntimeException("The length field is invalid.");
  }
  byte[] rest = new byte[length];
  in.readFully(rest, 0, length);
  byte[] blob = ByteBuffer.allocate(length + LENGTH_LENGTH).putInt(length)
                                                           .put(rest)
                                                           .array();
  // Caculates the checksum.
  Adler32 checksum = new Adler32();
  checksum.update(blob);
  if ((int)checksum.getValue() != checksumValue) {
    String exStr =
      String.format("Checksum after txn %s mismathes in file %s, file "
                    + "corrupted?",
                    prevZxid, logFile.getName());
    LOG.error(exStr);
    throw new RuntimeException(exStr);
  }
  // Checksum is correct, parse the byte array.
  ByteArrayInputStream bin = new ByteArrayInputStream(rest);
  DataInputStream din = new DataInputStream(bin);
  // Reads the Zxid.
  epoch = din.readLong();
  xid = din.readLong();
  // Reads the type of transaction.
  type = din.readInt();
  int payloadLength = length - ZXID_LENGTH - TYPE_LENGTH;
  byte[] payload = new byte[payloadLength];
  // Reads the data of the transaction body.
  din.readFully(payload, 0, payloadLength);
  din.close();
  Zxid zxid = new Zxid(epoch, xid);
  this.prevZxid = zxid;
  this.lastTransactionLength = CHECKSUM_LENGTH + LENGTH_LENGTH + length;
  // Updates the position of file.
  this.position += this.lastTransactionLength;
  return new Transaction(zxid, type, ByteBuffer.wrap(payload));
}
 
Example 19
Source File: ClassName.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
protected static long adler32(byte[] data)
{
    Adler32 adler = new Adler32();
    adler.update(data);
    return adler.getValue();
}
 
Example 20
Source File: AddlerHash.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public long hash(final byte[] value) {
  final Adler32 adler = new Adler32();
  adler.update(value);
  return adler.getValue();
}