1 26 27 package it.stefanochizzolini.clown.bytes; 28 29 import java.io.RandomAccessFile ; 30 import java.io.EOFException ; 31 import java.io.IOException ; 32 33 36 public final class FileInputStream 37 implements IInputStream 38 { 39 private RandomAccessFile file; 43 45 public FileInputStream( 47 RandomAccessFile file 48 ) 49 { 50 this.file = file; 51 } 52 54 public long getLength( 58 ) 59 { 60 try 61 {return file.length();} 62 catch(IOException e) 63 {throw new RuntimeException (e);} 64 } 65 66 public long getPosition( 67 ) 68 { 69 try 70 {return file.getFilePointer();} 71 catch(IOException e) 72 {throw new RuntimeException (e);} 73 } 74 75 public int hashCode( 76 ) 77 {return file.hashCode();} 78 79 public void read( 80 byte[] data 81 ) 82 throws EOFException  83 { 84 try 85 {file.readFully(data);} 86 catch(EOFException e) 87 {throw e;} 88 catch(IOException e) 89 {throw new RuntimeException (e);} 90 } 91 92 public void read( 93 byte[] data, 94 int offset, 95 int length 96 ) 97 throws EOFException  98 { 99 try 100 {file.readFully(data,offset,length);} 101 catch(EOFException e) 102 {throw e;} 103 catch(IOException e) 104 {throw new RuntimeException (e);} 105 } 106 107 public byte readByte( 108 ) 109 throws EOFException  110 { 111 try 112 {return file.readByte();} 113 catch(EOFException e) 114 {throw e;} 115 catch(IOException e) 116 {throw new RuntimeException (e);} 117 } 118 119 public int readInt( 120 ) 121 throws EOFException  122 { 123 try 124 {return file.readInt();} 125 catch(EOFException e) 126 {throw e;} 127 catch(IOException e) 128 {throw new RuntimeException (e);} 129 } 130 131 public String readLine( 132 ) 133 throws EOFException  134 { 135 try 136 {return file.readLine();} 137 catch(EOFException e) 138 {throw e;} 139 catch(IOException e) 140 {throw new RuntimeException (e);} 141 } 142 143 public short readShort( 144 ) 145 throws EOFException  146 { 147 try 148 {return file.readShort();} 149 catch(EOFException e) 150 {throw e;} 151 catch(IOException e) 152 {throw new RuntimeException (e);} 153 } 154 155 public String readString( 156 int length 157 ) 158 throws EOFException  159 { 160 byte[] data = new byte[length]; 161 try 162 { 163 file.readFully(data); 164 165 return new String (data,"ISO-8859-1"); 166 } 167 catch(EOFException e) 168 {throw e;} 169 catch(Exception e) 170 {throw new RuntimeException (e);} 171 } 172 173 public int readUnsignedByte( 174 ) 175 throws EOFException  176 { 177 try 178 {return file.readUnsignedByte();} 179 catch(EOFException e) 180 {throw e;} 181 catch(IOException e) 182 {throw new RuntimeException (e);} 183 } 184 185 public int readUnsignedShort( 186 ) 187 throws EOFException  188 { 189 try 190 {return file.readUnsignedShort();} 191 catch(EOFException e) 192 {throw e;} 193 catch(IOException e) 194 {throw new RuntimeException (e);} 195 } 196 197 public void seek( 198 long offset 199 ) 200 { 201 try 202 {file.seek(offset);} 203 catch(IOException e) 204 {throw new RuntimeException (e);} 205 } 206 207 public void setPosition( 208 long value 209 ) 210 { 211 try 212 {file.seek(value);} 213 catch(IOException e) 214 {throw new RuntimeException (e);} 215 } 216 217 public void skip( 218 long offset 219 ) 220 { 221 try 222 {file.seek(file.getFilePointer() + offset);} 223 catch(IOException e) 224 {throw new RuntimeException (e);} 225 } 226 227 public byte[] toByteArray( 228 ) 229 { 230 byte[] data = null; 231 try 232 { 233 file.seek(0); 234 data = new byte[(int)file.length()]; 235 file.readFully(data); 236 } 237 catch(IOException e) 238 {throw new RuntimeException (e);} 239 240 return data; 241 } 242 } | Popular Tags |