一个简单的java对象序列化工具类
2012-10-17 14:50
495 查看
/** * serialized or compressed data. * * @author dongjian */ @SuppressWarnings("unchecked") public final class Transcoder { private static Log log = LogFactory.getLog(Transcoder.class); protected static String charset = "UTF-8"; private Transcoder() { } public static <T> T decodeObject(byte[] b) { return (T) deserialize(decompress(b)); } public static byte[] encodeObject(Object o) { byte[] b = serialize(o); return compress(b); } /** * Decode the string with the current character set. */ public static String decodeString(byte[] data) { String rv = null; try { if (data != null) { rv = new String(data, charset); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } return rv; } /** * Encode a string into the current character set. */ public static byte[] encodeString(String in) { byte[] rv = null; try { rv = in.getBytes(charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } return rv; } /** * Get the bytes representing the given serialized object. */ private static byte[] serialize(Object o) { if (o == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(o); os.close(); bos.close(); rv = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { CloseUtil.close(os); CloseUtil.close(bos); } return rv; } /** * Get the object represented by the given serialized bytes. */ private static Object deserialize(byte[] in) { Object rv = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); rv = is.readObject(); is.close(); bis.close(); } } catch (IOException e) { log.warn("Caught IOException decoding " + (in == null ? 0 : in.length) + " bytes of data", e); } catch (ClassNotFoundException e) { log.warn("Caught CNFE decoding " + (in == null ? 0 : in.length) + " bytes of data", e); } finally { CloseUtil.close(is); CloseUtil.close(bis); } return rv; } /** * Compress the given array of bytes. */ private static byte[] compress(byte[] in) { if (in == null) { throw new NullPointerException("Can't compress null"); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gz = null; try { gz = new GZIPOutputStream(bos); gz.write(in); } catch (IOException e) { throw new RuntimeException("IO exception compressing data", e); } finally { CloseUtil.close(gz); CloseUtil.close(bos); } byte[] rv = bos.toByteArray(); log.debug("Compressed " + in.length + " bytes to " + rv.length); return rv; } /** * Decompress the given array of bytes. * * @return null if the bytes cannot be decompressed */ private static byte[] decompress(byte[] in) { ByteArrayOutputStream bos = null; if (in != null) { ByteArrayInputStream bis = new ByteArrayInputStream(in); bos = new ByteArrayOutputStream(); GZIPInputStream gis = null; try { gis = new GZIPInputStream(bis); byte[] buf = new byte[8192]; int r = -1; while ((r = gis.read(buf)) > 0) { bos.write(buf, 0, r); } } catch (IOException e) { log.warn("Failed to decompress data", e); bos = null; } finally { CloseUtil.close(gis); CloseUtil.close(bis); CloseUtil.close(bos); } } return bos == null ? null : bos.toByteArray(); } private static final class CloseUtil { private CloseUtil() { } public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { log.warn("Unable to close " + e.getMessage(), e); } } } } }
相关文章推荐
- 一个关于Json数据List和对象嵌套反序列化的工具类,java(20151130)
- java 对象序列化工具类
- 简单测试java里一个对象的初始化是按怎样的顺序执行的
- Java对象序列化的简单总结
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- 一个序列化的POJO对象-使用commons-lang的工具类
- Java序列化对象的一个使用案例-使用Http发送对象
- 初学java--利用面向对象概念做一个简单的新闻系统
- 如何序列化和反序列化一个java对象
- Java对象序列化工具类
- Java序列化 如何把多个对象存储在一个文件中
- .net中的对象序列化(1): 序列化是什么, 以及一个简单的例子
- Java中Json对象序列化与反序列化工具类
- Java 序列化对象的一个使用案例
- Java中如何序列化一个对象
- 简单设计一个 Java 数据库工具类(第一版,以废弃)
- 一个简单的Java序列化的例子
- 一个简单的Java模板工具类(二)—简单表达式解析实现
- .net中的对象序列化(1): 序列化是什么, 以及一个简单的例子
- java一个简单的自定义异常工具类