Search in sources :

Example 81 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteBuffer(java.nio.ByteBuffer) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) Test(org.junit.jupiter.api.Test)

Example 82 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ByteBuffer(java.nio.ByteBuffer) LogHeaderReader.readLogHeader(org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader) Test(org.junit.jupiter.api.Test)

Example 83 with ReadableByteChannel

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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 84 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 85 with ReadableByteChannel

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();
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11