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