BooleanStream

0

Data Structure

BooleanStream is an apache activemq class that allows compact way marshalling and unmarshalling boolean values. We want to be able to write the boolean values in the order 0 0 1 1 1 0 1 and read them back in the same order. The straight forward way is to to create a byte array and assign the boolean values to it, for example b[0]=0, b[1]=0, b[2]=1, b[3]=1, b[4]=1, b[5]=0, b[6]=1. The compact way would be to store the boolean values as a decimal value occupying single byte.
BooleanStream

 

To encode a boolean value, we left shift the value to most significant bit pos and apply an OR with the existing byte value. To decode we right shift the value to least significant bit pos and apply an AND with 0x01.

 

public void testBooleanStreamMarshallUnmarshall() {
        //0 0 1 1 1 0 1
        //data[arrayPos] |= 0x01 << bytePos;
        byte bytePos = 0;
        byte b = 0;

        //value =0
        //value is 0 so simply increment the bytePos
        bytePos++;

        //value =0
        //value is 0 so simply increment the bytePos
        bytePos++;

        // value=1
        // 0 | 100 = 100
        b |= 0x01 << bytePos;
        System.out.println(b);
        bytePos++;

        //value=1
        // 100 | 1000 = 1100
        b |= 0x01 << bytePos;
        System.out.println(b);
        bytePos++;

        //value=1
        // 1100 | 1 0000 = 1 1100
        b |= 0x01 << bytePos;
        System.out.println(b);
        bytePos++;

        //value=0
        bytePos++;

        //value=1
        // 1 1100 | 0100 0000 = 0101 1100
        b |= 0x01 << bytePos;
        System.out.println(b);

        /*** Un-marshall ***/
        //Right shift the bit to the least significant bit position and apply AND with 0x01 to know its value
        bytePos = 0;
        while (bytePos > bytePos) & 0x01));
            bytePos++;
        }
    }

 

Share.

Leave A Reply