Search in sources :

Example 56 with Checksum

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());
}
Also used : Checksum(java.util.zip.Checksum) ByteBuffer(java.nio.ByteBuffer)

Example 57 with Checksum

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;
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum) Signature(android.content.pm.Signature) ActivityNotFoundException(android.content.ActivityNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 58 with Checksum

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;
    }
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 59 with Checksum

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;
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum)

Example 60 with Checksum

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();
}
Also used : CRC32(java.util.zip.CRC32) Checksum(java.util.zip.Checksum)

Aggregations

Checksum (java.util.zip.Checksum)84 CRC32 (java.util.zip.CRC32)29 IOException (java.io.IOException)16 ByteBuffer (java.nio.ByteBuffer)15 Adler32 (java.util.zip.Adler32)12 File (java.io.File)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 Path (java.nio.file.Path)6 EOFException (java.io.EOFException)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)5 StoreChannel (org.neo4j.io.fs.StoreChannel)5 CheckedInputStream (java.util.zip.CheckedInputStream)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 PureJavaCrc32 (org.apache.hadoop.util.PureJavaCrc32)3 BinaryInputArchive (org.apache.jute.BinaryInputArchive)3