wrapper java.io.RandomAccessFile requiredif RFile, RFileSystem {
requires java.io.RFileMap;
state RFile rfile;
helper static void preOpen (RFile rf, boolean exists, String mode) {
if (mode.equals("r")) {
RFileSystem.openRead (rf);
} else if (mode.equals ("rw")) {
RFileSystem.openRead (rf);
if (exists) {
RFileSystem.openOverwrite (rf);
} else {
RFileSystem.openCreate (rf);
}
} else {
// Superclass constructor will throw an IllegalArgumentException
NCheck.fatalBug ("Unrecognized mode.");
}
}
wrapper RandomAccessFile (java.io.File file, String mode) {
RFile trfile;
trfile = RFileMap.lookupAdd (file);
preOpen (trfile, file.exists (), mode);
#;
rfile = trfile;
}
wrapper RandomAccessFile (String name, String mode) {
RFile trfile;
trfile = RFileMap.lookupAdd (name);
if (mode.equals ("rw")) {
File tfile;
tfile = new File (name);
preOpen (trfile, tfile.exists (), mode);
} else {
preOpen (trfile, true, mode);
}
#;
rfile = trfile;
}
helper void preRead (int n) {
RFileSystem.preRead (rfile, n);
}
helper void postRead (int n) {
if (n > 0) {
RFileSystem.postRead (rfile, n);
}
}
wrapper int read() {
preRead (1);
#;
if (result == -1) {
// postRead (0);
} else {
postRead (1);
}
}
wrapper int read (byte b[], int off, int len) {
preRead (len);
#;
postRead (result);
}
wrapper int read (byte b[]) {
preRead (b.length);
#;
postRead (result);
}
wrapper void write (int b) {
RFileSystem.preWrite (rfile, 1);
#;
RFileSystem.postWrite (rfile, 1);
}
helper int min (int v1, int v2) {
if (v1 > v2) {
return v1;
} else {
return v2;
}
}
wrapper void readFully (byte b[]) {
preRead (min ((int) b.length, (int) length ()));
#;
postRead (b.length);
}
wrapper void readFully (byte b[], int off, int len) {
preRead (min (len, (int) length ()));
#;
postRead (len);
}
wrapper int skipBytes (int n) {
#;
}
// public native long getFilePointer() throws IOException
// public native void seek(long pos) throws IOException
// public native long length() throws IOException
// public native void close() throws IOException
wrapper final boolean readBoolean() {
preRead (1);
#;
postRead (1);
}
wrapper byte readByte () {
preRead (1);
#;
postRead (1);
}
wrapper int readUnsignedByte () {
preRead (1);
#;
postRead (1);
}
wrapper short readShort () {
preRead (2);
#;
postRead (2);
}
wrapper int readUnsignedShort() {
preRead (2);
#;
postRead (2);
}
wrapper char readChar() {
preRead (2);
#;
postRead (2);
}
wrapper int readInt () {
preRead (4);
#;
postRead (4);
}
wrapper long readLong() {
preRead (8);
#;
postRead (8);
}
wrapper float readFloat () {
preRead (4);
#;
postRead (4);
}
wrapper double readDouble() {
preRead (8);
#;
postRead (8);
}
wrapper String readLine() {
preRead ((int) (length () - getFilePointer ()));
#;
postRead ((int) (result.length () * 2));
}
wrapper String readUTF() {
preRead ((int) (length () - getFilePointer ()));
#;
postRead ((int) (result.length () * 2));
}
wrapper void writeBoolean (boolean v) {
RFileSystem.preWrite (rfile, 1);
#;
RFileSystem.postWrite (rfile, 1);
}
wrapper void writeByte (int v) {
RFileSystem.preWrite (rfile, 1);
#;
RFileSystem.postWrite (rfile, 1);
}
wrapper void writeShort (int v) {
RFileSystem.preWrite (rfile, 2);
#;
RFileSystem.postWrite (rfile, 2);
}
wrapper void writeChar (int v) {
RFileSystem.preWrite (rfile, 2);
#;
RFileSystem.postWrite (rfile, 2);
}
wrapper void writeInt (int v) {
RFileSystem.preWrite (rfile, 4);
#;
RFileSystem.postWrite (rfile, 4);
}
wrapper void writeLong (long v) {
RFileSystem.preWrite (rfile, 8);
#;
RFileSystem.postWrite (rfile, 8);
}
wrapper void writeFloat(float v) {
RFileSystem.preWrite (rfile, 4);
#;
RFileSystem.postWrite (rfile, 4);
}
wrapper void writeDouble (double v) {
RFileSystem.preWrite (rfile, 8);
#;
RFileSystem.postWrite (rfile, 8);
}
wrapper void writeBytes (String s) {
RFileSystem.preWrite (rfile, s.length ());
#;
RFileSystem.postWrite (rfile, s.length ());
}
wrapper void writeChars (String s) {
RFileSystem.preWrite (rfile, s.length () * 2);
#;
RFileSystem.postWrite (rfile, s.length () * 2);
}
wrapper void writeUTF (String str) {
RFileSystem.preWrite (rfile, str.length () * 2 + 2);
#;
RFileSystem.postWrite (rfile, str.length () * 2 + 2);
}
wrapper void write(byte b[]) {
RFileSystem.preWrite (rfile, (int) b.length);
#;
RFileSystem.postWrite (rfile, (int) b.length);
}
wrapper void write(byte b[], int off, int len) {
RFileSystem.preWrite (rfile, len);
#;
RFileSystem.postWrite (rfile, len);
}
wrapper long length() {
RFileSystem.observeLength (rfile);
#;
}
wrapper void close() {
RFileSystem.close (rfile);
#;
}
wrapper java.io.FileDescriptor getFD () {
#;
RFileMap.addReference (result, rfile);
}
}
Naccio Home Page
University of Virginia, Computer Science