Java ByteBuffer to Short Array getShortBE(final ByteBuffer b, final int start, final int end)

Here you can find the source of getShortBE(final ByteBuffer b, final int start, final int end)

Description

Computes a number whereby the 1st byte is the most significant and the last byte is the least significant.

License

Open Source License

Parameter

Parameter Description
b The ByteBuffer
start The starting offset in b. The less significant byte
end The end index (included) in b

Return

a short number represented by the byte sequence.

Declaration

public static short getShortBE(final ByteBuffer b, final int start, final int end) 

Method Source Code

//package com.java2s;
/*//from ww w  .j a v  a2 s .c  o  m
 * Copyright (c) 2017 Eric A. Snell
 *
 * This file is part of eAlvaTag.
 *
 * eAlvaTag is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * eAlvaTag is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with eAlvaTag.  If not,
 * see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Computes a number whereby the 1st byte is the most significant and the last
     * byte is the least significant.
     *
     * @param b     The ByteBuffer
     * @param start The starting offset in b. The less significant byte
     * @param end   The end index (included) in b
     *
     * @return a short number represented by the byte sequence.
     */
    public static short getShortBE(final ByteBuffer b, final int start, final int end) {
        return (short) getIntBE(b, start, end);
    }

    /**
     * Computes a number whereby the 1st byte is the most significant and the last
     * byte is the least significant.
     *
     * @param b     The ByteBuffer
     * @param start The starting offset in b. The less significant byte
     * @param end   The end index (included) in b
     *
     * @return an int number represented by the byte sequence.
     */
    public static int getIntBE(final ByteBuffer b, final int start, final int end) {
        return (int) getLongBE(b, start, end);
    }

    /**
     * Computes a number whereby the 1st byte is the most significant and the last
     * byte is the least significant.
     * <p>
     * So if storing a number which only requires one byte it will be stored in the last
     * byte.
     * <p>
     * Will fail if end - start >= 8, due to the limitations of the long type.
     */
    public static long getLongBE(final ByteBuffer b, final int start, final int end) {
        long number = 0;
        for (int i = 0; i < (end - start + 1); i++) {
            number += ((long) ((b.get(end - i) & 0xFF)) << i * 8);
        }

        return number;
    }
}

Related

  1. getShort(ByteBuffer bb)
  2. getShort(ByteBuffer buffer)
  3. getShort(ByteBuffer byteBuffer)
  4. getShortB(ByteBuffer bb, int bi)
  5. getShortBE(ByteBuffer b, int start, int end)
  6. getShortLength(ByteBuffer bb)
  7. getShortLength(ByteBuffer bb, int position)
  8. getShortNumeric(java.nio.ByteBuffer buffer, int len)
  9. getShortString(ByteBuffer in)