Read Unsigned Short from InputStream via ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer Read

Description

Read Unsigned Short from InputStream via ByteBuffer

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import java.nio.ByteBuffer;

public class Main {
    private static ByteBuffer bb = ByteBuffer.allocate(8);
    private static byte[] tempBuffer = new byte[8];
    private static byte[] padder = { 0, 0, 0, 0, 0, 0, 0, 0 };

    public static int ReadUnsignedShort(InputStream is) throws IOException {
        bb.position(0);/*from  w ww .ja v a2 s.  c  o m*/
        is.read(tempBuffer, 0, 2);

        bb.put(tempBuffer, 0, 2);
        bb.put(padder, 0, 2);

        return bb.getInt(0);
    }
}

Related Tutorials