1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.ipack.util;
  27 
  28 import java.io.DataInput;
  29 import java.io.EOFException;
  30 import java.io.FilterInputStream;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 
  34 public final class LsbDataInputStream extends FilterInputStream
  35                                       implements DataInput {
  36     public LsbDataInputStream(final InputStream is) {
  37         super(is);
  38     }
  39 
  40     @Override
  41     public void readFully(final byte[] buffer) throws IOException {
  42         readFully(buffer, 0, buffer.length);
  43     }
  44 
  45     @Override
  46     public void readFully(final byte[] buffer,
  47                           final int offset,
  48                           final int length) throws IOException {
  49         int wrpos = offset;
  50         int remaining = length;
  51         while (remaining > 0) {
  52             final int read = in.read(buffer, wrpos, remaining);
  53             if (read == -1) {
  54                 throw new EOFException("No more data");
  55             }
  56 
  57             wrpos += read;
  58             remaining -= read;
  59         }
  60     }
  61 
  62     @Override
  63     public int skipBytes(int n) throws IOException {
  64         throw new UnsupportedOperationException("Not supported yet.");
  65     }
  66 
  67     @Override
  68     public boolean readBoolean() throws IOException {
  69         throw new UnsupportedOperationException("Not supported yet.");
  70     }
  71 
  72     @Override
  73     public byte readByte() throws IOException {
  74         final int value = in.read();
  75         if (value == -1) {
  76             throw new EOFException("No more data");
  77         }
  78         return (byte) value;
  79     }
  80 
  81     @Override
  82     public int readUnsignedByte() throws IOException {
  83         throw new UnsupportedOperationException("Not supported yet.");
  84     }
  85 
  86     @Override
  87     public short readShort() throws IOException {
  88         return (short) ((readByte() & 0xff) | (readByte() << 8));
  89     }
  90 
  91     @Override
  92     public int readUnsignedShort() throws IOException {
  93         throw new UnsupportedOperationException("Not supported yet.");
  94     }
  95 
  96     @Override
  97     public char readChar() throws IOException {
  98         throw new UnsupportedOperationException("Not supported yet.");
  99     }
 100 
 101     @Override
 102     public int readInt() throws IOException {
 103         return ((readByte() & 0xff) | ((readByte() & 0xff) << 8)
 104                                     | ((readByte() & 0xff) << 16)
 105                                     | ((readByte() << 24)));
 106     }
 107 
 108     @Override
 109     public long readLong() throws IOException {
 110         throw new UnsupportedOperationException("Not supported yet.");
 111     }
 112 
 113     @Override
 114     public float readFloat() throws IOException {
 115         throw new UnsupportedOperationException("Not supported yet.");
 116     }
 117 
 118     @Override
 119     public double readDouble() throws IOException {
 120         throw new UnsupportedOperationException("Not supported yet.");
 121     }
 122 
 123     @Override
 124     public String readLine() throws IOException {
 125         throw new UnsupportedOperationException("Not supported yet.");
 126     }
 127 
 128     @Override
 129     public String readUTF() throws IOException {
 130         throw new UnsupportedOperationException("Not supported yet.");
 131     }
 132 }