1 32 package com.imagero.uio; 33 34 import java.io.EOFException ; 35 import java.io.IOException ; 36 import java.io.RandomAccessFile ; 37 import java.lang.reflect.Method ; 38 39 49 public class RandomAccessFileWrapper extends AbstractRandomAccess { 50 51 RandomAccessFile in; 52 53 public RandomAccessFileWrapper(RandomAccessFile in, int byteOrder) throws IOException { 54 this.in = in; 55 _setByteOrder(byteOrder); 56 } 57 58 protected int _read() throws IOException { 59 int i = in.read(); 60 if(i < 0) { 61 throw new EOFException (); 62 } 63 return i; 64 } 65 66 public void write(int b) throws IOException { 67 in.write(b); 68 } 69 70 public void write(byte b[]) throws IOException { 71 in.write(b); 72 } 73 74 public void write(byte b[], int off, int len) throws IOException { 75 in.write(b, off, len); 76 } 77 78 public long getFilePointer() throws IOException { 79 return in.getFilePointer(); 80 } 81 82 public long length() throws IOException { 83 return in.length(); 84 } 85 86 public void seek(long offset) throws IOException { 87 in.seek(offset); 88 } 89 90 public int read() throws IOException { 91 return in.read(); 92 } 93 94 public int skip(int n) throws IOException { 95 return in.skipBytes(n); 96 } 97 98 public void close() throws IOException { 99 in.close(); 100 } 101 102 public int read(byte[] b) throws IOException { 103 return read(b, 0, b.length); 104 } 105 106 public int read(byte[] b, int off, int len) throws IOException { 107 return in.read(b, off, len); 108 } 109 110 118 public void setLength(long newLength) throws IOException { 119 try { 120 Class aClass = Class.forName("java.io.RandomAccessFile"); 121 Method method = aClass.getMethod("setLength", new Class []{Long .class}); 122 method.invoke(in, new Object []{new Long (newLength)}); 123 } 124 catch(Exception ex) { 125 if(newLength > in.length()) { 126 long pos = getFilePointer(); 127 seek(newLength); 128 write(0); 129 seek(pos); 130 } 131 } 132 } 133 } 134 | Popular Tags |