本文整理匯總了Java中java.nio.channels.AsynchronousFileChannel.write方法的典型用法代碼示例。如果您正苦於以下問題:Java AsynchronousFileChannel.write方法的具體用法?Java AsynchronousFileChannel.write怎麽用?Java AsynchronousFileChannel.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.AsynchronousFileChannel
的用法示例。
在下文中一共展示了AsynchronousFileChannel.write方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testErrorGoesToHandler
import java.nio.channels.AsynchronousFileChannel; //導入方法依賴的package包/類
@Test
public void testErrorGoesToHandler() throws Exception {
Path test = root.resolve("test");
AsynchronousFileChannel file = AsynchronousFileChannel.open(test,
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE,
StandardOpenOption.READ
);
file.close();
byte[] contents = new byte[512];
random.nextBytes(contents);
CountdownCompletionHandler<Integer> writeCompletionHandler =
new CountdownCompletionHandler<>();
file.write(ByteBuffer.wrap(contents), 0, writeCompletionHandler, writeCompletionHandler);
assertTrue(writeCompletionHandler.getExc() instanceof ClosedChannelException);
}
示例2: save
import java.nio.channels.AsynchronousFileChannel; //導入方法依賴的package包/類
public Publisher<Integer> save(Publisher<Customer> customers) throws IOException {
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
AtomicLong offset = new AtomicLong(0);
AtomicInteger resultCount = new AtomicInteger(0);
SingleItemPublisher<Integer> resultPublisher = new SingleItemPublisher<>();
Semaphore writeSemaphore = new Semaphore(1);
writeSemaphore.acquireUninterruptibly();
fileChannel.write(ByteBuffer.wrap("[".getBytes()), 0, resultPublisher,
andThen((count, s) -> {
writeSemaphore.release();
customers.subscribe(pullEach((Customer customer, Subscription subscription) -> {
String json = String.format("%s{\"firstName\": \"%s\", \"lastName\": \"%s\"}", offset.longValue() == 0 ? "" : ",",
customer.getFirstName(), customer.getLastName());
offset.addAndGet(count);
writeSemaphore.acquireUninterruptibly();
fileChannel.write(ByteBuffer.wrap(json.getBytes()), offset.get(), resultPublisher,
andThen((size, c) -> {
writeSemaphore.release();
offset.addAndGet(size);
resultCount.incrementAndGet();
subscription.request(1);
}));
}).andThen(() -> {
writeSemaphore.acquireUninterruptibly();
fileChannel.write(ByteBuffer.wrap("]".getBytes()), offset.longValue(), resultPublisher,
andThen((d, e) -> {
writeSemaphore.release();
try {
fileChannel.close();
resultPublisher.publish(resultCount.intValue());
} catch (IOException error) {
resultPublisher.publish(error);
}
}));
}).exceptionally(error -> resultPublisher.publish(error)));
}));
return resultPublisher;
}
示例3: checkAsyncWrite
import java.nio.channels.AsynchronousFileChannel; //導入方法依賴的package包/類
private static void checkAsyncWrite(AsynchronousFileChannel asyncChannel) throws Throwable {
ByteBuffer buf = buffer("1234567890");
assertEquals(10, (int) asyncChannel.write(buf, 0).get());
buf.flip();
SettableFuture<Integer> future = SettableFuture.create();
asyncChannel.write(buf, 0, null, setFuture(future));
assertThat(future.get(10, SECONDS)).isEqualTo(10);
}
示例4: writeFile
import java.nio.channels.AsynchronousFileChannel; //導入方法依賴的package包/類
private static void writeFile(Path path, ByteBuffer data, WriteHandler handler)
throws IOException {
AsynchronousFileChannel fileChannel = open(path, writeOptions);
fileChannel.write(data, 0, null,
new WriteCompletionHandler(handler, data.remaining()));
}