1 29 package net.metanotion.io; 30 31 import java.io.DataInput ; 32 import java.io.DataOutput ; 33 import java.io.File ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.RandomAccessFile ; 37 38 public class RAIFile implements RandomAccessInterface, DataInput , DataOutput { 39 private File f; 40 private RandomAccessFile delegate; 41 private boolean r=false, w=false; 42 43 public RAIFile(RandomAccessFile file) throws FileNotFoundException { 44 this.f = null; 45 this.delegate = file; 46 } 47 48 public RAIFile(File file, boolean read, boolean write) throws FileNotFoundException { 49 this.f = file; 50 this.r = read; 51 this.w = write; 52 String mode = ""; 53 if(this.r) { mode += "r"; } 54 if(this.w) { mode += "w"; } 55 this.delegate = new RandomAccessFile (file, mode); 56 } 57 58 public long getFilePointer() throws IOException { return delegate.getFilePointer(); } 59 public long length() throws IOException { return delegate.length(); } 60 public int read() throws IOException { return delegate.read(); } 61 public int read(byte[] b) throws IOException { return delegate.read(b); } 62 public int read(byte[] b, int off, int len) throws IOException { return delegate.read(b,off,len); } 63 public void seek(long pos) throws IOException { delegate.seek(pos); } 64 public void setLength(long newLength) throws IOException { delegate.setLength(newLength); } 65 66 public void close() throws IOException { delegate.close(); } 69 70 public boolean readBoolean() throws IOException { return delegate.readBoolean(); } 72 public byte readByte() throws IOException { return delegate.readByte(); } 73 public char readChar() throws IOException { return delegate.readChar(); } 74 public double readDouble() throws IOException { return delegate.readDouble(); } 75 public float readFloat() throws IOException { return delegate.readFloat(); } 76 public void readFully(byte[] b) throws IOException { delegate.readFully(b); } 77 public void readFully(byte[] b, int off, int len) throws IOException { delegate.readFully(b,off,len); } 78 public int readInt() throws IOException { return delegate.readInt(); } 79 public String readLine() throws IOException { return delegate.readLine(); } 80 public long readLong() throws IOException { return delegate.readLong(); } 81 public short readShort() throws IOException { return delegate.readShort(); } 82 public int readUnsignedByte() throws IOException { return delegate.readUnsignedByte(); } 83 public int readUnsignedShort() throws IOException { return delegate.readUnsignedShort(); } 84 85 94 public String readUTF() throws IOException { 95 int len = delegate.readInt(); 96 if((len < 0) || (len >= 16777216)) { throw new IOException ("Bad Length Encoding"); } 97 byte[] bytes = new byte[len]; 98 int l = delegate.read(bytes); 99 if(l==-1) { throw new IOException ("EOF while reading String"); } 100 String s = new String (bytes, "UTF-8"); 101 return s; 102 } 103 104 public int skipBytes(int n) throws IOException { return delegate.skipBytes(n); } 105 106 public void write(int b) throws IOException { delegate.write(b); } 108 public void write(byte[] b) throws IOException { delegate.write(b); } 109 public void write(byte[] b, int off, int len) throws IOException { delegate.write(b,off,len); } 110 public void writeBoolean(boolean v) throws IOException { delegate.writeBoolean(v); } 111 public void writeByte(int v) throws IOException { delegate.writeByte(v); } 112 public void writeShort(int v) throws IOException { delegate.writeShort(v); } 113 public void writeChar(int v) throws IOException { delegate.writeChar(v); } 114 public void writeInt(int v) throws IOException { delegate.writeInt(v); } 115 public void writeLong(long v) throws IOException { delegate.writeLong(v); } 116 public void writeFloat(float v) throws IOException { delegate.writeFloat(v); } 117 public void writeDouble(double v) throws IOException { delegate.writeDouble(v); } 118 public void writeBytes(String s) throws IOException { delegate.writeBytes(s); } 119 public void writeChars(String s) throws IOException { delegate.writeChars(s); } 120 121 130 public void writeUTF(String str) throws IOException { 131 byte[] string = str.getBytes("UTF-8"); 132 if(string.length >= 16777216) { throw new IOException ("String to long for encoding type"); } 133 delegate.writeInt(string.length); 134 delegate.write(string); 135 } 136 } 137 | Popular Tags |