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();
}
}
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();
}
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();
}
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;
}
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);
}
Aggregations