下面列出了java.util.zip.Adler32#update ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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));
}
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();
}
/**
* 修改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();
}
/**
* 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());
}
/**
* 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;
}
}
/**
* 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();
}
/**
* 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);
}
/**
* 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());
}
/**
* 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();
}
/**
* 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));
}
/**
* 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();
}
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();
}
}
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());
}
/**
* 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();
}
/**
* 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();
}
/**
* 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));
}
@Override
public long hash(final byte[] value) {
final Adler32 adler = new Adler32();
adler.update(value);
return adler.getValue();
}
protected static long adler32(byte[] data)
{
Adler32 adler = new Adler32();
adler.update(data);
return adler.getValue();
}
public static int calcChecksum(byte[] bytes) {
Adler32 a32 = new Adler32();
a32.update(bytes);
int sum = (int) a32.getValue();
return sum;
}