Search in sources :

Example 16 with Adler32

use of java.util.zip.Adler32 in project voltdb by VoltDB.

the class FileSnap method serialize.

/**
     * serialize the datatree and session into the file snapshot
     * @param dt the datatree to be serialized
     * @param sessions the sessions to be serialized
     * @param snapShot the file to store snapshot into
     */
@Override
public synchronized void serialize(DataTree dt, Map<Long, Long> sessions, File snapShot) throws IOException {
    if (!close) {
        OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
        CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32());
        //CheckedOutputStream cout = new CheckedOutputStream()
        OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
        FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
        serialize(dt, sessions, oa, header);
        long val = crcOut.getChecksum().getValue();
        oa.writeLong(val, "val");
        oa.writeString("/", "path");
        sessOS.flush();
        crcOut.close();
        sessOS.close();
    }
}
Also used : BinaryOutputArchive(org.apache.jute_voltpatches.BinaryOutputArchive) OutputArchive(org.apache.jute_voltpatches.OutputArchive) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) FileOutputStream(java.io.FileOutputStream) CheckedOutputStream(java.util.zip.CheckedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileHeader(org.apache.zookeeper_voltpatches.server.persistence.FileHeader) Adler32(java.util.zip.Adler32)

Example 17 with Adler32

use of java.util.zip.Adler32 in project jackson-module-afterburner by FasterXML.

the class ClassName method adler32.

protected static long adler32(byte[] data) {
    Adler32 adler = new Adler32();
    adler.update(data);
    return adler.getValue();
}
Also used : Adler32(java.util.zip.Adler32)

Example 18 with Adler32

use of java.util.zip.Adler32 in project smali by JesusFreke.

the class DexWriter method updateChecksum.

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();
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MemoryDeferredOutputStream(org.jf.dexlib2.writer.io.MemoryDeferredOutputStream) DeferredOutputStream(org.jf.dexlib2.writer.io.DeferredOutputStream) OutputStream(java.io.OutputStream) Adler32(java.util.zip.Adler32)

Example 19 with Adler32

use of java.util.zip.Adler32 in project jena by apache.

the class Journal method write.

public synchronized long write(JournalEntryType type, FileRef fileRef, Block block) {
    //log.info("@"+position()+" -- "+type+","+fileRef+", "+buffer+", "+block) ;
    ByteBuffer buffer = (block == null) ? null : block.getByteBuffer();
    long posn = position;
    int bufferCapacity = 0;
    int len = 0;
    if (buffer != null) {
        bufferCapacity = buffer.capacity();
        len = buffer.remaining();
    }
    header.clear();
    header.putInt(type.id);
    //header.putInt(len) ;
    // Write whole buffer.
    header.putInt(bufferCapacity);
    header.putInt(fileRef.getId());
    int blkId = (block == null) ? NoId : block.getId().intValue();
    header.putInt(blkId);
    header.flip();
    channel.write(header);
    Adler32 adler = new Adler32();
    adler.update(header.array());
    if (len > 0) {
        // Make buffer include it's full length.
        // [TxDEV:TODO] This is the full buffer, junk and all.
        // This makes the system able to check block sizes (BlockAccess checking).
        int bufferLimit = buffer.limit();
        int bufferPosition = buffer.position();
        buffer.position(0);
        buffer.limit(bufferCapacity);
        // Clear top.
        for (int i = len; i < bufferCapacity; i++) buffer.put(i, (byte) 0);
        // Write all bytes
        channel.write(buffer);
        if (buffer.hasArray()) {
            adler.update(buffer.array());
        } else {
            byte[] data = new byte[bufferCapacity];
            buffer.position(0);
            buffer.limit(bufferCapacity);
            buffer.get(data);
            adler.update(data);
        }
        buffer.position(bufferPosition);
        buffer.limit(bufferLimit);
    }
    // checksum
    crcTrailer.clear();
    Bytes.setInt((int) adler.getValue(), crcTrailer.array());
    channel.write(crcTrailer);
    // header + payload + checksum
    position += Overhead + len + SizeofCRC;
    return posn;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Adler32(java.util.zip.Adler32)

Example 20 with Adler32

use of java.util.zip.Adler32 in project jena by apache.

the class Journal method _read.

// read one entry at the channel position.
// Move position to end of read.
private JournalEntry _read() {
    header.clear();
    int lenRead = channel.read(header);
    if (lenRead == -1) {
        // probably broken file.
        throw new TDBTransactionException("Read off the end of a journal file");
    //return null ;
    }
    header.rewind();
    int typeId = header.getInt();
    int len = header.getInt();
    int ref = header.getInt();
    int blockId = header.getInt();
    Adler32 adler = new Adler32();
    adler.update(header.array());
    ByteBuffer bb = ByteBuffer.allocate(len);
    lenRead = channel.read(bb);
    if (lenRead != len)
        throw new TDBTransactionException("Failed to read the journal entry: wanted " + len + " bytes, got " + lenRead);
    adler.update(bb.array());
    bb.rewind();
    // checksum
    crcTrailer.clear();
    lenRead = channel.read(crcTrailer);
    if (lenRead != SizeofCRC)
        throw new TDBTransactionException("Failed to read block checksum (got " + lenRead + " bytes, not " + SizeofCRC + ").");
    int checksum = Bytes.getInt(crcTrailer.array());
    if (checksum != (int) adler.getValue())
        throw new TDBTransactionException("Checksum error reading from the Journal.");
    JournalEntryType type = JournalEntryType.type(typeId);
    FileRef fileRef = FileRef.get(ref);
    Block block = new Block(blockId, bb);
    return new JournalEntry(type, fileRef, block);
}
Also used : FileRef(org.apache.jena.tdb.sys.FileRef) Block(org.apache.jena.tdb.base.block.Block) ByteBuffer(java.nio.ByteBuffer) Adler32(java.util.zip.Adler32)

Aggregations

Adler32 (java.util.zip.Adler32)41 IOException (java.io.IOException)11 Checksum (java.util.zip.Checksum)9 ByteBuffer (java.nio.ByteBuffer)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)6 CheckedInputStream (java.util.zip.CheckedInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 BinaryInputArchive (org.apache.jute.BinaryInputArchive)5 InputArchive (org.apache.jute.InputArchive)4 EOFException (java.io.EOFException)3 OutputStream (java.io.OutputStream)3 CheckedOutputStream (java.util.zip.CheckedOutputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2