+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
package org.ibex.nestedvm.util;
import java.io.*;
+import java.nio.channels.*;
import java.net.*;
import java.util.*;
abstract boolean _atomicCreateFile(File f) throws IOException;
public static boolean atomicCreateFile(File f) throws IOException { return p._atomicCreateFile(f); }
+
+ abstract Seekable.Lock _lockFile(Seekable s, RandomAccessFile raf, long pos, long size, boolean shared) throws IOException;
+ public static Seekable.Lock lockFile(Seekable s, RandomAccessFile raf, long pos, long size, boolean shared) throws IOException {
+ return p._lockFile(s, raf, pos, size, shared); }
abstract void _socketHalfClose(Socket s, boolean output) throws IOException;
public static void socketHalfClose(Socket s, boolean output) throws IOException { p._socketHalfClose(s,output); }
abstract RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException;
public static RandomAccessFile truncatedRandomAccessFile(File f, String mode) throws IOException { return p._truncatedRandomAccessFile(f,mode); }
+ abstract File[] _listRoots();
+ public static File[] listRoots() { return p._listRoots(); }
+
+ abstract File _getRoot(File f);
+ public static File getRoot(File f) { return p._getRoot(f); }
+
static class Jdk11 extends Platform {
boolean _atomicCreateFile(File f) throws IOException {
// This is not atomic, but its the best we can do on jdk 1.1
new FileOutputStream(f).close();
return true;
}
+ Seekable.Lock _lockFile(Seekable s, RandomAccessFile raf, long p, long size, boolean shared) throws IOException {
+ throw new IOException("file locking requires jdk 1.4+");
+ }
void _socketHalfClose(Socket s, boolean output) throws IOException {
throw new IOException("half closing sockets not supported");
}
new FileOutputStream(f).close();
return new RandomAccessFile(f,mode);
}
+
+ File[] _listRoots() {
+ String[] rootProps = new String[]{"java.home","java.class.path","java.library.path","java.io.tmpdir","java.ext.dirs","user.home","user.dir" };
+ Hashtable known = new Hashtable();
+ for(int i=0;i<rootProps.length;i++) {
+ String prop = getProperty(rootProps[i]);
+ if(prop == null) continue;
+ for(;;) {
+ String path = prop;
+ int p;
+ if((p = prop.indexOf(File.pathSeparatorChar)) != -1) {
+ path = prop.substring(0,p);
+ prop = prop.substring(p+1);
+ }
+ File root = getRoot(new File(path));
+ //System.err.println(rootProps[i] + ": " + path + " -> " + root);
+ known.put(root,Boolean.TRUE);
+ if(p == -1) break;
+ }
+ }
+ File[] ret = new File[known.size()];
+ int i=0;
+ for(Enumeration e = known.keys();e.hasMoreElements();)
+ ret[i++] = (File) e.nextElement();
+ return ret;
+ }
+
+ File _getRoot(File f) {
+ if(!f.isAbsolute()) f = new File(f.getAbsolutePath());
+ String p;
+ while((p = f.getParent()) != null) f = new File(p);
+ if(f.getPath().length() == 0) f = new File("/"); // work around a classpath bug
+ return f;
+ }
}
static class Jdk12 extends Jdk11 {
RandomAccessFile raf = new RandomAccessFile(f,mode);
raf.setLength(0);
return raf;
- }
+ }
+
+ File[] _listRoots() { return File.listRoots(); }
}
static class Jdk13 extends Jdk12 {
static class Jdk14 extends Jdk13 {
InetAddress _inetAddressFromBytes(byte[] a) throws UnknownHostException { return InetAddress.getByAddress(a); }
+
+ Seekable.Lock _lockFile(Seekable s, RandomAccessFile r, long pos, long size, boolean shared) throws IOException {
+ FileLock flock;
+ try {
+ flock = pos == 0 && size == 0 ? r.getChannel().lock() :
+ r.getChannel().tryLock(pos, size, shared);
+ } catch (OverlappingFileLockException e) { flock = null; }
+ if (flock == null) return null; // region already locked
+ return new Jdk14FileLock(s, flock);
+ }
+ }
+
+ private static final class Jdk14FileLock extends Seekable.Lock {
+ private final Seekable s;
+ private final FileLock l;
+
+ Jdk14FileLock(Seekable sk, FileLock flock) { s = sk; l = flock; }
+ public Seekable seekable() { return s; }
+ public boolean isShared() { return l.isShared(); }
+ public boolean isValid() { return l.isValid(); }
+ public void release() throws IOException { l.release(); }
+ public long position() { return l.position(); }
+ public long size() { return l.size(); }
+ public String toString() { return l.toString(); }
}
}