X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fnestedvm%2Futil%2FSeekable.java;h=bc21a553a2d315c9771b417483ecf94e20d85a6d;hb=b5282b0a1a8ca5212c75623610be6d0483fd35bd;hp=2e48a7de4b251df9b23f8a09de8712b46a70aa5b;hpb=c59b7cfc7a6b67574d38c5c8eb7732bad37236b0;p=nestedvm.git diff --git a/src/org/ibex/nestedvm/util/Seekable.java b/src/org/ibex/nestedvm/util/Seekable.java index 2e48a7d..bc21a55 100644 --- a/src/org/ibex/nestedvm/util/Seekable.java +++ b/src/org/ibex/nestedvm/util/Seekable.java @@ -1,3 +1,7 @@ +// 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.*; @@ -19,10 +23,10 @@ public abstract class Seekable { public int tryReadFully(byte[] buf, int off, int len) throws IOException { int total = 0; while(len > 0) { - int n = read(buf,off,len); - if(n == -1) break; - off += n; - len -= n; + int n = read(buf,off,len); + if(n == -1) break; + off += n; + len -= n; total += n; } return total == 0 ? -1 : total; @@ -66,15 +70,13 @@ public abstract class Seekable { private final RandomAccessFile raf; public File(String fileName) throws IOException { this(fileName,false); } - public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable); } + public File(String fileName, boolean writable) throws IOException { this(new java.io.File(fileName),writable,false); } - public File(java.io.File file, boolean writable) throws IOException { - raf = new RandomAccessFile(file,writable ? "rw" : "r"); + public File(java.io.File file, boolean writable, boolean truncate) throws IOException { + String mode = writable ? "rw" : "r"; + raf = truncate ? Platform.truncatedRandomAccessFile(file,mode) : new RandomAccessFile(file,mode); } - // NOTE: RandomAccessFile.setLength() is a Java2 function - public void setLength(int n) throws IOException { raf.setLength(n); } - public int read(byte[] buf, int offset, int length) throws IOException { return raf.read(buf,offset,length); } public int write(byte[] buf, int offset, int length) throws IOException { raf.write(buf,offset,length); return length; } public void seek(int pos) throws IOException{ raf.seek(pos); }