use of java.util.zip.Checksum in project netty by netty.
the class Lz4FrameEncoderTest method newEncoder.
private Lz4FrameEncoder newEncoder(int blockSize, int maxEncodeSize) {
Checksum checksum = XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum();
Lz4FrameEncoder encoder = new Lz4FrameEncoder(LZ4Factory.fastestInstance(), true, blockSize, checksum, maxEncodeSize);
encoder.handlerAdded(ctx);
return encoder;
}
use of java.util.zip.Checksum in project neo4j by neo4j.
the class ReadAheadChannelTest method throwOnInvalidChecksum.
@ParameterizedTest
@EnumSource(Constructors.class)
void throwOnInvalidChecksum(Constructor constructor) throws Exception {
// given
Checksum checksum = CHECKSUM_FACTORY.get();
Path file = Path.of("foo.1");
try (StoreChannel storeChannel = fileSystem.write(file)) {
ByteBuffer buffer = ByteBuffers.allocate(6, INSTANCE);
buffer.put((byte) 1);
checksum.update(1);
buffer.put((byte) 2);
checksum.update(2);
int notChecksumValue = (int) checksum.getValue() + 1;
buffer.putInt(notChecksumValue);
buffer.flip();
storeChannel.writeAll(buffer);
storeChannel.force(false);
}
ReadAheadChannel<StoreChannel> bufferedReader = constructor.apply(fileSystem.read(file), DEFAULT_READ_AHEAD_SIZE);
assertEquals(1, bufferedReader.get());
assertEquals(2, bufferedReader.get());
assertThrows(ChecksumMismatchException.class, bufferedReader::endChecksumAndValidate);
}
use of java.util.zip.Checksum in project neo4j by neo4j.
the class PhysicalFlushableChecksumChannelTest method beginCehcksumShouldResetCalculations.
@Test
void beginCehcksumShouldResetCalculations() throws IOException {
final Path firstFile = directory.homePath().resolve("file1");
StoreChannel storeChannel = fileSystem.write(firstFile);
int channelChecksum;
try (PhysicalFlushableChecksumChannel channel = new PhysicalFlushableChecksumChannel(storeChannel, new HeapScopedBuffer(100, INSTANCE))) {
channel.put((byte) 5);
channel.beginChecksum();
channel.put((byte) 10);
channelChecksum = channel.putChecksum();
}
int fileSize = (int) fileSystem.getFileSize(firstFile);
assertEquals(Byte.BYTES + Byte.BYTES + Integer.BYTES, fileSize);
byte[] writtenBytes = new byte[fileSize];
try (InputStream in = Files.newInputStream(firstFile)) {
in.read(writtenBytes);
}
ByteBuffer buffer = ByteBuffer.wrap(writtenBytes);
Checksum checksum = CHECKSUM_FACTORY.get();
checksum.update(10);
assertEquals(checksum.getValue(), channelChecksum);
assertEquals(5, buffer.get());
assertEquals(10, buffer.get());
assertEquals(checksum.getValue(), buffer.getInt());
}
use of java.util.zip.Checksum in project neo4j by neo4j.
the class PhysicalFlushableChecksumChannelTest method calculateChecksum.
@Test
void calculateChecksum() throws IOException {
final Path firstFile = directory.homePath().resolve("file1");
StoreChannel storeChannel = fileSystem.write(firstFile);
int channelChecksum;
try (PhysicalFlushableChecksumChannel channel = new PhysicalFlushableChecksumChannel(storeChannel, new HeapScopedBuffer(100, INSTANCE))) {
channel.beginChecksum();
channel.put((byte) 10);
channelChecksum = channel.putChecksum();
}
int fileSize = (int) fileSystem.getFileSize(firstFile);
assertEquals(Byte.BYTES + Integer.BYTES, fileSize);
byte[] writtenBytes = new byte[fileSize];
try (InputStream in = Files.newInputStream(firstFile)) {
in.read(writtenBytes);
}
ByteBuffer buffer = ByteBuffer.wrap(writtenBytes);
Checksum checksum = CHECKSUM_FACTORY.get();
checksum.update(10);
assertEquals(checksum.getValue(), channelChecksum);
assertEquals(10, buffer.get());
assertEquals(checksum.getValue(), buffer.getInt());
}
Aggregations