@Override public synchronized int read() throws IOException { checkNotClosed(); if (finished) { return -1; } file.readLock().lock(); try { int b = file.read(pos++); // it's ok for pos to go beyond size() if (b == -1) { finished = true; } else { file.updateAccessTime(); } return b; } finally { file.readLock().unlock(); } }
private synchronized int readInternal(byte[] b, int off, int len) throws IOException { checkNotClosed(); if (finished) { return -1; } file.readLock().lock(); try { int read = file.read(pos, b, off, len); if (read == -1) { finished = true; } else { pos += read; } file.updateAccessTime(); return read; } finally { file.readLock().unlock(); } }
@Override public long size() throws IOException { checkOpen(); long size = 0; // will definitely either be assigned or an exception will be thrown boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { size = file.sizeWithoutLocking(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return size; }
@Override public int read(ByteBuffer dst, long position) throws IOException { checkNotNull(dst); Util.checkNotNegative(position, "position"); checkOpen(); checkReadable(); int read = 0; // will definitely either be assigned or an exception will be thrown // no need to synchronize here; this method does not make use of the channel's position boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return read; }
@Override public long transferTo(long position, long count, WritableByteChannel target) throws IOException { checkNotNull(target); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkReadable(); long transferred = 0; // will definitely either be assigned or an exception will be thrown // no need to synchronize here; this method does not make use of the channel's position boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { transferred = file.transferTo(position, count, target); file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return transferred; }
@Override public synchronized int read() throws IOException { checkNotClosed(); if (finished) { return -1; } file.readLock().lock(); try { int b = file.read(pos++); // it's ok for pos to go beyond size() if (b == -1) { finished = true; } else { file.updateAccessTime(); } return b; } finally { file.readLock().unlock(); } }
private synchronized int readInternal(byte[] b, int off, int len) throws IOException { checkNotClosed(); if (finished) { return -1; } file.readLock().lock(); try { int read = file.read(pos, b, off, len); if (read == -1) { finished = true; } else { pos += read; } file.updateAccessTime(); return read; } finally { file.readLock().unlock(); } }
@Override public long size() throws IOException { checkOpen(); long size = 0; // will definitely either be assigned or an exception will be thrown boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { size = file.sizeWithoutLocking(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return size; }
@Override public int read(ByteBuffer dst, long position) throws IOException { checkNotNull(dst); Util.checkNotNegative(position, "position"); checkOpen(); checkReadable(); int read = 0; // will definitely either be assigned or an exception will be thrown // no need to synchronize here; this method does not make use of the channel's position boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { read = file.read(position, dst); file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return read; }
@Override public long transferTo(long position, long count, WritableByteChannel target) throws IOException { checkNotNull(target); Util.checkNotNegative(position, "position"); Util.checkNotNegative(count, "count"); checkOpen(); checkReadable(); long transferred = 0; // will definitely either be assigned or an exception will be thrown // no need to synchronize here; this method does not make use of the channel's position boolean completed = false; try { if (!beginBlocking()) { return 0; // AsynchronousCloseException will be thrown } file.readLock().lockInterruptibly(); try { transferred = file.transferTo(position, count, target); file.updateAccessTime(); completed = true; } finally { file.readLock().unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { endBlocking(completed); } return transferred; }