| /* |
| * Copyright (C) 2007, 2018 Google Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.eclipse.apogy.common.lang.java.io; |
| |
| import java.io.DataInput; |
| import java.io.DataInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| |
| public class LittleEndianDataInputStream extends InputStream implements DataInput { |
| |
| public LittleEndianDataInputStream(InputStream in) { |
| this.in = in; |
| this.d = new DataInputStream(in); |
| this.w = new byte[8]; |
| } |
| |
| @Override |
| public int available() throws IOException { |
| return this.d.available(); |
| } |
| |
| @Override |
| public final short readShort() throws IOException { |
| this.d.readFully(this.w, 0, 2); |
| return (short) ((this.w[1] & 0xff) << 8 | (this.w[0] & 0xff)); |
| } |
| |
| /** |
| * Note, returns int even though it reads a short. |
| */ |
| @Override |
| public final int readUnsignedShort() throws IOException { |
| this.d.readFully(this.w, 0, 2); |
| return ((this.w[1] & 0xff) << 8 | (this.w[0] & 0xff)); |
| } |
| |
| /** |
| * like DataInputStream.readChar except little endian. |
| */ |
| @Override |
| public final char readChar() throws IOException { |
| this.d.readFully(this.w, 0, 2); |
| return (char) ((this.w[1] & 0xff) << 8 | (this.w[0] & 0xff)); |
| } |
| |
| /** |
| * like DataInputStream.readInt except little endian. |
| */ |
| @Override |
| public final int readInt() throws IOException { |
| this.d.readFully(this.w, 0, 4); |
| return (this.w[3]) << 24 | (this.w[2] & 0xff) << 16 | (this.w[1] & 0xff) << 8 | (this.w[0] & 0xff); |
| } |
| |
| /** |
| * like DataInputStream.readLong except little endian. |
| */ |
| @Override |
| public final long readLong() throws IOException { |
| this.d.readFully(this.w, 0, 8); |
| return (long) (this.w[7]) << 56 | (long) (this.w[6] & 0xff) << 48 | (long) (this.w[5] & 0xff) << 40 |
| | (long) (this.w[4] & 0xff) << 32 | (long) (this.w[3] & 0xff) << 24 | (long) (this.w[2] & 0xff) << 16 |
| | (long) (this.w[1] & 0xff) << 8 | this.w[0] & 0xff; |
| } |
| |
| @Override |
| public final float readFloat() throws IOException { |
| return Float.intBitsToFloat(readInt()); |
| } |
| |
| @Override |
| public final double readDouble() throws IOException { |
| return Double.longBitsToDouble(readLong()); |
| } |
| |
| @Override |
| public final int read(byte b[], int off, int len) throws IOException { |
| return this.in.read(b, off, len); |
| } |
| |
| @Override |
| public final void readFully(byte b[]) throws IOException { |
| this.d.readFully(b, 0, b.length); |
| } |
| |
| @Override |
| public final void readFully(byte b[], int off, int len) throws IOException { |
| this.d.readFully(b, off, len); |
| } |
| |
| @Override |
| public final int skipBytes(int n) throws IOException { |
| return this.d.skipBytes(n); |
| } |
| |
| @Override |
| public final boolean readBoolean() throws IOException { |
| return this.d.readBoolean(); |
| } |
| |
| @Override |
| public final byte readByte() throws IOException { |
| return this.d.readByte(); |
| } |
| |
| @Override |
| public int read() throws IOException { |
| return this.in.read(); |
| } |
| |
| @Override |
| public final int readUnsignedByte() throws IOException { |
| return this.d.readUnsignedByte(); |
| } |
| |
| @Override |
| @Deprecated |
| public final String readLine() throws IOException { |
| return this.d.readLine(); |
| } |
| |
| @Override |
| public final String readUTF() throws IOException { |
| return this.d.readUTF(); |
| } |
| |
| @Override |
| public final void close() throws IOException { |
| this.d.close(); |
| } |
| |
| private final DataInputStream d; // to get at high level readFully methods of |
| // DataInputStream |
| private final InputStream in; // to get at the low-level read methods of |
| // InputStream |
| private final byte w[]; // work array for buffering input |
| |
| @Override |
| public synchronized void reset() throws IOException { |
| this.in.reset(); |
| } |
| |
| @Override |
| public synchronized void mark(int readlimit) { |
| this.in.mark(Integer.MAX_VALUE); |
| } |
| |
| @Override |
| public boolean markSupported() { |
| return this.in.markSupported(); |
| } |
| |
| } |