use of java.util.zip.Checksum in project stdlib by petergeneric.
the class CRC32Digester method digest.
@Override
public String digest(ByteChannel channel) throws IOException {
Checksum crc = newChecksum();
ByteBuffer buffer = ByteBuffer.allocate(8192);
while (channel.read(buffer) >= 0) {
buffer.flip();
crc.update(buffer.array(), 0, buffer.limit());
buffer.clear();
}
return Long.toHexString(crc.getValue());
}
use of java.util.zip.Checksum in project xDrip by NightscoutFoundation.
the class JoH method isOldVersion.
public static boolean isOldVersion(Context context) {
try {
final Signature[] pinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
if (pinfo.length == 1) {
final Checksum s = new CRC32();
final byte[] ba = pinfo[0].toByteArray();
s.update(ba, 0, ba.length);
if (s.getValue() == 2009579833)
return true;
}
} catch (Exception e) {
Log.d(TAG, "exception: " + e);
}
return false;
}
use of java.util.zip.Checksum in project atomix by atomix.
the class JournalSegmentReader method readNext.
/**
* Reads the next entry in the segment.
*/
@SuppressWarnings("unchecked")
private void readNext() {
// Compute the index of the next entry in the segment.
final long index = getNextIndex();
Indexed cachedEntry = cache.get(index);
if (cachedEntry != null) {
this.nextEntry = cachedEntry;
buffer.skip(cachedEntry.size() + Bytes.INTEGER + Bytes.INTEGER);
return;
}
// Mark the buffer so it can be reset if necessary.
buffer.mark();
try {
// Read the length of the entry.
final int length = buffer.readInt();
// If the buffer length is zero then return.
if (length == 0) {
buffer.reset();
nextEntry = null;
return;
}
// Read the checksum of the entry.
long checksum = buffer.readUnsignedInt();
// Read the entry into memory.
buffer.read(memory.clear().limit(length));
memory.flip();
// Compute the checksum for the entry bytes.
final Checksum crc32 = new CRC32();
crc32.update(memory.array(), 0, length);
// If the stored checksum equals the computed checksum, return the entry.
if (checksum == crc32.getValue()) {
E entry = serializer.decode(memory.array());
nextEntry = new Indexed<>(index, entry, length);
} else {
buffer.reset();
nextEntry = null;
}
} catch (BufferUnderflowException e) {
buffer.reset();
nextEntry = null;
}
}
use of java.util.zip.Checksum in project atomix by atomix.
the class JournalSegmentWriter method append.
@Override
@SuppressWarnings("unchecked")
public <T extends E> Indexed<T> append(T entry) {
// Store the entry index.
final long index = getNextIndex();
// Serialize the entry.
final byte[] bytes = serializer.encode(entry);
final int length = bytes.length;
// Compute the checksum for the entry.
final Checksum crc32 = new CRC32();
crc32.update(bytes, 0, length);
final long checksum = crc32.getValue();
// Record the current buffer position;
int position = buffer.position();
// Write the entry length and entry to the segment.
buffer.writeInt(length).writeUnsignedInt(checksum).write(bytes);
// Update the last entry with the correct index/term/length.
Indexed<E> indexedEntry = new Indexed<>(index, entry, length);
this.lastEntry = indexedEntry;
this.cache.put(indexedEntry);
this.index.index(index, position);
return (Indexed<T>) indexedEntry;
}
use of java.util.zip.Checksum in project atomix by atomix.
the class JournalSegmentWriter method reset.
/**
* Initializes the writer by seeking to the end of the segment.
*/
@SuppressWarnings("unchecked")
private void reset(long index) {
long nextIndex = firstIndex;
// Clear the buffer indexes.
buffer.clear();
// Record the current buffer position.
int position = buffer.position();
// Read the entry length.
int length = buffer.mark().readInt();
// If the length is non-zero, read the entry.
while (length > 0 && (index == 0 || nextIndex <= index)) {
// Read the checksum of the entry.
final long checksum = buffer.readUnsignedInt();
// Read the entry into memory.
buffer.read(memory.clear().limit(length));
memory.flip();
// Compute the checksum for the entry bytes.
final Checksum crc32 = new CRC32();
crc32.update(memory.array(), 0, length);
// If the stored checksum equals the computed checksum, return the entry.
if (checksum == crc32.getValue()) {
final E entry = serializer.decode(memory.array());
lastEntry = new Indexed<>(nextIndex, entry, length);
this.index.index(nextIndex, position);
nextIndex++;
} else {
break;
}
// Read the next entry length.
position = buffer.position();
length = buffer.mark().readInt();
}
// Reset the buffer to the previous mark.
buffer.reset();
}
Aggregations