Search in sources :

Example 41 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project neo4j by neo4j.

the class PageCacheTest method writableByteChannelMustWriteAllBytesInFile.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writableByteChannelMustWriteAllBytesInFile() throws Exception {
    File file = file("a");
    configureStandardPageCache();
    try (PagedFile pf = pageCache.map(file, filePageSize)) {
        try (WritableByteChannel channel = pf.openWritableByteChannel()) {
            generateFileWithRecords(channel, recordCount, recordSize);
        }
        try (ReadableByteChannel channel = pf.openReadableByteChannel()) {
            verifyRecordsInFile(channel, recordCount);
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) WritableByteChannel(java.nio.channels.WritableByteChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) Test(org.junit.Test)

Example 42 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project druid by druid-io.

the class CompressedIndexedIntsBenchmark method serialize.

private static ByteBuffer serialize(WritableSupplier<IndexedInts> writableSupplier) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect((int) writableSupplier.getSerializedSize());
    WritableByteChannel channel = new WritableByteChannel() {

        @Override
        public int write(ByteBuffer src) throws IOException {
            int size = src.remaining();
            buffer.put(src);
            return size;
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };
    writableSupplier.writeToChannel(channel);
    buffer.rewind();
    return buffer;
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 43 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project druid by druid-io.

the class CompressedVSizeIndexedBenchmark method serialize.

private static ByteBuffer serialize(WritableSupplier<IndexedMultivalue<IndexedInts>> writableSupplier) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect((int) writableSupplier.getSerializedSize());
    WritableByteChannel channel = new WritableByteChannel() {

        @Override
        public int write(ByteBuffer src) throws IOException {
            int size = src.remaining();
            buffer.put(src);
            return size;
        }

        @Override
        public boolean isOpen() {
            return true;
        }

        @Override
        public void close() throws IOException {
        }
    };
    writableSupplier.writeToChannel(channel);
    buffer.rewind();
    return buffer;
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 44 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project druid by druid-io.

the class SerializerUtilsTest method testChannelWritelong.

@Test
public void testChannelWritelong() throws IOException {
    final int index = 0;
    WritableByteChannel channelOutput = Channels.newChannel(outStream);
    serializerUtils.writeLong(channelOutput, longs[index]);
    ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray());
    channelOutput.close();
    inputstream.close();
    long expected = serializerUtils.readLong(inputstream);
    long actuals = longs[index];
    Assert.assertEquals(expected, actuals);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Example 45 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project druid by druid-io.

the class SerializerUtilsTest method testChannelWriteString.

@Test
public void testChannelWriteString() throws IOException {
    final int index = 0;
    WritableByteChannel channelOutput = Channels.newChannel(outStream);
    serializerUtils.writeString(channelOutput, strings[index]);
    ByteArrayInputStream inputstream = new ByteArrayInputStream(outStream.toByteArray());
    channelOutput.close();
    inputstream.close();
    String expected = serializerUtils.readString(inputstream);
    String actuals = strings[index];
    Assert.assertEquals(expected, actuals);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) Test(org.junit.Test)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)118 ByteBuffer (java.nio.ByteBuffer)35 ReadableByteChannel (java.nio.channels.ReadableByteChannel)30 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 FileOutputStream (java.io.FileOutputStream)24 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 IOException (java.io.IOException)16 Test (org.junit.Test)15 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)7 Writer (java.io.Writer)6 FileChannel (java.nio.channels.FileChannel)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4