0

I am trying to use ByteBuffer properly with BigEndian byte order format..

I have couple of fields which I am trying to put together into a single ByteBuffer before storing it in Cassandra database.

That Byte Array which I will be writing into Cassandra is made up of three Byte Arrays as described below-

short employeeId = 32767;
long lastModifiedDate = "1379811105109L";
byte[] attributeValue = os.toByteArray();

Now, I will write employeeId , lastModifiedDate and attributeValue together into a single Byte Array and that resulting Byte Array I will write into Cassandra and then I will be having my C++ program which will retrieve that Byte Array data from Cassandra and then deserialize it to extract employeeId , lastModifiedDate and attributeValue from it.

So to do this, I am using ByteBuffer with BigEndian byte order format.

I have put up this code together -

public static void main(String[] args) throws Exception {

        String text = "Byte Buffer Test";
        byte[] attributeValue = text.getBytes();

        long lastModifiedDate = 1289811105109L;
        short employeeId = 32767;

        int size = 2 + 8 + 4 + attributeValue.length; // short is 2 bytes, long 8 and int 4

        ByteBuffer bbuf = ByteBuffer.allocate(size); 

        bbuf.order(ByteOrder.BIG_ENDIAN);
        bbuf.putShort(employeeId);
        bbuf.putLong(lastModifiedDate);
        bbuf.putInt(attributeValue.length);
        bbuf.put(attributeValue);

        bbuf.rewind();

        // best approach is copy the internal buffer
        byte[] bytesToStore = new byte[size];
        bbuf.get(bytesToStore);

        // write bytesToStore in Cassandra...

        // Now retrieve the Byte Array data from Cassandra and deserialize it...
        byte[] allWrittenBytesTest = bytesToStore;//magicFunctionToRetrieveDataFromCassandra();

        ByteBuffer bb = ByteBuffer.wrap(allWrittenBytesTest);

        bb.order(ByteOrder.BIG_ENDIAN);
        bb.rewind();

        short extractEmployeeId = bb.getShort();
        long extractLastModifiedDate = bb.getLong();
        int extractAttributeValueLength = bb.getInt();
        byte[] extractAttributeValue = new byte[extractAttributeValueLength];

        bb.get(extractAttributeValue); // read attributeValue from the remaining buffer

        System.out.println(extractEmployeeId);
        System.out.println(extractLastModifiedDate);
        System.out.println(new String(extractAttributeValue));

}

Is there any better way of doing this, the way I am doing it currently? Or some minor improvements that we can do it here??

This is the first time I am using ByteBuffer so having little bit problem...

Can anyone take a look and let me know whether this is the right way to use ByteBuffer?

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • 1
    how do you expect Cassandra to determine the length of `attributeValue` array? – Alexei Kaigorodov Oct 11 '13 at 07:19
  • @AlexeiKaigorodov : I am not sure, I understand your question.. Did cassandra need to determine the length of attributeValue array? We just need to store it and then retrieve it? – arsenal Oct 11 '13 at 17:13

1 Answers1

1

The default order is always BIG_ENDIAN, so you don't meed to set it. Also when you wrap() is is already rewind()ed.

Instead of copying the underlying array, I would use the underlying array.

Replace

    bbuf.rewind();

    // best approach is copy the internal buffer
    byte[] bytesToStore = new byte[size];
    bbuf.get(bytesToStore);

with

    byte[] bytesToStore = bbuf.array();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • :Thanks a lot Peter.. I will definitely make those changes... Is there anything else you are thinking is not right? Or you want me to improve on? Thanks for the help.. – arsenal Oct 11 '13 at 17:14
  • Instead of using byte[] and heap ByteBuffer you can use a direct ByteBuffer to improve performance. – Peter Lawrey Oct 11 '13 at 21:14
  • Sorry I got confused.. Can you provide an example how to do that basis on my code? Thanks. – arsenal Oct 11 '13 at 21:44
  • @TechGeeky Unless Cassandra supports direct buffers, I wouldn't bother as it won't help. – Peter Lawrey Oct 11 '13 at 21:47
  • I am not sure on this but I guess this is fine now.. One more thing, how about on the deserialization part? Do we need to make any change as well? Or deserialization part looks good.? Thanks.. – arsenal Oct 12 '13 at 06:50