| 15 | import java.nio.channels.FileChannel; |
| 16 | |
| 17 | public class RandomAccessFile { |
| 18 | private long peer; |
| 19 | private File file; |
| 20 | private long position = 0; |
| 21 | private long length; |
| 22 | private boolean allowWrite; |
| 23 | |
| 24 | public RandomAccessFile(String name, String mode) |
| 25 | throws FileNotFoundException |
| 26 | { |
| 27 | this(new File(name), mode); |
| 28 | } |
| 29 | |
| 30 | public RandomAccessFile(File file, String mode) |
| 31 | throws FileNotFoundException |
| 32 | { |
| 33 | if (file == null) throw new NullPointerException(); |
| 34 | if (mode.equals("rw")) allowWrite = true; |
| 35 | else if (! mode.equals("r")) throw new IllegalArgumentException(); |
| 36 | this.file = file; |
| 37 | open(); |
| 38 | } |
| 39 | |
| 40 | private void open() throws FileNotFoundException { |
| 41 | long[] result = new long[2]; |
| 42 | open(file.getPath(), allowWrite, result); |
| 43 | peer = result[0]; |
| 44 | length = result[1]; |
| 45 | } |
| 46 | |
| 47 | private static native void open(String name, boolean allowWrite, long[] result) |
| 48 | throws FileNotFoundException; |
| 49 | |
| 50 | private void refresh() throws IOException { |
| 51 | if (file.length() != length) { |
| 52 | close(); |
| 53 | open(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public long length() throws IOException { |
| 58 | refresh(); |
| 59 | return length; |
| 60 | } |
| 61 | |
| 62 | public long getFilePointer() throws IOException { |
| 63 | return position; |
| 64 | } |
| 65 | |
| 66 | public void seek(long position) throws IOException { |
| 67 | if (position < 0 || (!allowWrite && position > length())) throw new IOException(); |
| 68 | |
| 69 | this.position = position; |
| 70 | } |
| 71 | |
| 72 | public int skipBytes(int count) throws IOException { |
| 73 | if (position + count > length()) throw new IOException(); |
| 74 | this.position = position + count; |
nothing calls this directly
no outgoing calls
no test coverage detected