1 6 21 22 package de.schlichtherle.io.rof; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.RandomAccessFile ; 28 29 41 public class FastReadOnlyFile 42 extends RandomAccessFile 43 implements ReadOnlyFile 44 { 45 46 private long fp; 47 48 private boolean closed; 49 50 public FastReadOnlyFile(File file) 51 throws FileNotFoundException { 52 super(file, "r"); 53 } 54 55 public void seek(long pos) throws IOException { 56 if (pos == fp) { 57 if (closed) 58 throw new IOException ("File has been closed!"); 59 60 return; 61 } 62 63 super.seek(pos); 64 fp = pos; 65 } 66 67 public int read() throws IOException { 68 final int ret = super.read(); 69 if (ret != -1) 70 fp++; 71 return ret; 72 } 73 74 public final int read(byte b[]) throws IOException { 75 return read(b, 0, b.length); 76 } 77 78 public int read(byte[] b, int off, int len) throws IOException { 79 final int ret = super.read(b, off, len); 80 if (ret != -1) 81 fp += ret; 82 return ret; 83 } 84 85 public int skipBytes(int n) throws IOException { 86 final int ret = super.skipBytes(n); 87 fp += ret; 88 return ret; 89 } 90 91 public void close() throws IOException { 92 super.close(); 93 closed = true; 94 } 95 } 96 | Popular Tags |