Java ByteBuffer Set readBitSet(ByteBuffer buf, int len)

Here you can find the source of readBitSet(ByteBuffer buf, int len)

Description

read Bit Set

License

Open Source License

Declaration

public static BitSet readBitSet(ByteBuffer buf, int len) 

Method Source Code

//package com.java2s;
/**/*from  w  ww  .jav  a 2 s  .com*/
 * Project: ${puma-common.aid}
 * <p/>
 * File Created at 2012-6-6 $Id$
 * <p/>
 * Copyright 2010 dianping.com. All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of Dianping
 * Company. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with dianping.com.
 */

import java.nio.ByteBuffer;
import java.util.BitSet;

public class Main {
    public static BitSet readBitSet(ByteBuffer buf, int len) {
        BitSet bs = new BitSet((len + 7) >>> 3);
        int i;
        int b = 0;
        int leftBit = 0x01;
        int bitMask = 0;
        for (i = 0; i < len; i++) {
            if (i % 8 == 0) {
                b = buf.get();
                bitMask = leftBit;
            } else {
                bitMask = bitMask << 1;
            }
            if ((bitMask & b) == bitMask) {
                bs.set(i);
            } else {
                bs.clear(i);
            }
        }
        return bs;
    }
}

Related

  1. memset(ByteBuffer dstBuffer, int dstByteOffset, byte value, int length)
  2. parseContent(ByteBuffer buffer, int offset, int length)
  3. parseToByteBuffer(Object value)
  4. pourBufferToArray(ByteBuffer source, byte[] destination, int offset, int sizeRequested)
  5. readBitset(ByteBuffer bb, int numbytes)
  6. readBlock(ByteBuffer buffer, String fileName, long startOffset, Logger log)
  7. readFromChannel(FileChannel fc, ByteBuffer buffer, int bufferOffset, int num)
  8. readKey(ByteBuffer index, int indexByteOffset, byte[] foundKey)
  9. readPackageName(ByteBuffer buffer, int offset)