@Test public void testTransferFrom() throws IOException { RegularFile file = regularFile(0); FileChannel channel = channel(file, WRITE); ByteBufferChannel readChannel = new ByteBufferChannel(buffer("1234567890")); assertEquals(10, channel.transferFrom(readChannel, 0, 100)); assertEquals(0, channel.position()); }
@Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { checkNotNull(src); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkWritable(); long transferred = 0; if (append) { synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { position = file.sizeWithoutLocking(); transferred = file.transferFrom(src, position, count); this.position = position + transferred; file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } } else { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { transferred = file.transferFrom(src, position, count); file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return transferred; }
JimfsFileChannel extends FileChannel { @Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { checkNotNull(src); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkWritable(); long transferred = 0; if (append) { synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { position = file.sizeWithoutLocking(); transferred = file.transferFrom(src, position, count); this.position = position + transferred; file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } } else { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { transferred = file.transferFrom(src, position, count); file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return transferred; } }
JimfsFileChannel extends FileChannel { @Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { checkNotNull(src); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkWritable(); long transferred = 0; if (append) { synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { position = file.sizeWithoutLocking(); transferred = file.transferFrom(src, position, count); this.position = position + transferred; file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } } else { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { transferred = file.transferFrom(src, position, count); file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return transferred; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); }
JimfsFileChannel extends FileChannel { @Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { checkNotNull(src); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkWritable(); long transferred = 0; if (append) { synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { position = file.sizeWithoutLocking(); transferred = file.transferFrom(src, position, count); this.position = position + transferred; file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } } else { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { transferred = file.transferFrom(src, position, count); file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return transferred; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); AsynchronousFileChannel asAsynchronousFileChannel(ExecutorService executor); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int read(ByteBuffer dst, long position); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override int write(ByteBuffer src, long position); @Override long position(); @Override FileChannel position(long newPosition); @Override long size(); @Override FileChannel truncate(long size); @Override void force(boolean metaData); @Override long transferTo(long position, long count, WritableByteChannel target); @Override long transferFrom(ReadableByteChannel src, long position, long count); @Override MappedByteBuffer map(MapMode mode, long position, long size); @Override FileLock lock(long position, long size, boolean shared); @Override FileLock tryLock(long position, long size, boolean shared); }
JimfsFileChannel extends FileChannel { @Override public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException { checkNotNull(src); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkWritable(); long transferred = 0; if (append) { synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { position = file.sizeWithoutLocking(); transferred = file.transferFrom(src, position, count); this.position = position + transferred; file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } } else { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.writeLock().lockInterruptibly(); try { transferred = file.transferFrom(src, position, count); file.updateModifiedTime(); completed = true; } finally { file.writeLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return transferred; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); AsynchronousFileChannel asAsynchronousFileChannel(ExecutorService executor); @Override int read(ByteBuffer dst); @Override long read(ByteBuffer[] dsts, int offset, int length); @Override int read(ByteBuffer dst, long position); @Override int write(ByteBuffer src); @Override long write(ByteBuffer[] srcs, int offset, int length); @Override int write(ByteBuffer src, long position); @Override long position(); @Override FileChannel position(long newPosition); @Override long size(); @Override FileChannel truncate(long size); @Override void force(boolean metaData); @Override long transferTo(long position, long count, WritableByteChannel target); @Override long transferFrom(ReadableByteChannel src, long position, long count); @Override MappedByteBuffer map(MapMode mode, long position, long size); @Override FileLock lock(long position, long size, boolean shared); @Override FileLock tryLock(long position, long size, boolean shared);  }