@Test public void testRead() throws IOException { RegularFile file = regularFile(20); FileChannel channel = channel(file, READ); assertEquals(0, channel.position()); ByteBuffer buf = buffer("1234567890"); ByteBuffer buf2 = buffer("123457890"); assertEquals(10, channel.read(buf)); assertEquals(10, channel.position()); buf.flip(); assertEquals(10, channel.read(new ByteBuffer[] {buf, buf2})); assertEquals(20, channel.position()); buf.flip(); buf2.flip(); file.write(20, new byte[10], 0, 10); assertEquals(10, channel.read(new ByteBuffer[] {buf, buf2}, 0, 2)); assertEquals(30, channel.position()); buf.flip(); assertEquals(10, channel.read(buf, 5)); assertEquals(30, channel.position()); buf.flip(); assertEquals(-1, channel.read(buf)); assertEquals(30, channel.position()); }
@Override public int read(ByteBuffer dst) throws IOException { checkNotNull(dst); checkOpen(); checkReadable(); int read = 0; synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); if (read != -1) { position += read; } file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return read; }
JimfsFileChannel extends FileChannel { @Override public int read(ByteBuffer dst) throws IOException { checkNotNull(dst); checkOpen(); checkReadable(); int read = 0; synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); if (read != -1) { position += read; } file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return read; } }
JimfsFileChannel extends FileChannel { @Override public int read(ByteBuffer dst) throws IOException { checkNotNull(dst); checkOpen(); checkReadable(); int read = 0; synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); if (read != -1) { position += read; } file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return read; } JimfsFileChannel(
      RegularFile file, Set<OpenOption> options, FileSystemState fileSystemState); }
JimfsFileChannel extends FileChannel { @Override public int read(ByteBuffer dst) throws IOException { checkNotNull(dst); checkOpen(); checkReadable(); int read = 0; synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); if (read != -1) { position += read; } file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return read; } 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 int read(ByteBuffer dst) throws IOException { checkNotNull(dst); checkOpen(); checkReadable(); int read = 0; synchronized (this) { boolean completed = false; try { if (!beginBlocking()) { return 0; } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); if (read != -1) { position += read; } file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } } return read; } 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);  }