Search in sources :

Example 16 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 17 with WritableByteChannel

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

the class StreamToDisk method write.

@Override
public void write(String destination, int requiredAlignment, byte[] data) throws IOException {
    File fileName = new File(storeDir, destination);
    fs.mkdirs(fileName.getParentFile());
    fileCopyMonitor.copyFile(fileName);
    if (StoreType.shouldBeManagedByPageCache(destination)) {
        WritableByteChannel channel = channels.get(destination);
        if (channel == null) {
            int filePageSize = pageCache.pageSize() - pageCache.pageSize() % requiredAlignment;
            PagedFile pagedFile = pageCache.map(fileName, filePageSize, StandardOpenOption.CREATE);
            channel = pagedFile.openWritableByteChannel();
            pagedFiles.put(destination, pagedFile);
            channels.put(destination, channel);
        }
        ByteBuffer buffer = ByteBuffer.wrap(data);
        while (buffer.hasRemaining()) {
            channel.write(buffer);
        }
    } else {
        try (OutputStream outputStream = fs.openAsOutputStream(fileName, true)) {
            outputStream.write(data);
        }
    }
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) OutputStream(java.io.OutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 18 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project openhab1-addons by openhab.

the class CULNetworkHandlerImpl method processWrite.

private void processWrite(SelectionKey key) throws IOException {
    WritableByteChannel ch = (WritableByteChannel) key.channel();
    synchronized (writeBuf) {
        writeBuf.flip();
        int bytesOp = 0, bytesTotal = 0;
        while (writeBuf.hasRemaining() && (bytesOp = ch.write(writeBuf)) > 0) {
            bytesTotal += bytesOp;
        }
        logger.debug("Written {} bytes to the network", bytesTotal);
        if (writeBuf.remaining() == 0) {
            key.interestOps(key.interestOps() ^ SelectionKey.OP_WRITE);
        }
        if (bytesTotal > 0) {
            writeBuf.notify();
        } else if (bytesOp == -1) {
            logger.info("peer closed write channel");
            ch.close();
        }
        writeBuf.compact();
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 19 with WritableByteChannel

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

the class ChannelsTest method createNonBlockingChannel.

private Pipe.SourceChannel createNonBlockingChannel(byte[] content) throws IOException {
    Pipe pipe = Pipe.open();
    WritableByteChannel sinkChannel = pipe.sink();
    sinkChannel.write(ByteBuffer.wrap(content));
    Pipe.SourceChannel sourceChannel = pipe.source();
    sourceChannel.configureBlocking(false);
    return sourceChannel;
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) Pipe(java.nio.channels.Pipe)

Example 20 with WritableByteChannel

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

the class OldFileChannelTest method test_transferToJJLWritableByteChannel_IllegalArgument.

public void test_transferToJJLWritableByteChannel_IllegalArgument() throws Exception {
    WritableByteChannel writableByteChannel = DatagramChannel.open();
    try {
        readOnlyFileChannel.transferTo(10, -1, writableByteChannel);
        fail("should throw IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        readWriteFileChannel.transferTo(-1, 10, writableByteChannel);
        fail("should throw IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 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)6 Writer (java.io.Writer)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 Map (java.util.Map)4