Read Short from InputStream via ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer Read

Description

Read 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];

    public static short ReadShort(InputStream is) throws IOException {
        bb.position(0);//from w  w  w .  j  av a  2  s .  c om
        is.read(tempBuffer, 0, 2);
        bb.put(tempBuffer, 0, 2);
        return bb.getShort(0);
    }
}

Related Tutorials