Read a littleEndian integer(32b its) from DataInput - Java java.lang

Java examples for java.lang:int Format

Description

Read a littleEndian integer(32b its) from DataInput

Demo Code

/*// www .  jav a  2s  .c o  m

(C) Copyright 2015-2016 Alberto Fern?ndez <infjaf@gmail.com>
(C) Copyright 2014 Jan Schl??in
(C) Copyright 2003-2004 Anil Kumar K <anil@linuxense.com>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library.  If not, see <http://www.gnu.org/licenses/>.

 */
//package com.java2s;

import java.io.DataInput;
import java.io.IOException;

public class Main {
    /**
     * Read a littleEndian integer(32b its) from DataInput
     * @param in DataInput to read from
     * @return int value of next 32 bits as littleEndian
     * @throws IOException
     */
    public static int readLittleEndianInt(DataInput in) throws IOException {
        int bigEndian = 0;
        for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
            bigEndian |= (in.readUnsignedByte() & 0xff) << shiftBy;
        }
        return bigEndian;
    }
}

Related Tutorials