Merge short value from two byte buffer. - Java java.nio

Java examples for java.nio:ByteBuffer Short

Description

Merge short value from two byte buffer.

Demo Code

/*/*from w  ww. jav a  2 s .  co m*/
 * Copyright 1999-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    private static final int SIZE_OF_BYTE_IN_BITS = 8;
    private static final int MASK = 0x000000FF;

    /**
     * Merge short value from two byte buffer. First byte of short will be extracted from first byte buffer and second from second
     * one.
     * 
     * @param buffer
     *          to read first part of value
     * @param buffer1
     *          to read second part of value
     * @return merged value
     */
    public static short mergeShortFromBuffers(ByteBuffer buffer,
            ByteBuffer buffer1) {
        short result = 0;
        result = (short) (result | (buffer.get() & MASK));
        result = (short) (result << SIZE_OF_BYTE_IN_BITS);
        result = (short) (result | (buffer1.get() & MASK));
        return result;
    }
}

Related Tutorials