I need to write Byte Array
value into Cassandra using Java code. Then I will be having my C++ program which will retrieve that Byte Array data from Cassandra and then it will deserialize it.
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.
I am not sure whether I should use Big Endian here in my Java code while writing to Cassandra so that C++ code get simplified while reading it back?
I have given a try on the Java side to make sure it is following certain format (Big Endian) while writing everything into a single Byte Array and then this Byte Array will be written back to Cassandra as well but not sure whether this is right or not?
public static void main(String[] args) throws Exception {
String os = "Byte Array Test";
byte[] attributeValue = os.getBytes();
long lastModifiedDate = 1379811105109L;
short employeeId = 32767;
ByteArrayOutputStream byteOsTest = new ByteArrayOutputStream();
DataOutputStream outTest = new DataOutputStream(byteOsTest);
// merging everything into one Byte Array here
outTest.writeShort(employeeId);
outTest.writeLong(lastModifiedDate);
outTest.writeInt(attributeValue.length);
outTest.write(attributeValue);
byte[] allWrittenBytesTest = byteOsTest.toByteArray();
// initially I was writing allWrittenBytesTest into Cassandra...
ByteBuffer bb = ByteBuffer.wrap(allWrittenBytesTest).order(ByteOrder.BIG_ENDIAN);
// now what value I should write into Cassandra?
// or does this even looks right?
// And now how to deserialize it?
}
Can anyone help me with this ByteBuffer thing here? Thanks..
I might be mising minute details about Byte Buffer here as this is the first time I am working with it..
- First of all, should I be using ByteByffer here at all in my use case?
- Secondly, if yes, then what's the best way to use it within my use case...?
The only thing that I am trying to make sure is, I am writing correctly into Cassandra by following Big-Endians byte order format so that on C++ side, I am not facing any problem at all while deserializing that Byte Array...