Java ByteBuffer to Int readUnsignedShort(ByteBuffer bb)

Here you can find the source of readUnsignedShort(ByteBuffer bb)

Description

See the general contract of the readUnsignedShort method of DataInput.

License

Open Source License

Parameter

Parameter Description
bb bb

Return

the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.

Declaration

public static int readUnsignedShort(ByteBuffer bb) throws BufferUnderflowException 

Method Source Code

//package com.java2s;
/**/*  w  w w  .j  av  a  2 s .c o m*/
 * Copyright 2009 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 *
 * This file is part of MARY TTS.
 *
 * MARY TTS 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, version 3 of the License.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

public class Main {
    /**
     * See the general contract of the <code>readUnsignedShort</code> method of <code>DataInput</code>.
     * <p>
     * Bytes for this operation are read from the given byte buffer
     * 
     * @param bb
     *            bb
     * @return the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.
     * @exception BufferUnderflowException
     *                if this input stream reaches the end before reading two bytes.
     * @see java.io.FilterInputStream#in
     */
    public static int readUnsignedShort(ByteBuffer bb) throws BufferUnderflowException {
        int ch1 = bb.get() & 0xFF; // convert byte to unsigned byte
        int ch2 = bb.get() & 0xFF; // convert byte to unsigned byte
        return (ch1 << 8) + (ch2 << 0);
    }
}

Related

  1. readUnsignedInt(ByteBuffer buf)
  2. readUnsignedInt(ByteBuffer buffer)
  3. readUnsignedInt(ByteBuffer buffer, int index)
  4. readUnsignedInt(ByteBuffer byteBuf)
  5. readUnsignedMedium(ByteBuffer buf)
  6. readUnsignedShort(ByteBuffer buffer)
  7. readUnsignedTriByte(ByteBuffer buffer)
  8. readUnsignedVarint(ByteBuffer buffer)
  9. readVarInt(ByteBuffer buff)