下面列出了怎么用java.util.zip.Adler32的API类实例代码及写法,或者点击链接到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();
}
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();
}
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);
}
}
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;
}
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;
}
}
/**
* 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);
}
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);
}
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);
}
/**
* 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;
}
}
/**
* 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());
}
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 );
}
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();
}
/**
* 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) {}
}
/**
* 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));
}
/**
* 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());
}
/**
* 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));
}
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;
}
}
/**
* 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();
}
/**
* 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;
}
/**
* 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());
}
/**
* 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();
}
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());
}
private Long calculCheckSum( Object[] r ) throws Exception {
Long retval;
byte[] byteArray;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Loop through fields
for ( int i = 0; i < data.fieldnr; i++ ) {
if ( getInputRowMeta().getValueMeta( data.fieldnrs[ i ] ).isBinary() ) {
baos.write( getInputRowMeta().getBinary( r, data.fieldnrs[ i ] ) );
} else {
baos.write( getInputRowMeta().getValueMeta( data.fieldnrs[ i ] ).getNativeDataType( r[ data.fieldnrs[ i ] ] )
.toString().getBytes() );
}
}
byteArray = baos.toByteArray();
if ( meta.getCheckSumType().equals( CheckSumMeta.TYPE_CRC32 ) ) {
CRC32 crc32 = new CRC32();
crc32.update( byteArray );
retval = new Long( crc32.getValue() );
} else {
Adler32 adler32 = new Adler32();
adler32.update( byteArray );
retval = new Long( adler32.getValue() );
}
return retval;
}
/**
* Computes the checksum with the given options.
*
* @param memory The memory to generate the checksum from.
* @param addrSet The addresses over which to generate the checksum.
* @param monitor Cancelable task monitor to cancel the computation.
* @param onesComp True if the checksum should be complemented with a ones complement.
* @param twosComp True if the checksum should be complemented with a twos complement.
* @throws MemoryAccessException If there was a problem accessing the specified memory.
* @throws CancelledException If checksum generation was cancelled.
*/
public void updateChecksum(Memory memory, AddressSetView addrSet, TaskMonitor monitor,
boolean onesComp, boolean twosComp) throws MemoryAccessException, CancelledException {
byte[] bytes = new byte[1024];
long sum;
try (CheckedInputStream cis =
new CheckedInputStream(new MemoryInputStream(memory, addrSet), new Adler32())) {
while (cis.read(bytes) > 0) {
if (monitor.isCancelled()) {
throw new CancelledException();
}
}
sum = cis.getChecksum().getValue();
}
catch (IOException e) {
throw new MemoryAccessException(e.getMessage());
}
if (onesComp) {
sum = ~sum;
}
else if (twosComp) {
sum = -sum;
}
checksum = toArray(sum, 4);
}
protected ChecksumGenerator createChecksumGenerator() throws NoSuchAlgorithmException {
if (CHECKSUM_MD5.equals(getType())) {
return new MessageDigestChecksumGenerator(getType());
} else if (CHECKSUM_CRC32.equals(getType())) {
return new ZipChecksumGenerator(new CRC32());
} else if (CHECKSUM_ADLER32.equals(getType())) {
return new ZipChecksumGenerator(new Adler32());
} else if (CHECKSUM_SHA.equals(getType())) {
return new MessageDigestChecksumGenerator(getType());
} else {
throw new NoSuchAlgorithmException("unsupported algorithm ["+getType()+"]");
}
}
static ByteBufChecksum wrapChecksum(Checksum checksum) {
ObjectUtil.checkNotNull(checksum, "checksum");
if (checksum instanceof Adler32 && ADLER32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, ADLER32_UPDATE_METHOD);
}
if (checksum instanceof CRC32 && CRC32_UPDATE_METHOD != null) {
return new ReflectiveByteBufChecksum(checksum, CRC32_UPDATE_METHOD);
}
return new SlowByteBufChecksum(checksum);
}
private void adler32Test(byte[] values, long expected) {
Adler32 adler = new Adler32();
// try it all at once
adler.update(values);
assertEquals(adler.getValue(), expected);
// try resetting and computing one byte at a time
adler.reset();
for (int i = 0; i < values.length; i++) {
adler.update(values[i]);
}
assertEquals(adler.getValue(), expected);
}
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;
}
public static String calculateChecksum(final Path filePath) throws IOException {
try (final FileChannel fileChannel = FileChannel.open(filePath)) {
int bytesStart;
int bytesPerChecksum = 10 * 1024 * 1024;
// Get last 10 megabytes of file to use for checksum
if (fileChannel.size() >= bytesPerChecksum) {
bytesStart = toIntExact(fileChannel.size()) - bytesPerChecksum;
} else {
bytesStart = 0;
bytesPerChecksum = (int) fileChannel.size();
}
fileChannel.position(bytesStart);
final ByteBuffer bytesToChecksum = ByteBuffer.allocate(bytesPerChecksum);
int bytesRead = fileChannel.read(bytesToChecksum, bytesStart);
assert (bytesRead == bytesPerChecksum);
// Adler32 because it's faster than SHA / MD5 and Cassandra uses it - https://issues.apache.org/jira/browse/CASSANDRA-5862
final Adler32 adler32 = new Adler32();
adler32.update(bytesToChecksum.array());
return String.valueOf(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();
}