Reputation: 587
I have a Java app that lets users to store the data in database but while storing I store those data as byte array which is same as cassandra, Now when I get back the byte array I want to convert those data as User saved, means if user saved as Long I want to show long value, or if User saved String I want to show String value. Now If I converts all the bytes array fields to string, apparently long bytes array will be shown as wild char.string fields would be fine.
Please suggest me how to solve this issue in java. Its similar to cassandra way of storing data.cassandra store all the data as byte array.
Basically I want to know the datatype of the byte array.
Upvotes: 0
Views: 5374
Reputation: 6418
I'd recommend to serialize data in some format, that stores type info, like BSON: http://bsonspec.org/ or Smile: http://wiki.fasterxml.com/SmileFormat
In this case, deserialization will restore type info, and after deserialization you'll get Object of correct type.
These formats are very compact: type info takes only a few extra bytes, as opposed to java standart serialization, that requires several hundreeds bytes to serialize the simplest object.
Upvotes: 0
Reputation: 1276
Your question isn't very clear as to what exactly you want but...
You could come up with some custom scheme for doing this like the first byte of the array indicates what type and the remaining bytes are the actual data. You then need to write code to convert the byte[1] through byte[length-1] into that given type. It seems like a lot of work to me.
I would probably try using object serialization. It basically does what you are asking here w/o any custom code on your end.
public static void main(String[] args) throws Exception {
String strValue = "hello";
int myInt = 3;
long myLong = 45677;
short myShort = 1;
double myFloat = 4.5;
serializeThenDeserialize(strValue);
serializeThenDeserialize(myInt);
serializeThenDeserialize(myLong);
serializeThenDeserialize(myShort);
serializeThenDeserialize(myFloat);
}
private static void serializeThenDeserialize(Object value) throws Exception {
System.out.println("Input Type is " + value.getClass() + " with value '" + value + "'");
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteArrayStream);
out.writeObject(value);
out.close();
byte[] objectAsBytes = byteArrayStream.toByteArray();
// Persist here..
// Now lets deserialize the byte array
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objectAsBytes));
Object deserializedValue = in.readObject();
in.close();
System.out.println("Deserialized Type is " + deserializedValue.getClass() + " with Value '" + deserializedValue + "'");
System.out.println();
}
When running this it acts like we want. Data is returned and type is maintained.
Input Type is class java.lang.String with value 'hello'
Deserialized Type is class java.lang.String with Value 'hello'
Input Type is class java.lang.Integer with value '3'
Deserialized Type is class java.lang.Integer with Value '3'
Input Type is class java.lang.Long with value '45677'
Deserialized Type is class java.lang.Long with Value '45677'
Input Type is class java.lang.Short with value '1'
Deserialized Type is class java.lang.Short with Value '1'
Input Type is class java.lang.Double with value '4.5'
Deserialized Type is class java.lang.Double with Value '4.5'
The good thing about this is that it works for all Java objects. The bad thing is that Java object serialization can get a little harry as the objects you are storing evolve (i.e. you remove methods, field, compile w/ different JDK, etc). If you stick to primitives you should have no problems w/ this though. If you serialize your own objects you should read more on compatible and incompatible changes here.
Upvotes: 1