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);
}
}
}
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);
}
}
}
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();
}
}
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;
}
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
}
}
Aggregations