Rev 208 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 208 | chris | 1 | package com.gebauz.Bauzoid.file; |
| 2 | |||
| 3 | import java.io.ByteArrayOutputStream; |
||
| 4 | import java.io.DataInputStream; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.io.InputStream; |
||
| 7 | |||
| 8 | import com.gebauz.Bauzoid.math.Matrix4; |
||
| 9 | import com.gebauz.Bauzoid.math.Vector2; |
||
| 10 | import com.gebauz.Bauzoid.math.Vector3; |
||
| 11 | import com.gebauz.Bauzoid.math.Vector4; |
||
| 12 | |||
| 13 | /** File reader that supports interpreting various data types. |
||
| 14 | * |
||
| 15 | * Extends the functionality of DataInputStream by the following features: |
||
| 16 | * - More data types interpreted from stream |
||
| 17 | * - Endianness handling |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | public class File |
||
| 21 | { |
||
| 22 | private abstract class DataStreamAdapter |
||
| 23 | { |
||
| 24 | public abstract int read(byte[] buffer, int offset, int length) throws IOException; |
||
| 25 | public abstract int read(byte[] buffer) throws IOException; |
||
| 26 | public abstract boolean readBoolean() throws IOException; |
||
| 27 | public abstract byte readByte() throws IOException; |
||
| 28 | public abstract char readChar() throws IOException; |
||
| 29 | public abstract double readDouble() throws IOException; |
||
| 30 | public abstract float readFloat() throws IOException; |
||
| 31 | public abstract void readFully(byte[] dst) throws IOException; |
||
| 32 | public abstract void readFully(byte[] dst, int offset, int byteCount) throws IOException; |
||
| 33 | public abstract int readInt() throws IOException; |
||
| 34 | public abstract String readLine() throws IOException; |
||
| 35 | public abstract long readLong() throws IOException; |
||
| 36 | public abstract short readShort() throws IOException; |
||
| 37 | public abstract String readUTF() throws IOException; |
||
| 38 | public abstract int readUnsignedByte() throws IOException; |
||
| 39 | public abstract int readUnsignedShort() throws IOException; |
||
| 40 | public abstract int skipBytes(int count) throws IOException; |
||
| 41 | }; |
||
| 42 | |||
| 43 | private class LittleEndianAdapter extends DataStreamAdapter |
||
| 44 | { |
||
| 45 | private LittleEndianDataInputStream mIS = null; |
||
| 46 | |||
| 47 | public LittleEndianAdapter(InputStream is) |
||
| 48 | { |
||
| 49 | mIS = new LittleEndianDataInputStream(is); |
||
| 50 | } |
||
| 51 | |||
| 52 | public int read(byte[] buffer, int offset, int length) throws IOException { return mIS.read(buffer, offset, length); } |
||
| 53 | public int read(byte[] buffer) throws IOException { return mIS.read(buffer); } |
||
| 54 | public boolean readBoolean() throws IOException { return mIS.readBoolean(); } |
||
| 55 | public byte readByte() throws IOException { return mIS.readByte(); } |
||
| 56 | public char readChar() throws IOException { return mIS.readChar(); } |
||
| 57 | public double readDouble() throws IOException { return mIS.readDouble(); } |
||
| 58 | public float readFloat() throws IOException { return mIS.readFloat(); } |
||
| 59 | public void readFully(byte[] dst) throws IOException { mIS.readFully(dst); } |
||
| 60 | public void readFully(byte[] dst, int offset, int byteCount) throws IOException { mIS.readFully(dst, offset, byteCount); } |
||
| 61 | public int readInt() throws IOException { return mIS.readInt(); } |
||
| 62 | public String readLine() throws IOException { return mIS.readLine(); } |
||
| 63 | public long readLong() throws IOException { return mIS.readLong(); } |
||
| 64 | public short readShort() throws IOException { return mIS.readShort(); } |
||
| 65 | public String readUTF() throws IOException { return mIS.readUTF(); } |
||
| 66 | public int readUnsignedByte() throws IOException { return mIS.readUnsignedByte(); } |
||
| 67 | public int readUnsignedShort() throws IOException { return mIS.readUnsignedShort(); } |
||
| 68 | public int skipBytes(int count) throws IOException { return mIS.skipBytes(count); } |
||
| 69 | } |
||
| 70 | |||
| 71 | private class BigEndianAdapter extends DataStreamAdapter |
||
| 72 | { |
||
| 73 | private DataInputStream mIS = null; |
||
| 74 | |||
| 75 | public BigEndianAdapter(InputStream is) |
||
| 76 | { |
||
| 77 | mIS = new DataInputStream(is); |
||
| 78 | } |
||
| 79 | |||
| 80 | public int read(byte[] buffer, int offset, int length) throws IOException { return mIS.read(buffer, offset, length); } |
||
| 81 | public int read(byte[] buffer) throws IOException { return mIS.read(buffer); } |
||
| 82 | public boolean readBoolean() throws IOException { return mIS.readBoolean(); } |
||
| 83 | public byte readByte() throws IOException { return mIS.readByte(); } |
||
| 84 | public char readChar() throws IOException { return mIS.readChar(); } |
||
| 85 | public double readDouble() throws IOException { return mIS.readDouble(); } |
||
| 86 | public float readFloat() throws IOException { return mIS.readFloat(); } |
||
| 87 | public void readFully(byte[] dst) throws IOException { mIS.readFully(dst); } |
||
| 88 | public void readFully(byte[] dst, int offset, int byteCount) throws IOException { mIS.readFully(dst, offset, byteCount); } |
||
| 89 | public int readInt() throws IOException { return mIS.readInt(); } |
||
| 221 | chris | 90 | @SuppressWarnings("deprecation") |
| 208 | chris | 91 | public String readLine() throws IOException { return mIS.readLine(); } |
| 92 | public long readLong() throws IOException { return mIS.readLong(); } |
||
| 93 | public short readShort() throws IOException { return mIS.readShort(); } |
||
| 94 | public String readUTF() throws IOException { return mIS.readUTF(); } |
||
| 95 | public int readUnsignedByte() throws IOException { return mIS.readUnsignedByte(); } |
||
| 96 | public int readUnsignedShort() throws IOException { return mIS.readUnsignedShort(); } |
||
| 97 | public int skipBytes(int count) throws IOException { return mIS.skipBytes(count); } |
||
| 98 | } |
||
| 99 | |||
| 100 | private InputStream mInputStream = null; |
||
| 101 | private LittleEndianAdapter mLittleEndian = null; |
||
| 102 | private BigEndianAdapter mBigEndian = null; |
||
| 103 | private DataStreamAdapter mIS = null; |
||
| 104 | private Endianness mEndianness = Endianness.BIG_ENDIAN; |
||
| 105 | |||
| 106 | public enum Endianness |
||
| 107 | { |
||
| 108 | LITTLE_ENDIAN, |
||
| 109 | BIG_ENDIAN |
||
| 110 | }; |
||
| 111 | |||
| 112 | /** Constructor. Takes an InputStream and claims responsibility for closing it. */ |
||
| 113 | public File(InputStream is) |
||
| 114 | { |
||
| 115 | mInputStream = is; |
||
| 116 | |||
| 117 | mLittleEndian = new LittleEndianAdapter(mInputStream); |
||
| 118 | mBigEndian = new BigEndianAdapter(mInputStream); |
||
| 119 | |||
| 120 | mIS = mBigEndian; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** Close the InputStream. */ |
||
| 124 | public void close() throws IOException |
||
| 125 | { |
||
| 126 | if (mInputStream != null) |
||
| 127 | { |
||
| 128 | mInputStream.close(); |
||
| 129 | mInputStream = null; |
||
| 130 | |||
| 131 | mLittleEndian = null; |
||
| 132 | mBigEndian = null; |
||
| 133 | mIS = null; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | public final void setEndianness(Endianness endianness) |
||
| 138 | { |
||
| 139 | mEndianness = endianness; |
||
| 140 | |||
| 141 | switch (endianness) |
||
| 142 | { |
||
| 143 | case LITTLE_ENDIAN: |
||
| 144 | mIS = mLittleEndian; |
||
| 145 | break; |
||
| 146 | case BIG_ENDIAN: |
||
| 147 | mIS = mBigEndian; |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | public final Endianness getEndianness() |
||
| 153 | { |
||
| 154 | return mEndianness; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** Reads n bytes to a String. */ |
||
| 158 | public final String readString(int n) throws IOException |
||
| 159 | { |
||
| 160 | byte[] buffer = new byte[n]; |
||
| 161 | mIS.read(buffer); |
||
| 162 | |||
| 163 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
||
| 164 | os.write(buffer); |
||
| 165 | os.close(); |
||
| 166 | |||
| 167 | return os.toString(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** Reads an int for the length, then interprets the following length bytes as the String. */ |
||
| 171 | public final String readString() throws IOException |
||
| 172 | { |
||
| 173 | int length = mIS.readInt(); |
||
| 174 | |||
| 175 | return readString(length); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** Read a 8-bit C-style bool value. */ |
||
| 179 | public final boolean readBool() throws IOException |
||
| 180 | { |
||
| 181 | byte result = readByte(); |
||
| 182 | return (result != 0); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** Reads 3 float values and returns a Vector2. */ |
||
| 186 | public final Vector2 readVector2() throws IOException |
||
| 187 | { |
||
| 188 | Vector2 result = new Vector2(); |
||
| 189 | result.x = readFloat(); |
||
| 190 | result.y = readFloat(); |
||
| 191 | return result; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** Reads 3 float values and returns a Vector3. */ |
||
| 195 | public final Vector3 readVector3() throws IOException |
||
| 196 | { |
||
| 197 | Vector3 result = new Vector3(); |
||
| 198 | result.x = readFloat(); |
||
| 199 | result.y = readFloat(); |
||
| 200 | result.z = readFloat(); |
||
| 201 | return result; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** Reads 4 float values and returns a Vector4. */ |
||
| 205 | public final Vector4 readVector4() throws IOException |
||
| 206 | { |
||
| 207 | Vector4 result = new Vector4(); |
||
| 208 | result.x = readFloat(); |
||
| 209 | result.y = readFloat(); |
||
| 210 | result.z = readFloat(); |
||
| 211 | result.w = readFloat(); |
||
| 212 | return result; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** Read a 4x4 matrix (16 floats). */ |
||
| 216 | public final Matrix4 readMatrix4() throws IOException |
||
| 217 | { |
||
| 218 | float[] matrix = new float[16]; |
||
| 219 | for (int i = 0; i < 16; i++) |
||
| 220 | { |
||
| 221 | matrix[i] = readFloat(); |
||
| 222 | } |
||
| 223 | |||
| 224 | return new Matrix4(matrix); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** Read a bitmap. */ |
||
| 228 | /* public final Bitmap readBitmap() throws IOException |
||
| 229 | { |
||
| 230 | return BitmapFactory.decodeStream(mInputStream); |
||
| 231 | }*/ |
||
| 232 | |||
| 233 | /** Get the internal input stream. */ |
||
| 234 | public final InputStream getInputStream() |
||
| 235 | { |
||
| 236 | return mInputStream; |
||
| 237 | } |
||
| 238 | |||
| 239 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
| 240 | |||
| 241 | public final int read(byte[] buffer, int offset, int length) throws IOException |
||
| 242 | { |
||
| 243 | return mIS.read(buffer, offset, length); |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** Equivalent to read(buffer, 0, buffer.length).*/ |
||
| 248 | public final int read(byte[] buffer) throws IOException |
||
| 249 | { |
||
| 250 | return mIS.read(buffer); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** Reads a Java boolean (32-bit single, 8-bit in arrays). */ |
||
| 254 | public final boolean readBoolean() throws IOException |
||
| 255 | { |
||
| 256 | return mIS.readBoolean(); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** Reads an 8-bit byte. */ |
||
| 260 | public final byte readByte() throws IOException |
||
| 261 | { |
||
| 262 | return mIS.readByte(); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** Reads a big-endian 16-bit character value. */ |
||
| 266 | public final char readChar() throws IOException |
||
| 267 | { |
||
| 268 | return mIS.readChar(); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** Reads a big-endian 64-bit double value. */ |
||
| 272 | public final double readDouble() throws IOException |
||
| 273 | { |
||
| 274 | return mIS.readDouble(); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** Reads a big-endian 32-bit float value. */ |
||
| 278 | public final float readFloat() throws IOException |
||
| 279 | { |
||
| 280 | return mIS.readFloat(); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** Equivalent to readFully(dst, 0, dst.length);. */ |
||
| 284 | public final void readFully(byte[] dst) throws IOException |
||
| 285 | { |
||
| 286 | mIS.readFully(dst); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** Reads byteCount bytes from this stream and stores them in the byte array dst starting at offset. */ |
||
| 290 | public final void readFully(byte[] dst, int offset, int byteCount) throws IOException |
||
| 291 | { |
||
| 292 | mIS.readFully(dst, offset, byteCount); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** Reads a big-endian 32-bit integer value. */ |
||
| 296 | public final int readInt() throws IOException |
||
| 297 | { |
||
| 298 | return mIS.readInt(); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** Returns a string containing the next line of text available from this stream. */ |
||
| 302 | public final String readLine() throws IOException |
||
| 303 | { |
||
| 304 | return mIS.readLine(); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** Reads a big-endian 64-bit long value. */ |
||
| 308 | public final long readLong() throws IOException |
||
| 309 | { |
||
| 310 | return mIS.readLong(); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** Reads a big-endian 16-bit short value. */ |
||
| 314 | public final short readShort() throws IOException |
||
| 315 | { |
||
| 316 | return mIS.readShort(); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** Reads a string encoded with modified UTF-8. */ |
||
| 320 | public final String readUTF() throws IOException |
||
| 321 | { |
||
| 322 | return mIS.readUTF(); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** Reads an unsigned 8-bit byte value and returns it as an int. */ |
||
| 326 | public final int readUnsignedByte() throws IOException |
||
| 327 | { |
||
| 328 | return mIS.readUnsignedByte(); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** Reads a big-endian 16-bit unsigned short value and returns it as an int. */ |
||
| 332 | public final int readUnsignedShort() throws IOException |
||
| 333 | { |
||
| 334 | return mIS.readUnsignedShort(); |
||
| 335 | } |
||
| 336 | |||
| 337 | /** Skips count number of bytes in this stream. */ |
||
| 338 | public final int skipBytes(int count) throws IOException |
||
| 339 | { |
||
| 340 | return mIS.skipBytes(count); |
||
| 341 | } |
||
| 342 | |||
| 343 | } |