Java File Read via ByteBuffer readIntLittleEndian(InputStream in)

Here you can find the source of readIntLittleEndian(InputStream in)

Description

read Int Little Endian

License

Apache License

Declaration

public static int readIntLittleEndian(InputStream in)
            throws IOException 

Method Source Code

//package com.java2s;
/* // ww  w . jav  a 2 s  . c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    /**
     * reads an int in little endian at the given position
     * @param in
     * @param offset
     * @return
     * @throws IOException
     */
    public static int readIntLittleEndian(ByteBuffer in, int offset)
            throws IOException {
        int ch4 = in.get(offset) & 0xff;
        int ch3 = in.get(offset + 1) & 0xff;
        int ch2 = in.get(offset + 2) & 0xff;
        int ch1 = in.get(offset + 3) & 0xff;
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }

    /**
     * reads an int in little endian at the given position
     * @param in
     * @param offset
     * @return
     * @throws IOException
     */
    public static int readIntLittleEndian(byte[] in, int offset)
            throws IOException {
        int ch4 = in[offset] & 0xff;
        int ch3 = in[offset + 1] & 0xff;
        int ch2 = in[offset + 2] & 0xff;
        int ch1 = in[offset + 3] & 0xff;
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }

    public static int readIntLittleEndian(InputStream in)
            throws IOException {
        // TODO: this is duplicated code in LittleEndianDataInputStream
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0) {
            throw new EOFException();
        }
        return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
    }
}

Related

  1. readInt(InputStream inputStream)
  2. readInt(ReadableByteChannel channel)
  3. readInt(ReadableByteChannel channel)
  4. readInt16(DataInput di)
  5. readIntArray(DataInput input)
  6. readIntoBuffer(RandomAccessFile file, int offset, int size)
  7. readJsonFromFile(String path)
  8. readLargeFile(File file)
  9. readLong(byte[] src, int pointer)