Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * 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 com.google.common.io;
 
 
An implementation of java.io.DataInput that uses little-endian byte ordering for reading short, int, float, double, and long values.

Note: This class intentionally violates the specification of its supertype DataInput, which explicitly requires big-endian byte order.

Author(s):
Chris Nokleberg
Keith Bottner
Since:
8.0
 
 public final class LittleEndianDataInputStream extends FilterInputStream
     implements DataInput {

  
Creates a LittleEndianDataInputStream that wraps the given stream.

Parameters:
in the stream to delegate to
 
     super(Preconditions.checkNotNull(in));
   }

  
This method will throw an java.lang.UnsupportedOperationException.
 
   @Override
   public String readLine() {
     throw new UnsupportedOperationException("readLine is not supported");
   }
 
   @Override
   public void readFully(byte[] bthrows IOException {
     ByteStreams.readFully(thisb);
   }
 
   @Override
   public void readFully(byte[] bint offint lenthrows IOException {
     ByteStreams.readFully(thisbofflen);
   }
 
   @Override
   public int skipBytes(int nthrows IOException {
     return (int.skip(n);
   }
 
   @Override
   public int readUnsignedByte() throws IOException {
     int b1 = .read();
     if (0 > b1) {
       throw new EOFException();
     }
     
     return b1;
   }

  
Reads an unsigned short as specified by java.io.DataInputStream.readUnsignedShort(), except using little-endian byte order.

Returns:
the next two bytes of the input stream, interpreted as an unsigned 16-bit integer in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
 
   @Override
   public int readUnsignedShort() throws IOException {
    byte b1 = readAndCheckByte();
    byte b2 = readAndCheckByte();
    return Ints.fromBytes((byte) 0, (byte) 0, b2b1);
  }

  
Reads an integer as specified by java.io.DataInputStream.readInt(), except using little-endian byte order.

Returns:
the next four bytes of the input stream, interpreted as an int in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
  public int readInt() throws IOException {
    byte b1 = readAndCheckByte();
    byte b2 = readAndCheckByte();
    byte b3 = readAndCheckByte();
    byte b4 = readAndCheckByte();
    return Ints.fromBytesb4b3b2b1);
  }

  
Reads a long as specified by java.io.DataInputStream.readLong(), except using little-endian byte order.

Returns:
the next eight bytes of the input stream, interpreted as a long in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
  public long readLong() throws IOException {
    byte b1 = readAndCheckByte();
    byte b2 = readAndCheckByte();
    byte b3 = readAndCheckByte();
    byte b4 = readAndCheckByte();
    byte b5 = readAndCheckByte();
    byte b6 = readAndCheckByte();
    byte b7 = readAndCheckByte();
    byte b8 = readAndCheckByte();
    return Longs.fromBytes(b8b7b6b5b4b3b2b1);
  }

  
Reads a float as specified by java.io.DataInputStream.readFloat(), except using little-endian byte order.

Returns:
the next four bytes of the input stream, interpreted as a float in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
  public float readFloat() throws IOException {
    return Float.intBitsToFloat(readInt());
  }

  
Reads a double as specified by java.io.DataInputStream.readDouble(), except using little-endian byte order.

Returns:
the next eight bytes of the input stream, interpreted as a double in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
  public double readDouble() throws IOException {
    return Double.longBitsToDouble(readLong());
  }
  public String readUTF() throws IOException {
    return new DataInputStream().readUTF();
  }

  
Reads a short as specified by java.io.DataInputStream.readShort(), except using little-endian byte order.

Returns:
the next two bytes of the input stream, interpreted as a short in little-endian byte order.
Throws:
java.io.IOException if an I/O error occurs.
  public short readShort() throws IOException {
    return (shortreadUnsignedShort();
  }

  
Reads a char as specified by java.io.DataInputStream.readChar(), except using little-endian byte order.

Returns:
the next two bytes of the input stream, interpreted as a char in little-endian byte order
Throws:
java.io.IOException if an I/O error occurs
  public char readChar() throws IOException {
    return (charreadUnsignedShort();
  }
  public byte readByte() throws IOException {
    return (bytereadUnsignedByte();
  }
  public boolean readBoolean() throws IOException {
    return readUnsignedByte() != 0;
  }

  
Reads a byte from the input stream checking that the end of file (EOF) has not been encountered.

Returns:
byte read from input
Throws:
java.io.IOException if an error is encountered while reading
java.io.EOFException if the end of file (EOF) is encountered.
  private byte readAndCheckByte() throws IOExceptionEOFException {
    int b1 = .read();
    if (-1 == b1) {
      throw new EOFException();
    }
    return (byteb1;
  }
New to GrepCode? Check out our FAQ X