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];
}
public static class File extends Seekable {
+ private final java.io.File file;
private final RandomAccessFile raf;
public File(String fileName) throws IOException { this(fileName,false); }
public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable,false); }
public File(java.io.File file, boolean writable, boolean truncate) throws IOException {
+ this.file = file;
String mode = writable ? "rw" : "r";
- raf = truncate ? Platform.truncatedRandomAccessFile(file,mode) : new RandomAccessFile(file,mode);
+ raf = new RandomAccessFile(file,mode);
+ if (truncate) Platform.setFileLength(raf, 0);
}
public int read(byte[] buf, int offset, int length) throws IOException { return raf.read(buf,offset,length); }
public int write(byte[] buf, int offset, int length) throws IOException { raf.write(buf,offset,length); return length; }
+ public void sync() throws IOException { raf.getFD().sync(); }
public void seek(int pos) throws IOException{ raf.seek(pos); }
public int pos() throws IOException { return (int) raf.getFilePointer(); }
public int length() throws IOException { return (int)raf.length(); }
public void close() throws IOException { raf.close(); }
- public void resize(long length) throws IOException { raf.setLength(length); }
+ public void resize(long length) throws IOException { Platform.setFileLength(raf, (int)length); }
+ public boolean equals(Object o) {
+ return o != null && o instanceof File
+ && file.equals(((File)o).file);
+ }
+ public Lock lock(long pos, long size, boolean shared)
+ throws IOException {
+ return Platform.lockFile(this, raf, pos, size, shared);
+ }
}
public static class InputStream extends Seekable {
public void close() throws IOException { is.close(); }
}
+ public abstract static class Lock {
+ private Object owner = null;
+
+ public abstract Seekable seekable();
+ public abstract boolean isShared();
+ public abstract boolean isValid();
+ public abstract void release() throws IOException;
+ public abstract long position();
+ public abstract long size();
+
+ public void setOwner(Object o) { owner = o; }
+ public Object getOwner() { return owner; }
+
+ public final boolean contains(int start, int len) {
+ return start >= position() && position() + size() >= start + len;
+ }
+
+ public final boolean contained(int start, int len) {
+ return start < position() && position() + size() < start + len;
+ }
+
+ public final boolean overlaps(int start, int len) {
+ return contains(start, len) || contained(start, len);
+ }
+ }
}