Reads a big-endian unsigned short integer from the buffer at the provided offset. - Java java.lang

Java examples for java.lang:String Endian

Description

Reads a big-endian unsigned short integer from the buffer at the provided offset.

Demo Code

/*//from ww  w.  j  ava2  s.c  o  m
 * Copyright (c) 2015 Twitter, Inc. All rights reserved.
 * Licensed under the Apache License v2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * This file is substantially based on work from the Netty project, also
 * released under the above license.
 */
//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Reads a big-endian unsigned short integer from the buffer at the provided offset.
     */
    static int getUnsignedShort(ByteBuffer buffer, int offset) {
        return (buffer.get(offset) & 0xFF) << 8 | buffer.get(offset + 1)
                & 0xFF;
    }

    /**
     * Reads a big-endian unsigned short integer from the buffer and advances position.
     */
    static int getUnsignedShort(ByteBuffer buffer) {
        return (buffer.get() & 0xFF) << 8 | buffer.get() & 0xFF;
    }
}

Related Tutorials