Java:对象为byte []和byte []对象转换器(对于Tokyo Cabinet) [英] Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)
本文介绍了Java:对象为byte []和byte []对象转换器(对于Tokyo Cabinet)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将对象转换为byte []以存储在Tokyo Cabinet键值存储中。
我还需要在从键值存储区读取时将字节[]取消对象。
I need to convert objects to a byte[] to be stored in the Tokyo Cabinet key-value store. I also need to unbyte the byte[] to an Object when reading from the key-value store.
是否有任何包可以帮助我有这个任务?或者自己实现它的最佳解决方案是什么?
Are there any packages out there that will help me with this task? Or would the best solution to implement it myself?
推荐答案
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();
}
这篇关于Java:对象为byte []和byte []对象转换器(对于Tokyo Cabinet)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文