X-Git-Url: http://git.megacz.com/?p=nestedvm.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fnestedvm%2Futil%2FPlatform.java;h=8f27af25a7ecd9f1d4d867c6af9fdb9e1c00d5a4;hp=b81e3ac1d9d24589a7ad2eadd86f58efaee86d87;hb=b11e7c6c29f2b5f7b0828bf93eb741c4a30ec411;hpb=85f4875347b68c954691cc2ce84f03215b5293b6 diff --git a/src/org/ibex/nestedvm/util/Platform.java b/src/org/ibex/nestedvm/util/Platform.java index b81e3ac..8f27af2 100644 --- a/src/org/ibex/nestedvm/util/Platform.java +++ b/src/org/ibex/nestedvm/util/Platform.java @@ -1,10 +1,11 @@ // Copyright 2000-2005 the Contributors, as shown in the revision logs. -// Licensed under the Apache Public Source License 2.0 ("the License"). +// Licensed under the Apache 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.*; @@ -58,6 +59,10 @@ public abstract class Platform { 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); } @@ -71,9 +76,11 @@ public abstract class Platform { 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()); } - - 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 void _setFileLength(RandomAccessFile f, int length) + throws IOException; + public static void setFileLength(RandomAccessFile f, int length) + throws IOException { p._setFileLength(f, length); } abstract File[] _listRoots(); public static File[] listRoots() { return p._listRoots(); } @@ -88,6 +95,9 @@ public abstract class Platform { 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"); } @@ -113,7 +123,27 @@ public abstract class Platform { if(off > 0) sb.append(":").append(off); return sb.toString(); } - + + void _setFileLength(RandomAccessFile f, int length) throws IOException{ + InputStream in = new FileInputStream(f.getFD()); + OutputStream out = new FileOutputStream(f.getFD()); + + byte[] buf = new byte[1024]; + for (int len; length > 0; length -= len) { + len = in.read(buf, 0, Math.min(length, buf.length)); + if (len == -1) break; + out.write(buf, 0, len); + } + if (length == 0) return; + + // fill the rest of the space with zeros + for (int i=0; i < buf.length; i++) buf[i] = 0; + while (length > 0) { + out.write(buf, 0, Math.min(length, buf.length)); + length -= buf.length; + } + } + RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException { new FileOutputStream(f).close(); return new RandomAccessFile(f,mode); @@ -162,13 +192,11 @@ public abstract class Platform { String _timeZoneGetDisplayName(TimeZone tz, boolean dst, boolean showlong, Locale l) { return tz.getDisplayName(dst,showlong ? TimeZone.LONG : TimeZone.SHORT, l); } - - RandomAccessFile _truncatedRandomAccessFile(File f, String mode) throws IOException { - RandomAccessFile raf = new RandomAccessFile(f,mode); - raf.setLength(0); - return raf; + + void _setFileLength(RandomAccessFile f, int length) throws IOException { + f.setLength(length); } - + File[] _listRoots() { return File.listRoots(); } } @@ -185,5 +213,29 @@ public abstract class Platform { 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(); } } }