Java File Read via ByteBuffer readUnsignedInt24(InputStream is)

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

Description

Reads an unsigned 24 bit integer from specified input stream.

License

Open Source License

Parameter

Parameter Description
is The input stream to read from.

Return

The unsigned 24 bit integer value from the stream.

Declaration

public static int readUnsignedInt24(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;

public class Main {
    /**/*  w ww.jav a 2 s.  c om*/
     * Reads an unsigned 24 bit integer from specified input stream.
     * @param is The input stream to read from.
     * @return The unsigned 24 bit integer value from the stream.
     */
    public static int readUnsignedInt24(InputStream is) throws IOException {
        ByteBuffer bb4 = getByteBuffer(4);
        for (int cnt = 0; cnt < 3;) {
            int n = is.read(bb4.array(), cnt, bb4.array().length - cnt - 1);
            if (n < 0) {
                throw new IOException("End of stream");
            }
            cnt += n;
        }
        bb4.position(0);
        return bb4.getInt() & 0xffffff;
    }

    /** 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. readTxtFromFile(File file)
  2. readUint32(final DataInput di)
  3. readUint32AsInt(DataInput di)
  4. readUintLE(byte[] bytes, int pointer, int size)
  5. readUnsignedInt(InputStream is)
  6. readUtf8(DataInput in)
  7. readVarLong(byte[] arr)
  8. readWaveFile44100_16Bit_Mono(String dir, String name)
  9. readWavHeader(DataInputStream inStrm, FileChannel fc)