com.datasalt.pangool.io
Class BitField

java.lang.Object
  extended by com.datasalt.pangool.io.BitField
All Implemented Interfaces:
Comparable<BitField>

public class BitField
extends Object
implements Comparable<BitField>

Bit Field class. It can holds unbounded bit fields, that can be compared and serialized. The internal representation uses one bit per byte for internal usage, so this class is able to represent 7 bits of information per each byte.
Serialization: BitField uses a variable length serialization. The minimum size is one byte when the bits that are set fits on one byte. That is, when there are not bits set above the 6 position. 2 bytes if there are not bits set above the position 13, and so on. See ser(java.io.DataOutput) for more information.
Comparison: BitField compares bits lexicographically. Lower position bits are smaller than bigger ones.
Internal in memory representation: A byte array contains all the bits. Each byte contains 7 bits on the most significant bits. The less significant bit of each byte is not used. This representation is used to be quite close to the serialized representation, where the less significant big states if there are more bytes to be read.


Nested Class Summary
 class BitField.BitFieldComparator
          Simple lexicographic BitField comparator
 
Constructor Summary
BitField()
           
 
Method Summary
protected static int bitOnByte(int bit, int bite)
           
protected static byte byteBitSet(int bit, byte bite)
           
protected static byte byteBitUnset(int bit, byte bite)
           
protected static int byteForBit(int bit)
          Minimum size of the backing array needed for setting the given bit
 void clear()
          Clears the bit field, unsetting all bits.
 int compareTo(BitField o)
           
 int deser(byte[] bytes, int start)
          Deserialize a BitField serialized from a byte array.
 int deser(DataInput in)
          Deserialize a BitField serialized using ser(java.io.DataOutput).
protected  void ensureSpace(int bytes)
          Ensures a minimum size for the backing byte array
protected  byte[] getBackingArray()
          Returns the backing array keeping the bits.
 boolean isSet(int bit)
          Returns the value of a given bit.
 void ser(DataOutput out)
          Serializes the bit field to the data output.
 void set(int bit)
          Sets a bit.
 void set(int bit, boolean value)
          Sets or unsets a bit.
 String toString()
          Returns the hexadecimal representation of the set backing array
 void unset(int bit)
          Unsets a bit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BitField

public BitField()
Method Detail

set

public void set(int bit,
                boolean value)
Sets or unsets a bit. The smaller allowed bit is 0


set

public void set(int bit)
Sets a bit. The smaller allowed bit is 0


unset

public void unset(int bit)
Unsets a bit. The smaller allowed bit is 0


isSet

public boolean isSet(int bit)
Returns the value of a given bit. False is returned for unexisting bits.


ser

public void ser(DataOutput out)
         throws IOException
Serializes the bit field to the data output. It uses one byte per each 7 bits. If the rightmost bit of the read byte is set, that means that there are more bytes to consume. The latest byte has the rightmost bit unset.

Throws:
IOException

deser

public int deser(DataInput in)
          throws IOException
Deserialize a BitField serialized using ser(java.io.DataOutput). Return the number of bytes consumed.

Throws:
IOException

deser

public int deser(byte[] bytes,
                 int start)
          throws IOException
Deserialize a BitField serialized from a byte array. Return the number of bytes consumed.

Throws:
IOException

getBackingArray

protected byte[] getBackingArray()
Returns the backing array keeping the bits. Be careful because the array is a reference.


byteForBit

protected static int byteForBit(int bit)
Minimum size of the backing array needed for setting the given bit


bitOnByte

protected static int bitOnByte(int bit,
                               int bite)

ensureSpace

protected void ensureSpace(int bytes)
Ensures a minimum size for the backing byte array


byteBitSet

protected static byte byteBitSet(int bit,
                                 byte bite)

byteBitUnset

protected static byte byteBitUnset(int bit,
                                   byte bite)

compareTo

public int compareTo(BitField o)
Specified by:
compareTo in interface Comparable<BitField>

clear

public void clear()
Clears the bit field, unsetting all bits.


toString

public String toString()
Returns the hexadecimal representation of the set backing array

Overrides:
toString in class Object


Copyright © –2014 Datasalt Systems S.L.. All rights reserved.