use of java.util.zip.Checksum in project netty by netty.
the class FastLzFrameEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
final Checksum checksum = this.checksum;
for (; ; ) {
if (!in.isReadable()) {
return;
}
final int idx = in.readerIndex();
final int length = Math.min(in.readableBytes(), MAX_CHUNK_LENGTH);
final int outputIdx = out.writerIndex();
out.setMedium(outputIdx, MAGIC_NUMBER);
int outputOffset = outputIdx + CHECKSUM_OFFSET + (checksum != null ? 4 : 0);
final byte blockType;
final int chunkLength;
if (length < MIN_LENGTH_TO_COMPRESSION) {
blockType = BLOCK_TYPE_NON_COMPRESSED;
out.ensureWritable(outputOffset + 2 + length);
final byte[] output = out.array();
final int outputPtr = out.arrayOffset() + outputOffset + 2;
if (checksum != null) {
final byte[] input;
final int inputPtr;
if (in.hasArray()) {
input = in.array();
inputPtr = in.arrayOffset() + idx;
} else {
input = new byte[length];
in.getBytes(idx, input);
inputPtr = 0;
}
checksum.reset();
checksum.update(input, inputPtr, length);
out.setInt(outputIdx + CHECKSUM_OFFSET, (int) checksum.getValue());
System.arraycopy(input, inputPtr, output, outputPtr, length);
} else {
in.getBytes(idx, output, outputPtr, length);
}
chunkLength = length;
} else {
// try to compress
final byte[] input;
final int inputPtr;
if (in.hasArray()) {
input = in.array();
inputPtr = in.arrayOffset() + idx;
} else {
input = new byte[length];
in.getBytes(idx, input);
inputPtr = 0;
}
if (checksum != null) {
checksum.reset();
checksum.update(input, inputPtr, length);
out.setInt(outputIdx + CHECKSUM_OFFSET, (int) checksum.getValue());
}
final int maxOutputLength = calculateOutputBufferLength(length);
out.ensureWritable(outputOffset + 4 + maxOutputLength);
final byte[] output = out.array();
final int outputPtr = out.arrayOffset() + outputOffset + 4;
final int compressedLength = compress(input, inputPtr, length, output, outputPtr, level);
if (compressedLength < length) {
blockType = BLOCK_TYPE_COMPRESSED;
chunkLength = compressedLength;
out.setShort(outputOffset, chunkLength);
outputOffset += 2;
} else {
blockType = BLOCK_TYPE_NON_COMPRESSED;
System.arraycopy(input, inputPtr, output, outputPtr - 2, length);
chunkLength = length;
}
}
out.setShort(outputOffset, length);
out.setByte(outputIdx + OPTIONS_OFFSET, blockType | (checksum != null ? BLOCK_WITH_CHECKSUM : BLOCK_WITHOUT_CHECKSUM));
out.writerIndex(outputOffset + 2 + chunkLength);
in.skipBytes(length);
}
}
use of java.util.zip.Checksum in project DirectMemory by raffaeleguidi.
the class BaseTests method crc.
public long crc(String str) {
final Checksum checksum = new CRC32();
final byte[] bytes = str.getBytes();
checksum.update(bytes, 0, bytes.length);
return checksum.getValue();
}
use of java.util.zip.Checksum in project ratpack by ratpack.
the class Adler32Checksummer method apply.
/**
* Algorythm implementation.
*
* @param is {@code InputStream} of file to calculate checksum
* @return checksum string
*/
@Override
public String apply(InputStream is) throws Exception {
byte[] buffer = new byte[BUFFER_SIZE];
Checksum checksum = new Adler32();
int read = is.read(buffer, 0, BUFFER_SIZE);
while (read != -1) {
checksum.update(buffer, 0, read);
read = is.read(buffer, 0, BUFFER_SIZE);
}
return Long.toHexString(checksum.getValue());
}
use of java.util.zip.Checksum in project robolectric by robolectric.
the class ShadowBitmapFactory method decodeByteArray.
@Implementation
public static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
String desc = new String(data);
if (!Charset.forName("US-ASCII").newEncoder().canEncode(desc)) {
Checksum checksumEngine = new CRC32();
checksumEngine.update(data, 0, data.length);
desc = "byte array, checksum: " + checksumEngine.getValue();
}
if (offset != 0 || length != data.length) {
desc += " bytes " + offset + ".." + length;
}
return create(desc, opts);
}
use of java.util.zip.Checksum in project exhibitor by soabase.
the class ZooKeeperLogParser method parse.
public void parse(LogEntryReceiver receiver) throws Exception {
if (!validHeader) {
throw new Exception("Invalid magic number for");
}
while (true) {
long crcValue;
byte[] bytes;
try {
crcValue = logStream.readLong("crcvalue");
bytes = logStream.readBuffer("txnEntry");
} catch (EOFException e) {
break;
}
if (bytes.length == 0) {
// empty transaction
break;
}
Checksum crc = new Adler32();
crc.update(bytes, 0, bytes.length);
if (crcValue != crc.getValue()) {
throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue());
}
InputArchive iab = BinaryInputArchive.getArchive(new ByteArrayInputStream(bytes));
TxnHeader hdr = new TxnHeader();
Record record = useOldDeserializeMethod ? (Record) deserializeTxnMethod.invoke(null, iab, hdr) : (Record) deserializeTxnMethod.invoke(null, bytes, hdr);
if (logStream.readByte("EOR") != 'B') {
// partial transaction
break;
}
receiver.receiveEntry(hdr, record);
}
}
Aggregations