Here you can find the source of readBitset(ByteBuffer bb, int numbytes)
Parameter | Description |
---|---|
bb | ByteBuffer to read from |
numbytes | Number of bytes to read. |
public static BitSet readBitset(ByteBuffer bb, int numbytes)
//package com.java2s; /**/*from w w w . j a va 2s.c om*/ * Copyright (C) 2011 Darien Hager * * This code is part of the "HL2Parse" project, and is licensed under * a Creative Commons Attribution-ShareAlike 3.0 Unported License. For * either a summary of conditions or the full legal text, please visit: * * http://creativecommons.org/licenses/by-sa/3.0/ * * Permissions beyond the scope of this license may be available * at http://technofovea.com/ . */ import java.nio.ByteBuffer; import java.util.BitSet; public class Main { /** * Reads a series of bytes from the given buffer, turning them into a * little-endian bit-set. The ByteBuffer's position is advanced appropriately. * * Thus the three-byte series of 10000001,00001000,00000000 has set flags * at positions 0,7, and 11 * @param bb ByteBuffer to read from * @param numbytes Number of bytes to read. * @return A new BitSet with the correct areas set/unset */ public static BitSet readBitset(ByteBuffer bb, int numbytes) { byte[] barr = new byte[numbytes]; bb.get(barr); BitSet ret = new BitSet(numbytes * 8); for (int i_byte = 0; i_byte < barr.length; i_byte++) { for (int i = 0; i < 8; i++) { int idx = i_byte * 8 + i; boolean isSet = (barr[i_byte] & (1 << i)) > 0; ret.set(idx, isSet); } } return ret; } }