Java ByteBuffer to Short toShort(ByteBuffer buffer)

Here you can find the source of toShort(ByteBuffer buffer)

Description

to Short

License

Open Source License

Declaration

public static short toShort(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static short toShort(byte[] bytes) {
        return toShort(ByteBuffer.wrap(bytes));
    }/*from  w w w  . j a v a 2s  .  co  m*/

    public static short toShort(ByteBuffer buffer) {
        return toByteBuffer(buffer, Short.BYTES).getShort();
    }

    protected static ByteBuffer toByteBuffer(ByteBuffer buffer, int length) {
        if (length > buffer.remaining()) {
            return (ByteBuffer) ((ByteBuffer) ByteBuffer.allocate(length).position(length - buffer.remaining()))
                    .put(buffer.array()).rewind();
        } else {
            return buffer;
        }
    }
}

Related

  1. toShort(ByteBuffer bytes)
  2. toShort(ByteBuffer value)