Guest User

DataReader.java

a guest
Jul 16th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.25 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package de.deaod.wc3doo;
  5.  
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.nio.ByteOrder;
  9.  
  10. /**
  11.  * Class to read multi-byte integers and floats from an input Stream
  12.  *
  13.  * @author Deaod <[email protected]>
  14.  * @version 1.0
  15.  * @see InputStream
  16.  */
  17. public class DataReader {
  18.     private ByteOrder   byteOrder = ByteOrder.nativeOrder();
  19.     private InputStream input;
  20.    
  21.     /**
  22.      * Constructs a reader with the specified input stream as input.
  23.      *
  24.      * @param input the input stream to read from.
  25.      */
  26.     public DataReader(InputStream input) {
  27.         this.input = input;
  28.     }
  29.    
  30.     /**
  31.      * Constructs a reader with the specified input stream as input and the specified byte order as default byte order.
  32.      *
  33.      * @param input the input stream to read from.
  34.      * @param byteOrder the byte order to use as default.
  35.      */
  36.     public DataReader(InputStream input, ByteOrder byteOrder) {
  37.         this.input = input;
  38.         this.byteOrder = byteOrder;
  39.     }
  40.    
  41.     //
  42.    
  43.     /**
  44.      * Returns the default byte order with which to read from the array. After constructing an instance of DataReader,
  45.      * the default byte order is the native one.
  46.      *
  47.      * @return The default byte order with which to read from the array.
  48.      * @see #readShort()
  49.      * @see #readInt()
  50.      * @see #readLong()
  51.      * @see #readFloat()
  52.      * @see #readDouble()
  53.      * @see ByteOrder
  54.      */
  55.     public ByteOrder getByteOrder() {
  56.         return this.byteOrder;
  57.     }
  58.    
  59.     /**
  60.      * Changes the default byte order to the specified one.
  61.      *
  62.      * @param byteOrder the new default byte order
  63.      * @see #readShort()
  64.      * @see #readInt()
  65.      * @see #readLong()
  66.      * @see #readFloat()
  67.      * @see #readDouble()
  68.      * @see ByteOrder
  69.      */
  70.     public void setByteOrder(ByteOrder byteOrder) {
  71.         this.byteOrder = byteOrder;
  72.     }
  73.    
  74.     //
  75.    
  76.     /**
  77.      * Reads an 8-bit integer from the input stream. Blocks until the next byte is available.
  78.      *
  79.      * @return The 8-bit integer value read from the input stream.
  80.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  81.      *             unexpectedly.
  82.      */
  83.     public byte readByte() throws IOException {
  84.         int b = this.input.read();
  85.         if (b < 0)
  86.             throw new IOException("Unexpected End of Stream");
  87.         return (byte) b;
  88.     }
  89.    
  90.     //
  91.    
  92.     /**
  93.      * Reads a 16-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
  94.      * read from the input stream.
  95.      *
  96.      * @return The 16-bit integer value read from the input stream.
  97.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  98.      *             unexpectedly.
  99.      */
  100.     public short readShortLE() throws IOException {
  101.         short result = 0;
  102.         result |= (readByte() << 0) & 0x00FF;
  103.         result |= (readByte() << 8) & 0xFF00;
  104.         return result;
  105.     }
  106.    
  107.     /**
  108.      * Reads a 16-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
  109.      * from the input stream.
  110.      *
  111.      * @return The 16-bit integer value read from the input stream.
  112.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  113.      *             unexpectedly.
  114.      */
  115.     public short readShortBE() throws IOException {
  116.         short result = 0;
  117.         result |= (readByte() << 8) & 0xFF00;
  118.         result |= (readByte() << 0) & 0x00FF;
  119.         return result;
  120.     }
  121.    
  122.     /**
  123.      * Reads a 16-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
  124.      * read from the input stream.
  125.      *
  126.      * @return The 16-bit integer value read from the input stream.
  127.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  128.      *             unexpectedly.
  129.      * @see #getByteOrder()
  130.      * @see #setByteOrder(ByteOrder)
  131.      */
  132.     public short readShort() throws IOException {
  133.         if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
  134.             return readShortBE();
  135.         } else {
  136.             return readShortLE();
  137.         }
  138.     }
  139.    
  140.     //
  141.    
  142.     /**
  143.      * Reads a 32-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
  144.      * read from the input stream.
  145.      *
  146.      * @return The 32-bit integer value read from the input stream.
  147.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  148.      *             unexpectedly.
  149.      */
  150.     public int readIntLE() throws IOException {
  151.         int result = 0;
  152.         result |= (readByte() << 0) & 0x000000FF;
  153.         result |= (readByte() << 8) & 0x0000FF00;
  154.         result |= (readByte() << 16) & 0x00FF0000;
  155.         result |= (readByte() << 24) & 0xFF000000;
  156.         return result;
  157.     }
  158.    
  159.     /**
  160.      * Reads a 32-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
  161.      * from the input stream.
  162.      *
  163.      * @return The 32-bit integer value read from the input stream.
  164.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  165.      *             unexpectedly.
  166.      */
  167.     public int readIntBE() throws IOException {
  168.         int result = 0;
  169.         result |= (readByte() << 24) & 0xFF000000;
  170.         result |= (readByte() << 16) & 0x00FF0000;
  171.         result |= (readByte() << 8) & 0x0000FF00;
  172.         result |= (readByte() << 0) & 0x000000FF;
  173.         return result;
  174.     }
  175.    
  176.     /**
  177.      * Reads a 32-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
  178.      * read from the input stream.
  179.      *
  180.      * @return The 32-bit integer value read from the input stream.
  181.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  182.      *             unexpectedly.
  183.      * @see #getByteOrder()
  184.      * @see #setByteOrder(ByteOrder)
  185.      */
  186.     public int readInt() throws IOException {
  187.         if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
  188.             return readIntBE();
  189.         } else {
  190.             return readIntLE();
  191.         }
  192.     }
  193.    
  194.     //
  195.    
  196.     /**
  197.      * Reads a 64-bit integer from the input stream with Little Endian byte order. Blocks until enough bytes have been
  198.      * read from the input stream.
  199.      *
  200.      * @return The 64-bit integer value read from the input stream.
  201.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  202.      *             unexpectedly.
  203.      */
  204.     public long readLongLE() throws IOException {
  205.         long result = 0;
  206.         result |= ((long) readByte() << 0L) & 0x00000000000000FFL;
  207.         result |= ((long) readByte() << 8L) & 0x000000000000FF00L;
  208.         result |= ((long) readByte() << 16L) & 0x0000000000FF0000L;
  209.         result |= ((long) readByte() << 24L) & 0x00000000FF000000L;
  210.         result |= ((long) readByte() << 32L) & 0x000000FF00000000L;
  211.         result |= ((long) readByte() << 40L) & 0x0000FF0000000000L;
  212.         result |= ((long) readByte() << 48L) & 0x00FF000000000000L;
  213.         result |= ((long) readByte() << 56L) & 0xFF00000000000000L;
  214.         return result;
  215.     }
  216.    
  217.     /**
  218.      * Reads a 64-bit integer from the input stream with Big Endian byte order. Blocks until enough bytes have been read
  219.      * from the input stream.
  220.      *
  221.      * @return The 64-bit integer value read from the input stream.
  222.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  223.      *             unexpectedly.
  224.      */
  225.     public long readLongBE() throws IOException {
  226.         long result = 0;
  227.         result |= ((long) readByte() << 56L) & 0xFF00000000000000L;
  228.         result |= ((long) readByte() << 48L) & 0x00FF000000000000L;
  229.         result |= ((long) readByte() << 40L) & 0x0000FF0000000000L;
  230.         result |= ((long) readByte() << 32L) & 0x000000FF00000000L;
  231.         result |= ((long) readByte() << 24L) & 0x00000000FF000000L;
  232.         result |= ((long) readByte() << 16L) & 0x0000000000FF0000L;
  233.         result |= ((long) readByte() << 8L) & 0x000000000000FF00L;
  234.         result |= ((long) readByte() << 0L) & 0x00000000000000FFL;
  235.         return result;
  236.     }
  237.    
  238.     /**
  239.      * Reads a 64-bit integer from the input stream with the default byte order. Blocks until enough bytes have been
  240.      * read from the input stream.
  241.      *
  242.      * @return The 64-bit integer value read from the input stream.
  243.      * @throws IOException if the underlying input stream throws an exception or the end of the input stream is reached
  244.      *             unexpectedly.
  245.      * @see #getByteOrder()
  246.      * @see #setByteOrder(ByteOrder)
  247.      */
  248.     public long readLong() throws IOException {
  249.         if (this.byteOrder == ByteOrder.BIG_ENDIAN) {
  250.             return readLongBE();
  251.         } else {
  252.             return readLongLE();
  253.         }
  254.     }
  255.    
  256.     //
  257.    
  258.     public float readFloatLE() throws IOException {
  259.         return Float.intBitsToFloat(readIntLE());
  260.     }
  261.    
  262.     public float readFloatBE() throws IOException {
  263.         return Float.intBitsToFloat(readIntBE());
  264.     }
  265.    
  266.     public float readFloat() throws IOException {
  267.         return Float.intBitsToFloat(readInt());
  268.     }
  269.    
  270.     //
  271.    
  272.     public double readDoubleLE() throws IOException {
  273.         return Double.longBitsToDouble(readLongLE());
  274.     }
  275.    
  276.     public double readDoubleBE() throws IOException {
  277.         return Double.longBitsToDouble(readLongBE());
  278.     }
  279.    
  280.     public double readDouble() throws IOException {
  281.         return Double.longBitsToDouble(readLong());
  282.     }
  283.    
  284.     //
  285.    
  286.     public int read(byte[] destination, int offset, int length) throws IOException {
  287.         return this.input.read(destination, offset, length);
  288.     }
  289.    
  290.     public int read(byte[] destination) throws IOException {
  291.         return read(destination, 0, destination.length);
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment