Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package com.jme3.util;
     33 
     34 import java.io.*;
     35 
     36 /**
     37  * <code>LittleEndien</code> is a class to read littleendien stored data
     38  * via a InputStream.  All functions work as defined in DataInput, but
     39  * assume they come from a LittleEndien input stream.  Currently used to read .ms3d and .3ds files.
     40  * @author Jack Lindamood
     41  */
     42 public class LittleEndien extends InputStream implements DataInput {
     43 
     44     private BufferedInputStream in;
     45     private BufferedReader inRead;
     46 
     47     /**
     48      * Creates a new LittleEndien reader from the given input stream.  The
     49      * stream is wrapped in a BufferedReader automatically.
     50      * @param in The input stream to read from.
     51      */
     52     public LittleEndien(InputStream in) {
     53         this.in = new BufferedInputStream(in);
     54         inRead = new BufferedReader(new InputStreamReader(in));
     55     }
     56 
     57     public int read() throws IOException {
     58         return in.read();
     59     }
     60 
     61     @Override
     62     public int read(byte[] buf) throws IOException {
     63         return in.read(buf);
     64     }
     65 
     66     @Override
     67     public int read(byte[] buf, int off, int len) throws IOException {
     68         return in.read(buf, off, len);
     69     }
     70 
     71     public int readUnsignedShort() throws IOException {
     72         return (in.read() & 0xff) | ((in.read() & 0xff) << 8);
     73     }
     74 
     75     /**
     76      * read an unsigned int as a long
     77      */
     78     public long readUInt() throws IOException {
     79         return ((in.read() & 0xff)
     80                 | ((in.read() & 0xff) << 8)
     81                 | ((in.read() & 0xff) << 16)
     82                 | (((long) (in.read() & 0xff)) << 24));
     83     }
     84 
     85     public boolean readBoolean() throws IOException {
     86         return (in.read() != 0);
     87     }
     88 
     89     public byte readByte() throws IOException {
     90         return (byte) in.read();
     91     }
     92 
     93     public int readUnsignedByte() throws IOException {
     94         return in.read();
     95     }
     96 
     97     public short readShort() throws IOException {
     98         return (short) this.readUnsignedShort();
     99     }
    100 
    101     public char readChar() throws IOException {
    102         return (char) this.readUnsignedShort();
    103     }
    104 
    105     public int readInt() throws IOException {
    106         return ((in.read() & 0xff)
    107                 | ((in.read() & 0xff) << 8)
    108                 | ((in.read() & 0xff) << 16)
    109                 | ((in.read() & 0xff) << 24));
    110     }
    111 
    112     public long readLong() throws IOException {
    113         return ((in.read() & 0xff)
    114                 | ((long) (in.read() & 0xff) << 8)
    115                 | ((long) (in.read() & 0xff) << 16)
    116                 | ((long) (in.read() & 0xff) << 24)
    117                 | ((long) (in.read() & 0xff) << 32)
    118                 | ((long) (in.read() & 0xff) << 40)
    119                 | ((long) (in.read() & 0xff) << 48)
    120                 | ((long) (in.read() & 0xff) << 56));
    121     }
    122 
    123     public float readFloat() throws IOException {
    124         return Float.intBitsToFloat(readInt());
    125     }
    126 
    127     public double readDouble() throws IOException {
    128         return Double.longBitsToDouble(readLong());
    129     }
    130 
    131     public void readFully(byte b[]) throws IOException {
    132         in.read(b, 0, b.length);
    133     }
    134 
    135     public void readFully(byte b[], int off, int len) throws IOException {
    136         in.read(b, off, len);
    137     }
    138 
    139     public int skipBytes(int n) throws IOException {
    140         return (int) in.skip(n);
    141     }
    142 
    143     public String readLine() throws IOException {
    144         return inRead.readLine();
    145     }
    146 
    147     public String readUTF() throws IOException {
    148         throw new IOException("Unsupported operation");
    149     }
    150 
    151     @Override
    152     public void close() throws IOException {
    153         in.close();
    154     }
    155 
    156     @Override
    157     public int available() throws IOException {
    158         return in.available();
    159     }
    160 }
    161