Java ByteBuffer Set readBitset(ByteBuffer bb, int numbytes)

Here you can find the source of readBitset(ByteBuffer bb, int numbytes)

Description

Reads a series of bytes from the given buffer, turning them into a little-endian bit-set.

License

Creative Commons License

Parameter

Parameter Description
bb ByteBuffer to read from
numbytes Number of bytes to read.

Return

A new BitSet with the correct areas set/unset

Declaration

public static BitSet readBitset(ByteBuffer bb, int numbytes) 

Method Source Code

//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;
    }
}

Related

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