implement truncate and fstat better
[nestedvm.git] / src / org / ibex / nestedvm / util / Platform.java
index e1f412a..ed59627 100644 (file)
@@ -8,7 +8,30 @@ import java.text.DateFormatSymbols;
 
 public abstract class Platform {
     Platform() { }
-    private static final Platform p=null;
+    private static final Platform p;
+    
+    static {
+        float version;
+        try {
+            version = Float.valueOf(System.getProperty("java.specification.version")).floatValue();
+        } catch(Exception e) {
+            System.err.println("WARNING: " + e + " while trying to find jvm version -  assuming 1.1");
+            version = 1.1f;
+        }
+        String platformClass;
+        if(version >= 1.4f) platformClass = "Jdk14";
+        else if(version >= 1.3f) platformClass = "Jdk13";
+        else if(version >= 1.2f) platformClass = "Jdk12";
+        else if(version >= 1.1f) platformClass = "Jdk11";
+        else throw new Error("JVM Specification version: " + version + " is too old. (see org.ibex.util.Platform to add support)");
+        
+        try {
+            p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
+        } catch(Exception e) {
+            e.printStackTrace();
+            throw new Error("Error instansiating platform class");
+        }
+    }
     
     abstract boolean _atomicCreateFile(File f) throws IOException;
     public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
@@ -26,9 +49,17 @@ public abstract class Platform {
     public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); }
     public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); }
     
+    abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException;
+    public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); }
+    
+    // FEATURE: Make sure GCClass can get rid of uncalled superclass methdos
+    
     static class Jdk11 extends Platform {
         boolean _atomicCreateFile(File f) throws IOException {
-            throw new Error("FIXME");
+            // This is not atomic, but its the best we can do on jdk 1.1
+            if(f.exists()) return false;
+            new FileOutputStream(f).close();
+            return true;
         }
         void _socketHalfClose(Socket s, boolean output) throws IOException {
             throw new IOException("half closing sockets not supported");
@@ -55,6 +86,11 @@ public abstract class Platform {
             if(off > 0) sb.append(":").append(off);
             return sb.toString();
         }
+        
+        RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
+            new FileOutputStream(f).close();
+            return new RandomAccessFile(f,mode);
+        }
     }
     
     static class Jdk12 extends Jdk11 {
@@ -65,6 +101,12 @@ public abstract class Platform {
         String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
             return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
         }
+        
+        RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
+            RandomAccessFile raf = new RandomAccessFile(f,mode);
+            raf.setLength(0);
+            return raf;
+        }       
     }
     
     static class Jdk13 extends Jdk12 {