jdk 1.1 part 3
[nestedvm.git] / src / org / ibex / nestedvm / util / Platform.java
1 package org.ibex.nestedvm.util;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6
7 import java.text.DateFormatSymbols;
8
9 public abstract class Platform {
10     Platform() { }
11     private static final Platform p;
12     
13     static {
14         float version;
15         try {
16             version = Float.parseFloat(System.getProperty("java.specification.version"));
17         } catch(Exception e) {
18             System.err.println("WARNING: " + e + " while trying to find jvm version -  assuming 1.1");
19             version = 1.1f;
20         }
21         String platformClass;
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)");
27         
28         try {
29             p = (Platform) Class.forName(Platform.class.getName() + "$" + platformClass).newInstance();
30         } catch(Exception e) {
31             e.printStackTrace();
32             throw new Error("Error instansiating platform class");
33         }
34     }
35     
36     abstract boolean _atomicCreateFile(File f) throws IOException;
37     public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
38     
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); }
41     
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); }
44     
45     abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
46     public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
47     
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()); }
51     
52     static class Jdk11 extends Platform {
53         boolean _atomicCreateFile(File f) throws IOException {
54             // FIXME: Just do this non-atomicly
55             throw new RuntimeException("FIXME");
56         }
57         void _socketHalfClose(Socket s, boolean output) throws IOException {
58             throw new IOException("half closing sockets not supported");
59         }
60         InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
61             if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
62             return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
63         }
64         void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
65             if(on) throw new SocketException("keepalive not supported");
66         }
67         String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
68             String[][] zs  = new DateFormatSymbols(l).getZoneStrings();
69             String id = tz.getID();
70             for(int i=0;i<zs.length;i++)
71                 if(zs[i][0].equals(id))
72                     return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
73             StringBuffer sb = new StringBuffer("GMT");
74             int off = tz.getRawOffset() / 1000;
75             if(off < 0) { sb.append("-"); off = -off; }
76             else sb.append("+");
77             sb.append(off/3600); off = off%3600;
78             if(off > 0) sb.append(":").append(off/60); off=off%60;
79             if(off > 0) sb.append(":").append(off);
80             return sb.toString();
81         }
82     }
83     
84     static class Jdk12 extends Jdk11 {
85         boolean _atomicCreateFile(File f) throws IOException {
86             return f.createNewFile();
87         }
88         
89         String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
90             return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
91         }
92     }
93     
94     static class Jdk13 extends Jdk12 {
95         void _socketHalfClose(Socket s, boolean output) throws IOException {
96             if(output) s.shutdownOutput();
97             else s.shutdownInput();
98         }
99         
100         void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
101             s.setKeepAlive(on);
102         }
103     }
104     
105     static class Jdk14 extends Jdk13 {
106         InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); } 
107     }
108 }