abstract RandomAccessFile.setLength() for Jdk11 support
[nestedvm.git] / src / org / ibex / nestedvm / util / Seekable.java
index 964ebc2..9da8d27 100644 (file)
@@ -84,7 +84,8 @@ public abstract class Seekable {
         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); }
@@ -93,7 +94,7 @@ public abstract class Seekable {
         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);
@@ -149,12 +150,29 @@ public abstract class Seekable {
         public void close() throws IOException { is.close(); }
     }
     
-    public interface Lock {
-        public Seekable seekable();
-        public boolean isShared();
-        public boolean isValid();
-        public void release() throws IOException;
-        public long position();
-        public long size();
+    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);
+        }
     }
 }