1 package org.ibex.nestedvm.util;
7 import java.text.DateFormatSymbols;
9 public abstract class Platform {
11 private static final Platform p=null;
13 abstract boolean _atomicCreateFile(File f) throws IOException;
14 public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
16 abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
17 public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
19 abstract void _socketSetKeepAlive(Socket s, boolean on) throws SocketException;
20 public static void socketSetKeepAlive(Socket s, boolean on) throws SocketException { p._socketSetKeepAlive(s,on); }
22 abstract InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException;
23 public static InetAddress inetAddressFromBytes(byte[] a) throws UnknownHostException { return p._inetAddressFromBytes(a); }
25 abstract String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l);
26 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return p._timeZoneGetDisplayName(tz,dst,showlong,l); }
27 public static String timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong) { return timeZoneGetDisplayName(tz,dst,showlong,Locale.getDefault()); }
29 static class Jdk11 extends Platform {
30 boolean _atomicCreateFile(File f) throws IOException {
31 throw new Error("FIXME");
33 void _socketHalfClose(Socket s, boolean output) throws IOException {
34 throw new IOException("half closing sockets not supported");
36 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException {
37 if(a.length != 4) throw new UnknownHostException("only ipv4 addrs supported");
38 return InetAddress.getByName(""+(a[0]&0xff)+"."+(a[1]&0xff)+"."+(a[2]&0xff)+"."+(a[3]&0xff));
40 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
41 if(on) throw new SocketException("keepalive not supported");
43 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
44 String[][] zs = new DateFormatSymbols(l).getZoneStrings();
45 String id = tz.getID();
46 for(int i=0;i<zs.length;i++)
47 if(zs[i][0].equals(id))
48 return zs[i][dst ? (showlong ? 3 : 4) : (showlong ? 1 : 2)];
49 StringBuffer sb = new StringBuffer("GMT");
50 int off = tz.getRawOffset() / 1000;
51 if(off < 0) { sb.append("-"); off = -off; }
53 sb.append(off/3600); off = off%3600;
54 if(off > 0) sb.append(":").append(off/60); off=off%60;
55 if(off > 0) sb.append(":").append(off);
60 static class Jdk12 extends Jdk11 {
61 boolean _atomicCreateFile(File f) throws IOException {
62 return f.createNewFile();
65 String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) {
66 return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l);
70 static class Jdk13 extends Jdk12 {
71 void _socketHalfClose(Socket s, boolean output) throws IOException {
72 if(output) s.shutdownOutput();
73 else s.shutdownInput();
76 void _socketSetKeepAlive(Socket s, boolean on) throws SocketException {
81 static class Jdk14 extends Jdk13 {
82 InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); }