Java ByteBuffer Write writeByteArray(byte[] array, ByteBuffer out)

Here you can find the source of writeByteArray(byte[] array, ByteBuffer out)

Description

write Byte Array

License

Apache License

Declaration

public static void writeByteArray(byte[] array, ByteBuffer out) 

Method Source Code

//package com.java2s;
/*/*from www.  j ava 2 s  .  c  o m*/
 * Copyright 2013-2014 eBay Software Foundation
 *
 * 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.
 */

import java.nio.ByteBuffer;

public class Main {
    public static void writeByteArray(byte[] array, ByteBuffer out) {
        if (array == null) {
            writeVInt(-1, out);
            return;
        }
        writeVInt(array.length, out);
        out.put(array);
    }

    public static void writeVInt(int i, ByteBuffer out) {
        writeVLong(i, out);
    }

    public static void writeVLong(long i, ByteBuffer out) {
        if (i >= -112 && i <= 127) {
            out.put((byte) i);
            return;
        }

        int len = -112;
        if (i < 0) {
            i ^= -1L; // take one's complement'
            len = -120;
        }

        long tmp = i;
        while (tmp != 0) {
            tmp = tmp >> 8;
            len--;
        }

        out.put((byte) len);

        len = (len < -120) ? -(len + 120) : -(len + 112);

        for (int idx = len; idx != 0; idx--) {
            int shiftbits = (idx - 1) * 8;
            long mask = 0xFFL << shiftbits;
            out.put((byte) ((i & mask) >> shiftbits));
        }
    }
}

Related

  1. writeBuffer(ByteBuffer dest, ByteBuffer src)
  2. writeBuffer(ByteChannel channel, ByteBuffer buffer)
  3. writeBuffer(FileChannel fc, ByteBuffer buf, int startPos)
  4. writeBuffer(WritableByteChannel chan, ByteBuffer buf)
  5. writeByte(ByteBuffer dest, int off, int i)
  6. writeByteArray(byte[] data, ByteBuffer buffer)
  7. writeByteArray(ByteBuffer byteBuffer, byte[] bytes)
  8. writeByteArray(ByteBuffer logBuf, byte[] b)
  9. writeByteBuffer(ByteBuffer bbuf, String filename)