Chiddix

InputStream

Sep 22nd, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package buffer;
  2.  
  3. public final class InputStream extends Stream {
  4.  
  5.     public InputStream(final byte[] buffer) {
  6.         super(buffer);
  7.     }
  8.  
  9.     private byte get(final int index) {
  10.         return buffer[index];
  11.     }
  12.  
  13.     public byte get() {
  14.         return get(index++);
  15.     }
  16.  
  17.     public short getShort(final boolean bigEndian) {
  18.         if (bigEndian) {
  19.             return (short) ((get(index) << 8) + (get(++index) & 0xff));
  20.         }
  21.         return (short) ((get(index) & 0xff) + (get(++index) << 8));
  22.     }
  23.  
  24.     public short getShort() {
  25.         return getShort(true);
  26.     }
  27.  
  28.     public int getInt(final boolean bigEndian) {
  29.         if (bigEndian) {
  30.             return (get() << 24) + ((get() & 0xff) << 16) + ((get() & 0xff) << 8) + (get() & 0xff);
  31.         }
  32.         return (get() & 0xff) + ((get() & 0xff) << 8) + ((get() & 0xff) << 16) + (get() << 24);
  33.     }
  34.  
  35.     public int getInt() {
  36.         return getInt(true);
  37.     }
  38.  
  39.     public long getLong(final boolean bigEndian) {
  40.         if (bigEndian) {
  41.             return ((long) get() << 56) + ((get() & 0xffL) << 48) + ((get() & 0xffL) << 40) + ((get() & 0xffL) << 32) + ((get() & 0xffL) << 24) + ((get() & 0xff) << 16)
  42.                     + ((get() & 0xff) << 8) + (get() & 0xff);
  43.         }
  44.         return (get() & 0xff) + ((get() & 0xff) << 8) + ((get() & 0xff) << 16) + ((get() & 0xffL) << 24) + ((get() & 0xffL) << 32) + ((get() & 0xffL) << 40)
  45.                 + ((get() & 0xffL) << 48) + ((long) get() << 56);
  46.     }
  47.  
  48.     public long getLong() {
  49.         return getLong(true);
  50.     }
  51. }
Add Comment
Please, Sign In to add comment