Java ByteBuffer Read readSmart(ByteBuffer buffer)

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

Description

Reads a 'smart' (either a byte or short depending on the value) from the specified buffer.

License

Open Source License

Parameter

Parameter Description
buffer The buffer.

Return

The 'smart'.

Declaration

public static int readSmart(ByteBuffer buffer) 

Method Source Code


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

import java.nio.ByteBuffer;

public class Main {
    /**//from   ww  w  .  j  av  a 2 s  .com
     * Reads a 'smart' (either a {@code byte} or {@code short} depending on the value) from the specified buffer.
     * 
     * @param buffer The buffer.
     * @return The 'smart'.
     */
    public static int readSmart(ByteBuffer buffer) {
        int peek = buffer.get(buffer.position()) & 0xFF;
        if (peek < 128) {
            return buffer.get() & 0xFF;
        }
        return (buffer.getShort() & 0xFFFF) - 32768;
    }
}

Related

  1. readShorts(final ByteBuffer bb, final int length)
  2. readShortString(ByteBuffer buffer)
  3. readSignedVarint(ByteBuffer buffer)
  4. readSize(ByteBuffer buf)
  5. readSize(ByteBuffer buffer)
  6. readToBytes(ByteBuffer byteBuffer)
  7. readTs(ByteBuffer is)
  8. readTs(ByteBuffer is, int c)
  9. readUUID(ByteBuffer buffer)