-public interface Seekable {
- public int read(byte[] buf, int offset, int length) throws IOException;
- public int write(byte[] buf, int offset, int length) throws IOException;
- public int length() throws IOException;
- public void seek(int pos) throws IOException;
- public void close() throws IOException;
- public int pos() throws IOException;
+public abstract class Seekable {
+ public abstract int read(byte[] buf, int offset, int length) throws IOException;
+ public abstract int write(byte[] buf, int offset, int length) throws IOException;
+ public abstract int length() throws IOException;
+ public abstract void seek(int pos) throws IOException;
+ public abstract void close() throws IOException;
+ public abstract int pos() throws IOException;
+
+ public void sync() throws IOException {
+ throw new IOException("sync not implemented for " + getClass());
+ }
+ public void resize(long length) throws IOException {
+ throw new IOException("resize not implemented for " + getClass());
+ }
+ /** If pos == 0 and size == 0 lock covers whole file. */
+ public Lock lock(long pos, long size, boolean shared) throws IOException {
+ throw new IOException("lock not implemented for " + getClass());
+ }
+
+ public int read() throws IOException {
+ byte[] buf = new byte[1];
+ int n = read(buf,0,1);
+ return n == -1 ? -1 : buf[0]&0xff;
+ }
+
+ public int tryReadFully(byte[] buf, int off, int len) throws IOException {
+ int total = 0;
+ while(len > 0) {
+ int n = read(buf,off,len);
+ if(n == -1) break;
+ off += n;
+ len -= n;
+ total += n;
+ }
+ return total == 0 ? -1 : total;
+ }