Here you can find the source of writeBooleanArray(boolean[] array, ByteBuffer out)
public static void writeBooleanArray(boolean[] array, ByteBuffer out)
//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)); } } }