當前位置: 首頁>>代碼示例>>Java>>正文


Java AsynchronousFileChannel.write方法代碼示例

本文整理匯總了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);
    
}
 
開發者ID:sbridges,項目名稱:ephemeralfs,代碼行數:24,代碼來源:AsynchronousFileChannelTest.java

示例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;
}
 
開發者ID:openknowledge,項目名稱:reactive-jax-rs,代碼行數:39,代碼來源:CustomerRepository.java

示例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);
}
 
開發者ID:google,項目名稱:jimfs,代碼行數:11,代碼來源:JimfsAsynchronousFileChannelTest.java

示例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()));
}
 
開發者ID:bsonntag,項目名稱:Neddy,代碼行數:7,代碼來源:Files.java


注:本文中的java.nio.channels.AsynchronousFileChannel.write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。