1 32 package com.imagero.uio; 33 34 import java.io.File ; 35 import java.io.IOException ; 36 import java.io.RandomAccessFile ; 37 38 46 public class OffsetRandomAccessFile extends RandomAccessFile { 47 protected long offset; 48 protected long length; 49 50 public OffsetRandomAccessFile(File file, String mode, long offset) throws IOException { 51 this(file, mode, offset, file.length() - offset); 52 } 53 54 public OffsetRandomAccessFile(File file, String mode, long offset, long length) throws IOException { 55 super(file, mode); 56 this.offset = offset; 57 this.length = length; 58 seek(0); 59 } 60 61 public OffsetRandomAccessFile(String name, String mode, long offset) throws IOException { 62 this(new File (name), mode, offset); 63 } 64 65 public OffsetRandomAccessFile(String name, String mode, long offset, long length) throws IOException { 66 this(new File (name), mode, offset, length); 67 } 68 69 public void seek(long pos) throws IOException { 70 if(pos < 0) { 71 throw new IOException (); 72 } 73 super.seek(pos + offset); 74 } 75 76 public int read() throws IOException { 77 if(getFilePointer() >= length) { 78 return -1; 79 } 80 return super.read(); 81 } 82 83 public long length() throws IOException { 84 return length; 85 } 86 87 public long getFilePointer() throws IOException { 88 return super.getFilePointer() - offset; 89 } 90 91 public int skip(int n) throws IOException { 92 return skipBytes(n); 93 } 94 } 95 | Popular Tags |