Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package de.deaod.wc3doo;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.ByteOrder;
- /**
- * Class to read multi-byte integers and floats from an input Stream
- *
- * @author Deaod <[email protected]>
- * @version 1.0
- * @see InputStream
- */
- public class DataReader {
- private ByteOrder byteOrder = ByteOrder.nativeOrder();
- private InputStream input;
- /**
- * Constructs a reader with the specified input stream as input.
- *
- * @param input the input stream to read from.
- */
- public DataReader(InputStream input) {
- this.input = input;
- }
- /**
- * Constructs a reader with the specified input stream as input and the specified byte order as default byte order.
- *
- * @param input the input stream to read from.
- * @param byteOrder the byte order to use as default.
- */
- public DataReader(InputStream input, ByteOrder byteOrder) {
- this.input = input;
- this.byteOrder = byteOrder;
- }
- //
- /**
- * Returns the default byte order with which to read from the array. After constructing an instance of DataReader,
- * the default byte order is the native one.
- *
- * @return The default byte order with which to read from the array.
- * @see #readShort()
- * @see #readInt()
- * @see #readLong()
- * @see #readFloat()
- * @see #readDouble()
- * @see ByteOrder
- */
- public ByteOrder getByteOrder() {
- return this.byteOrder;
- }
- /**
- * Changes the default byte order to the specified one.
- *
- * @param byteOrder the new default byte order
- * @see #readShort()
- * @see #readInt()
- * @see #readLong()
- * @see #readFloat()
- * @see #readDouble()
- * @see ByteOrder
- */
- public void setByteOrder(ByteOrder byteOrder) {
- this.byteOrder = byteOrder;
- }
- //
- /**
- * Reads an 8-bit integer from the input stream. Blocks until the next byte is available.
- *
- * @return The 8-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public byte readByte() throws IOException {
- int b = this.input.read();
- if (b < 0)
- throw new IOException("Unexpected End of Stream");
- return (byte) b;
- }
- //
- /**
- * Reads a 16-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 16-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public short readShortLE() throws IOException {
- short result = 0;
- result |= (readByte() << 0) & 0x00FF;
- result |= (readByte() << 8) & 0xFF00;
- return result;
- }
- /**
- * Reads a 16-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
- * from the input stream.
- *
- * @return The 16-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public short readShortBE() throws IOException {
- short result = 0;
- result |= (readByte() << 8) & 0xFF00;
- result |= (readByte() << 0) & 0x00FF;
- return result;
- }
- /**
- * Reads a 16-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 16-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- * @see #getByteOrder()
- * @see #setByteOrder(ByteOrder)
- */
- public short readShort() throws IOException {
- if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
- return readShortBE();
- } else {
- return readShortLE();
- }
- }
- //
- /**
- * Reads a 32-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 32-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public int readIntLE() throws IOException {
- int result = 0;
- result |= (readByte() << 0) & 0x000000FF;
- result |= (readByte() << 8) & 0x0000FF00;
- result |= (readByte() << 16) & 0x00FF0000;
- result |= (readByte() << 24) & 0xFF000000;
- return result;
- }
- /**
- * Reads a 32-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
- * from the input stream.
- *
- * @return The 32-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public int readIntBE() throws IOException {
- int result = 0;
- result |= (readByte() << 24) & 0xFF000000;
- result |= (readByte() << 16) & 0x00FF0000;
- result |= (readByte() << 8) & 0x0000FF00;
- result |= (readByte() << 0) & 0x000000FF;
- return result;
- }
- /**
- * Reads a 32-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 32-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- * @see #getByteOrder()
- * @see #setByteOrder(ByteOrder)
- */
- public int readInt() throws IOException {
- if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
- return readIntBE();
- } else {
- return readIntLE();
- }
- }
- //
- /**
- * Reads a 64-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 64-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public long readLongLE() throws IOException {
- long result = 0;
- result |= ((long) readByte() << 0L) & 0x00000000000000FFL;
- result |= ((long) readByte() << 8L) & 0x000000000000FF00L;
- result |= ((long) readByte() << 16L) & 0x0000000000FF0000L;
- result |= ((long) readByte() << 24L) & 0x00000000FF000000L;
- result |= ((long) readByte() << 32L) & 0x000000FF00000000L;
- result |= ((long) readByte() << 40L) & 0x0000FF0000000000L;
- result |= ((long) readByte() << 48L) & 0x00FF000000000000L;
- result |= ((long) readByte() << 56L) & 0xFF00000000000000L;
- return result;
- }
- /**
- * Reads a 64-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
- * from the input stream.
- *
- * @return The 64-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- */
- public long readLongBE() throws IOException {
- long result = 0;
- result |= ((long) readByte() << 56L) & 0xFF00000000000000L;
- result |= ((long) readByte() << 48L) & 0x00FF000000000000L;
- result |= ((long) readByte() << 40L) & 0x0000FF0000000000L;
- result |= ((long) readByte() << 32L) & 0x000000FF00000000L;
- result |= ((long) readByte() << 24L) & 0x00000000FF000000L;
- result |= ((long) readByte() << 16L) & 0x0000000000FF0000L;
- result |= ((long) readByte() << 8L) & 0x000000000000FF00L;
- result |= ((long) readByte() << 0L) & 0x00000000000000FFL;
- return result;
- }
- /**
- * Reads a 64-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
- * read from the input stream.
- *
- * @return The 64-bit integer value read from the input stream.
- * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
- * unexpectedly.
- * @see #getByteOrder()
- * @see #setByteOrder(ByteOrder)
- */
- public long readLong() throws IOException {
- if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
- return readLongBE();
- } else {
- return readLongLE();
- }
- }
- //
- public float readFloatLE() throws IOException {
- return Float.intBitsToFloat(readIntLE());
- }
- public float readFloatBE() throws IOException {
- return Float.intBitsToFloat(readIntBE());
- }
- public float readFloat() throws IOException {
- return Float.intBitsToFloat(readInt());
- }
- //
- public double readDoubleLE() throws IOException {
- return Double.longBitsToDouble(readLongLE());
- }
- public double readDoubleBE() throws IOException {
- return Double.longBitsToDouble(readLongBE());
- }
- public double readDouble() throws IOException {
- return Double.longBitsToDouble(readLong());
- }
- //
- public int read(byte[] destination, int offset, int length) throws IOException {
- return this.input.read(destination, offset, length);
- }
- public int read(byte[] destination) throws IOException {
- return read(destination, 0, destination.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment