Java ByteBuffer Merge packByteBufferIntoSingleByteArray( ByteBuffer[] byteBufferArr)

Here you can find the source of packByteBufferIntoSingleByteArray( ByteBuffer[] byteBufferArr)

Description

This method will form one single byte [] for all the high card dims.

License

Apache License

Parameter

Parameter Description
byteBufferArr a parameter

Return

byte[] key.

Declaration

public static byte[] packByteBufferIntoSingleByteArray(
        ByteBuffer[] byteBufferArr) 

Method Source Code

//package com.java2s;
/*/*from www  . j a  va  2s.c om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * This method will form one single byte [] for all the high card dims.
     * First it will add all the indexes of variable length byte[] and then the
     * actual value
     *
     * @param byteBufferArr
     * @return byte[] key.
     */
    public static byte[] packByteBufferIntoSingleByteArray(
            ByteBuffer[] byteBufferArr) {
        // for empty array means there is no data to remove dictionary.
        if (null == byteBufferArr || byteBufferArr.length == 0) {
            return null;
        }
        int noOfCol = byteBufferArr.length;
        short offsetLen = (short) (noOfCol * 2);
        int totalBytes = calculateTotalBytes(byteBufferArr) + offsetLen;
        ByteBuffer buffer = ByteBuffer.allocate(totalBytes);
        // writing the offset of the first element.
        buffer.putShort(offsetLen);

        // prepare index for byte []
        for (int index = 0; index < byteBufferArr.length - 1; index++) {
            ByteBuffer individualCol = byteBufferArr[index];
            int noOfBytes = individualCol.capacity();
            buffer.putShort((short) (offsetLen + noOfBytes));
            offsetLen += noOfBytes;
            individualCol.rewind();
        }

        // put actual data.
        for (int index = 0; index < byteBufferArr.length; index++) {
            ByteBuffer individualCol = byteBufferArr[index];
            buffer.put(individualCol.array());
        }

        buffer.rewind();
        return buffer.array();

    }

    /**
     * To calculate the total bytes in byte Buffer[].
     *
     * @param byteBufferArr
     * @return
     */
    private static int calculateTotalBytes(ByteBuffer[] byteBufferArr) {
        int total = 0;
        for (int index = 0; index < byteBufferArr.length; index++) {
            total += byteBufferArr[index].capacity();
        }
        return total;
    }
}

Related

  1. packByteBufferIntoSingleByteArray( ByteBuffer[] byteBufferArr)