use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class LogHeaderReaderTest method shouldReadALogHeaderFromAByteChannel.
@Test
void shouldReadALogHeaderFromAByteChannel() throws IOException {
// given
final ByteBuffer buffer = ByteBuffers.allocate(CURRENT_FORMAT_LOG_HEADER_SIZE, INSTANCE);
final ReadableByteChannel channel = mock(ReadableByteChannel.class);
when(channel.read(buffer)).thenAnswer(new Answer<>() {
private int count;
@Override
public Integer answer(InvocationOnMock invocation) {
count++;
if (count == 1) {
buffer.putLong(encodeLogVersion(expectedLogVersion, CURRENT_LOG_FORMAT_VERSION));
return Long.BYTES;
}
if (count == 2) {
buffer.putLong(expectedTxId);
buffer.putLong(expectedStoreId.getCreationTime());
buffer.putLong(expectedStoreId.getRandomId());
buffer.putLong(expectedStoreId.getStoreVersion());
buffer.putLong(expectedStoreId.getUpgradeTime());
buffer.putLong(expectedStoreId.getUpgradeTxId());
// reserved
buffer.putLong(0);
return Long.BYTES * 7;
}
throw new AssertionError("Should only be called 3 times");
}
});
// when
final LogHeader result = readLogHeader(buffer, channel, true, null);
// then
assertEquals(new LogHeader(CURRENT_LOG_FORMAT_VERSION, expectedLogVersion, expectedTxId, expectedStoreId, CURRENT_FORMAT_LOG_HEADER_SIZE), result);
}
use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class LogHeaderReaderTest method shouldReadAnOldLogHeaderFromAByteChannel.
@Test
void shouldReadAnOldLogHeaderFromAByteChannel() throws IOException {
// given
final ByteBuffer buffer = ByteBuffers.allocate(CURRENT_FORMAT_LOG_HEADER_SIZE, INSTANCE);
final ReadableByteChannel channel = mock(ReadableByteChannel.class);
byte oldVersion = 6;
when(channel.read(buffer)).thenAnswer(new Answer<>() {
private int count;
@Override
public Integer answer(InvocationOnMock invocation) {
count++;
if (count == 1) {
buffer.putLong(encodeLogVersion(expectedLogVersion, oldVersion));
return Long.BYTES;
}
if (count == 2) {
buffer.putLong(expectedTxId);
return Long.BYTES;
}
throw new AssertionError("Should only be called twice");
}
});
// when
final LogHeader result = readLogHeader(buffer, channel, true, null);
// then
assertEquals(new LogHeader(oldVersion, expectedLogVersion, expectedTxId, LOG_HEADER_SIZE_3_5), result);
}
use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class LogHeaderReaderTest method shouldFailWhenUnableToReadALogHeaderFromAChannel.
@Test
void shouldFailWhenUnableToReadALogHeaderFromAChannel() throws IOException {
// given
final ByteBuffer buffer = ByteBuffers.allocate(CURRENT_FORMAT_LOG_HEADER_SIZE, INSTANCE);
final ReadableByteChannel channel = mock(ReadableByteChannel.class);
when(channel.read(buffer)).thenReturn(1);
assertThrows(IncompleteLogHeaderException.class, () -> readLogHeader(buffer, channel, true, null));
}
use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class PackStreamTest method handlesDataCrossingBufferBoundaries.
@Test
void handlesDataCrossingBufferBoundaries() throws Throwable {
// Given
Machine machine = new Machine();
PackStream.Packer packer = machine.packer();
packer.pack(Long.MAX_VALUE);
packer.pack(Long.MAX_VALUE);
packer.flush();
ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(machine.output()));
PackStream.Unpacker unpacker = new PackStream.Unpacker(new BufferedChannelInput(11).reset(ch));
// Serialized ch will look like, and misalign with the 11-byte unpack buffer:
// [XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX]
// mkr \___________data______________/ mkr \___________data______________/
// \____________unpack buffer_________________/
// When
long first = unpacker.unpackLong();
long second = unpacker.unpackLong();
// Then
assertEquals(Long.MAX_VALUE, first);
assertEquals(Long.MAX_VALUE, second);
}
use of java.nio.channels.ReadableByteChannel in project Lazy by l123456789jy.
the class NioFileUtiles method writeToFile1.
public static void writeToFile1(InputStream dataIns, File target) throws IOException {
FileOutputStream fo = null;
ReadableByteChannel src = null;
FileChannel out = null;
try {
int len = dataIns.available();
src = Channels.newChannel(dataIns);
fo = new FileOutputStream(target);
out = fo.getChannel();
out.transferFrom(src, 0, len);
} finally {
if (fo != null) {
fo.close();
}
if (src != null) {
src.close();
}
if (out != null) {
out.close();
}
}
}
Aggregations