@Override public long skip(long n) throws IOException { if (n <= 0) { return 0; } synchronized (this) { checkNotClosed(); if (finished) { return 0; } // available() must be an int, so the min must be also int skip = (int) Math.min(Math.max(file.size() - pos, 0), n); pos += skip; return skip; } }
@Override public synchronized int available() throws IOException { checkNotClosed(); if (finished) { return 0; } long available = Math.max(file.size() - pos, 0); return Ints.saturatedCast(available); }
@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 skip(long n) throws IOException { if (n <= 0) { return 0; } synchronized (this) { checkNotClosed(); if (finished) { return 0; } // available() must be an int, so the min must be also int skip = (int) Math.min(Math.max(file.size() - pos, 0), n); pos += skip; return skip; } }
@Override public synchronized int available() throws IOException { checkNotClosed(); if (finished) { return 0; } long available = Math.max(file.size() - pos, 0); return Ints.saturatedCast(available); }
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 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(); } }