use of java.util.zip.Adler32 in project atlas by alibaba.
the class DexFile method calcChecksum.
/**
* 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);
}
use of java.util.zip.Adler32 in project atlas by alibaba.
the class Dex method computeChecksum.
/**
* 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];
// positioned ByteBuffers aren't thread safe
ByteBuffer data = this.data.duplicate();
data.limit(data.capacity());
data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM);
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();
}
use of java.util.zip.Adler32 in project dex2jar by pxb1988.
the class DexFileWriter method updateChecksum.
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);
}
use of java.util.zip.Adler32 in project ratpack by ratpack.
the class Adler32Checksummer method apply.
/**
* Algorythm implementation.
*
* @param is {@code InputStream} of file to calculate checksum
* @return checksum string
*/
@Override
public String apply(InputStream is) throws Exception {
byte[] buffer = new byte[BUFFER_SIZE];
Checksum checksum = new Adler32();
int read = is.read(buffer, 0, BUFFER_SIZE);
while (read != -1) {
checksum.update(buffer, 0, read);
read = is.read(buffer, 0, BUFFER_SIZE);
}
return Long.toHexString(checksum.getValue());
}
use of java.util.zip.Adler32 in project exhibitor by soabase.
the class ZooKeeperLogParser method parse.
public void parse(LogEntryReceiver receiver) throws Exception {
if (!validHeader) {
throw new Exception("Invalid magic number for");
}
while (true) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
break;
}
if (bytes.length == 0) {
// empty transaction
break;
}
Checksum crc = new Adler32();
crc.update(bytes, 0, bytes.length);
if (crcValue != crc.getValue()) {
throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
}
InputArchive iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
TxnHeader hdr = new TxnHeader();
Record record = useOldDeserializeMethod ? (Record) deserializeTxnMethod.invoke(null, iab, hdr) : (Record) deserializeTxnMethod.invoke(null, bytes, hdr);
if (logStream.readByte("EOR") != 'B') {
// partial transaction
break;
}
receiver.receiveEntry(hdr, record);
}
}
Aggregations