make jdk 1.1 compliant part 2
[nestedvm.git] / src / org / ibex / nestedvm / util / Platform.java
diff --git a/src/org/ibex/nestedvm/util/Platform.java b/src/org/ibex/nestedvm/util/Platform.java
new file mode 100644 (file)
index 0000000..e1f412a
--- /dev/null
@@ -0,0 +1,84 @@
+package org.ibex.nestedvm.util;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import java.text.DateFormatSymbols;
+
+public abstract class Platform {
+    Platform() { }
+    private static final Platform p=null;
+    
+    abstract boolean _atomicCreateFile(File f) throws IOException;
+    public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
+    
+    abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
+    public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
+    
+    abstract void _socketSetKeepAlive(Socket s, boolean on) throws SocketException;
+    public static void socketSetKeepAlive(Socket s, boolean on) throws SocketException { p._socketSetKeepAlive(s,on); }
+    
+    abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
+    public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
+    
+    abstract String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l);
+    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()); }
+    
+    static class Jdk11 extends Platform {
+        boolean _atomicCreateFile(File f) throws IOException {
+            throw new Error("FIXME");
+        }
+        void _socketHalfClose(Socket s, boolean output) throws IOException {
+            throw new IOException("half closing sockets not supported");
+        }
+        InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
+            if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
+            return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
+        }
+        void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
+            if(on) throw new SocketException("keepalive not supported");
+        }
+        String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
+            String[][] zs  = new DateFormatSymbols(l).getZoneStrings();
+            String id = tz.getID();
+            for(int i=0;i<zs.length;i++)
+                if(zs[i][0].equals(id))
+                    return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
+            StringBuffer sb = new StringBuffer("GMT");
+            int off = tz.getRawOffset() / 1000;
+            if(off < 0) { sb.append("-"); off = -off; }
+            else sb.append("+");
+            sb.append(off/3600); off = off%3600;
+            if(off > 0) sb.append(":").append(off/60); off=off%60;
+            if(off > 0) sb.append(":").append(off);
+            return sb.toString();
+        }
+    }
+    
+    static class Jdk12 extends Jdk11 {
+        boolean _atomicCreateFile(File f) throws IOException {
+            return f.createNewFile();
+        }
+        
+        String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
+            return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
+        }
+    }
+    
+    static class Jdk13 extends Jdk12 {
+        void _socketHalfClose(Socket s, boolean output) throws IOException {
+            if(output) s.shutdownOutput();
+            else s.shutdownInput();
+        }
+        
+        void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
+            s.setKeepAlive(on);
+        }
+    }
+    
+    static class Jdk14 extends Jdk13 {
+        InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); } 
+    }
+}