对象字节数组转换工具类
2017-08-10 16:10
204 查看
package cn.tootoo.kzh.util.redis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * ClassName: ObjectsTranscoder * * @Description: 对象字节数组转换工具类 */ class ObjectsTranscoder { private static final Logger LOGGER = LoggerFactory .getLogger(ObjectsTranscoder.class); private ObjectsTranscoder() { } private static ObjectsTranscoder objectsTranscoder; public static ObjectsTranscoder getInstance() { if (objectsTranscoder == null) { objectsTranscoder = new ObjectsTranscoder(); } return objectsTranscoder; } public byte[] serialize(Object value) { if (value == null) { LOGGER.error("null value error"); } byte[] result = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); os.close(); bos.close(); result = bos.toByteArray(); } catch (IOException e) { LOGGER.error("Non-serializable object", e); } finally { close(os); close(bos); } return result; } public Object deserialize(byte[] in) { Object result = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); result = is.readObject(); is.close(); bis.close(); } } catch (IOException e) { LOGGER.error("convert byte to Object error", e); } catch (ClassNotFoundException e) { LOGGER.error("convert byte to Object error", e); } finally { close(is); close(bis); } return result; } private static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { LOGGER.info("Unable to close " + closeable, e); } } } }
相关文章推荐
- 对象与字节数组之间的转换工具类
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
- java中对象与字节数组相互转换
- android中对象与字节数组、字符串之间的转换
- java中对象与字节数组相互转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- jedis实现redis的消息队列、发布对象消息、字节数组与字符串相互转换
- activity 自定义对象传递,对象和对象字节数组互相转换
- java对象转换为字节数组
- java中对象与字节数组相互转换
- java中对象与字节数组相互转换
- Android学习之Bitmap对象与字节数组相互转换
- jedis实现redis的消息队列、发布对象消息、字节数组与字符串相互转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android学习之Bitmap对象与字节数组相互转换
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- php数组工具类 php数组与对象、字符串的转换实例
- java中对象与字节数组相互转换
- JSON对象转换成Byte(字节)数组