add ftruncate() syscall
[nestedvm.git] / src / org / ibex / nestedvm / util / Seekable.java
index 2e48a7d..cacca0a 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.nestedvm.util;
 
 import java.io.*;
@@ -9,6 +13,10 @@ public abstract class Seekable {
     public abstract void seek(int pos) throws IOException;
     public abstract void close() throws IOException;
     public abstract int pos() throws IOException;
+
+    public void resize(long length) throws IOException {
+        throw new IOException("resize not implemented for " + getClass());
+    }
     
     public int read() throws IOException {
         byte[] buf = new byte[1];
@@ -19,10 +27,10 @@ public abstract class Seekable {
     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;
+                int n = read(buf,off,len);
+                if(n == -1) break;
+                off += n;
+                len -= n;
             total += n;
         }
         return total == 0 ? -1 : total;
@@ -66,21 +74,20 @@ public abstract class Seekable {
         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); }    
+        public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable,false); }    
         
-        public File(java.io.File file, boolean writable) throws IOException {
-            raf = new RandomAccessFile(file,writable ? "rw" : "r");
+        public File(java.io.File file, boolean writable, boolean truncate) throws IOException {
+            String mode = writable ? "rw" : "r";
+            raf = truncate ? Platform.truncatedRandomAccessFile(file,mode) : new RandomAccessFile(file,mode);
         }
         
-        // NOTE: RandomAccessFile.setLength() is a Java2 function
-        public void setLength(int n) throws IOException { raf.setLength(n); }
-        
         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 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 static class InputStream extends Seekable {