1 package org.ibex.nestedvm.util;
7 import java.text.DateFormatSymbols;
9 public abstract class Platform {
11 private static final Platform p;
16 version = Float.valueOf(System.getProperty("java.specification.version")).floatValue();
17 } catch(Exception e) {
18 System.err.println("WARNING: " + e + " while trying to find jvm version - assuming 1.1");
22 if(version >= 1.4f) platformClass = "Jdk14";
23 else if(version >= 1.3f) platformClass = "Jdk13";
24 else if(version >= 1.2f) platformClass = "Jdk12";
25 else if(version >= 1.1f) platformClass = "Jdk11";
26 else throw new Error("JVM Specification version: " + version + " is too old. (see org.ibex.util.Platform to add support)");
29 p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
30 } catch(Exception e) {
32 throw new Error("Error instansiating platform class");
36 abstract boolean _atomicCreateFile(File f) throws IOException;
37 public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
39 abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
40 public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
42 abstract void _socketSetKeepAlive(Socket s, boolean on) throws SocketException;
43 public static void socketSetKeepAlive(Socket s, boolean on) throws SocketException { p._socketSetKeepAlive(s,on); }
45 abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
46 public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
48 abstract String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l);
49 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); }
50 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); }
52 abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException;
53 public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); }
55 // FEATURE: Make sure GCClass can get rid of uncalled superclass methdos
57 static class Jdk11 extends Platform {
58 boolean _atomicCreateFile(File f) throws IOException {
59 // This is not atomic, but its the best we can do on jdk 1.1
60 if(f.exists()) return false;
61 new FileOutputStream(f).close();
64 void _socketHalfClose(Socket s, boolean output) throws IOException {
65 throw new IOException("half closing sockets not supported");
67 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
68 if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
69 return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
71 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
72 if(on) throw new SocketException("keepalive not supported");
74 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
75 String[][] zs = new DateFormatSymbols(l).getZoneStrings();
76 String id = tz.getID();
77 for(int i=0;i<zs.length;i++)
78 if(zs[i][0].equals(id))
79 return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
80 StringBuffer sb = new StringBuffer("GMT");
81 int off = tz.getRawOffset() / 1000;
82 if(off < 0) { sb.append("-"); off = -off; }
84 sb.append(off/3600); off = off%3600;
85 if(off > 0) sb.append(":").append(off/60); off=off%60;
86 if(off > 0) sb.append(":").append(off);
90 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
91 new FileOutputStream(f).close();
92 return new RandomAccessFile(f,mode);
96 static class Jdk12 extends Jdk11 {
97 boolean _atomicCreateFile(File f) throws IOException {
98 return f.createNewFile();
101 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
102 return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
105 RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException {
106 RandomAccessFile raf = new RandomAccessFile(f,mode);
112 static class Jdk13 extends Jdk12 {
113 void _socketHalfClose(Socket s, boolean output) throws IOException {
114 if(output) s.shutdownOutput();
115 else s.shutdownInput();
118 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
123 static class Jdk14 extends Jdk13 {
124 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); }