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

The following examples show how to use java.util.zip.Adler32#update() . 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: 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 2
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 3
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 4
Source File: TestChecksumUtility.java    From database with GNU General Public License v2.0 6 votes vote down vote up
public void test_checksum04_direct() {
    
    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.allocateDirect(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 0,
            data.length));
    
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: Dex.java    From J2ME-Loader 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 14
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 15
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 16
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 17
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 18
Source File: BU.java    From mdict-java with GNU General Public License v3.0 4 votes vote down vote up
public static int calcChecksum(byte[] bytes) {
    Adler32 a32 = new Adler32();
    a32.update(bytes);
    int sum = (int) a32.getValue();
    return sum;
}
 
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();
}