Java File Read via ByteBuffer readUnsignedInt(InputStream is)

Here you can find the source of readUnsignedInt(InputStream is)

Description

Reads an unsigned integer (32 bit) from specified input stream.

License

Open Source License

Parameter

Parameter Description
is The input stream to read from.

Return

The unsigned integer value from the stream.

Declaration

public static long readUnsignedInt(InputStream is) throws IOException 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

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

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.nio.channels.ReadableByteChannel;

public class Main {
    /**// ww  w .  j a va2  s .co m
     * Reads an unsigned integer (32 bit) from specified input stream.
     * @param is The input stream to read from.
     * @return The unsigned integer value from the stream.
     */
    public static long readUnsignedInt(InputStream is) throws IOException {
        return (long) readInt(is) & 0xffffffffL;
    }

    /**
     * Reads an integer (32 bit) from the specified byte channel.
     * @param channel The {@link ReadableByteChannel} to read from.
     * @return The integer value from the channel.
     */
    public static int readInt(ReadableByteChannel channel) throws IOException {
        ByteBuffer bb4 = getByteBuffer(4);
        bb4.position(0);
        for (int cnt = 0; cnt < 4;) {
            int n = channel.read(bb4);
            if (n < 0) {
                throw new IOException("End of stream");
            }
            cnt += n;
        }
        bb4.position(0);
        return bb4.getInt();
    }

    /**
     * Reads an integer (32 bit) from specified input stream.
     * @param is The input stream to read from.
     * @return The integer value from the stream.
     */
    public static int readInt(InputStream is) throws IOException {
        ByteBuffer bb4 = getByteBuffer(4);
        for (int cnt = 0; cnt < 4;) {
            int n = is.read(bb4.array(), cnt, bb4.array().length - cnt);
            if (n < 0) {
                throw new IOException("End of stream");
            }
            cnt += n;
        }
        bb4.position(0);
        return bb4.getInt();
    }

    /** Returns a fully initialized empty {@link ByteBuffer} in little endian order. */
    public static ByteBuffer getByteBuffer(int size) {
        return ByteBuffer.allocate(Math.max(0, size)).order(ByteOrder.LITTLE_ENDIAN);
    }

    /** Returns a {@link ByteBuffer} based on {@code buffer} in little endian order. */
    public static ByteBuffer getByteBuffer(byte[] buffer) {
        if (buffer == null) {
            buffer = new byte[0];
        }
        return ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN);
    }
}

Related

  1. readTwoByteInt(byte[] array, int start)
  2. readTxtFromFile(File file)
  3. readUint32(final DataInput di)
  4. readUint32AsInt(DataInput di)
  5. readUintLE(byte[] bytes, int pointer, int size)
  6. readUnsignedInt24(InputStream is)
  7. readUtf8(DataInput in)
  8. readVarLong(byte[] arr)
  9. readWaveFile44100_16Bit_Mono(String dir, String name)