Java: object for byte [] and byte [] for object converter (for Tokyo Cabinet)

I need to convert the objects to byte [], which will be stored in the key store for the Tokyo Cabinet store. I also need to remove byte [] in the object when reading from the key value store.

Are there packages that will help me in this task? Or the best solution to implement it?

+44
java serialization byte bytearray tokyo-cabinet
Sep 17 '10 at 14:10
source share
4 answers
public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(out); os.writeObject(obj); return out.toByteArray(); } public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException { ByteArrayInputStream in = new ByteArrayInputStream(data); ObjectInputStream is = new ObjectInputStream(in); return is.readObject(); } 
+117
Sep 17 '10 at 14:33
source share

If your class extends Serializable , you can write and read objects through ByteArrayOutputStream , which I usually do.

+9
Sep 17 '10 at 14:14
source share

Use serialize and deserialize in SerializationUtils from commons-lang .

+6
Mar 31 '14 at 5:16
source share

You can see how Hector does it for Cassandra, where the goal is the same - convert everything to and from byte[] to store / retrieve from NoSQL database - see here . For primitive types (+ String) there are special Serializers, otherwise there is a common ObjectSerializer (expecting Serializable and using ObjectOutputStream ). Of course, you can use it only for everything, but in serialized form there may be redundant metadata.

I think you can copy the whole package and use it.

+5
Sep 17 '10 at 14:14
source share



All Articles