Java ByteBuffer Write writeBooleanArray(boolean[] array, ByteBuffer out)

Here you can find the source of writeBooleanArray(boolean[] array, ByteBuffer out)

Description

write Boolean Array

License

Apache License

Declaration

public static void writeBooleanArray(boolean[] array, ByteBuffer out) 

Method Source Code

//package com.java2s;
/*//from www.j  av a  2 s  .  c o m
 * 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 {
    public static void writeBooleanArray(boolean[] array, ByteBuffer out) {
        if (array == null) {
            writeVInt(-1, out);
            return;
        }
        writeVInt(array.length, out);
        byte b_true = (byte) 1;
        byte b_false = (byte) 0;

        for (int i = 0; i < array.length; i++) {
            if (array[i])
                out.put(b_true);
            else
                out.put(b_false);
        }
    }

    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. writeAllToChannel(List buffers, WritableByteChannel channel)
  2. writeBE(ByteBuffer bb, int elementWidth, long value)
  3. writeBER32(ByteBuffer buffer, int value)
  4. writeBigInteger(ByteBuffer bb, BigInteger bigInteger, int length)
  5. writeBlock(String fileName, long startOffset, ByteBuffer buffer, Logger log)
  6. writeBuffer(ByteBuffer dest, ByteBuffer src)
  7. writeBuffer(ByteChannel channel, ByteBuffer buffer)
  8. writeBuffer(FileChannel fc, ByteBuffer buf, int startPos)
  9. writeBuffer(WritableByteChannel chan, ByteBuffer buf)