@Test public void testWrite() throws IOException { Path srcPath = Paths.get("test_data/test.txt"); FileChannel channel = FileChannel.open(srcPath, set(READ, WRITE)); byte[] array = "Hello world!!!!!".getBytes(); await(Promises.all(IntStream.range(0, 1000) .mapToObj($ -> service.write(channel, 0, array, 0, array.length) .whenComplete((res, e) -> { if (e != null) { e.printStackTrace(); fail(); } assertEquals(res.intValue(), array.length); })))); }
@Override public Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size) { return ofBlockingCallable(executor, () -> { ByteBuffer buffer = ByteBuffer.wrap(array, offset, size); long pos = position; try { do { pos += channel.write(buffer, pos); } while (buffer.position() < buffer.limit()); } catch (IOException e) { throw new UncheckedException(e); } return Math.toIntExact(pos - position); }); }
ExecutorAsyncFileService implements AsyncFileService { @Override public Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size) { return ofBlockingCallable(executor, () -> { ByteBuffer buffer = ByteBuffer.wrap(array, offset, size); long pos = position; try { do { pos += channel.write(buffer, pos); } while (buffer.position() < buffer.limit()); } catch (IOException e) { throw new UncheckedException(e); } return Math.toIntExact(pos - position); }); } }
ExecutorAsyncFileService implements AsyncFileService { @Override public Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size) { return ofBlockingCallable(executor, () -> { ByteBuffer buffer = ByteBuffer.wrap(array, offset, size); long pos = position; try { do { pos += channel.write(buffer, pos); } while (buffer.position() < buffer.limit()); } catch (IOException e) { throw new UncheckedException(e); } return Math.toIntExact(pos - position); }); } ExecutorAsyncFileService(@NotNull Executor executor); }
ExecutorAsyncFileService implements AsyncFileService { @Override public Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size) { return ofBlockingCallable(executor, () -> { ByteBuffer buffer = ByteBuffer.wrap(array, offset, size); long pos = position; try { do { pos += channel.write(buffer, pos); } while (buffer.position() < buffer.limit()); } catch (IOException e) { throw new UncheckedException(e); } return Math.toIntExact(pos - position); }); } ExecutorAsyncFileService(@NotNull Executor executor); @Override Promise<Integer> read(FileChannel channel, long position, byte[] array, int offset, int size); @Override Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size); }
ExecutorAsyncFileService implements AsyncFileService { @Override public Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size) { return ofBlockingCallable(executor, () -> { ByteBuffer buffer = ByteBuffer.wrap(array, offset, size); long pos = position; try { do { pos += channel.write(buffer, pos); } while (buffer.position() < buffer.limit()); } catch (IOException e) { throw new UncheckedException(e); } return Math.toIntExact(pos - position); }); } ExecutorAsyncFileService(@NotNull Executor executor); @Override Promise<Integer> read(FileChannel channel, long position, byte[] array, int offset, int size); @Override Promise<Integer> write(FileChannel channel, long position, byte[] array, int offset, int size);  }