当前位置:   article > 正文

一个简单的java对象序列化工具类

一个简单的java对象序列化工具类
  1. /**
  2. * serialized or compressed data.
  3. *
  4. * @author dongjian
  5. */
  6. @SuppressWarnings("unchecked")
  7. public final class Transcoder {
  8. private static Log log = LogFactory.getLog(Transcoder.class);
  9. protected static String charset = "UTF-8";
  10. private Transcoder() { }
  11. public static <T> T decodeObject(byte[] b) {
  12. return (T) deserialize(decompress(b));
  13. }
  14. public static byte[] encodeObject(Object o) {
  15. byte[] b = serialize(o);
  16. return compress(b);
  17. }
  18. /**
  19. * Decode the string with the current character set.
  20. */
  21. public static String decodeString(byte[] data) {
  22. String rv = null;
  23. try {
  24. if (data != null) {
  25. rv = new String(data, charset);
  26. }
  27. } catch (UnsupportedEncodingException e) {
  28. throw new RuntimeException(e);
  29. }
  30. return rv;
  31. }
  32. /**
  33. * Encode a string into the current character set.
  34. */
  35. public static byte[] encodeString(String in) {
  36. byte[] rv = null;
  37. try {
  38. rv = in.getBytes(charset);
  39. } catch (UnsupportedEncodingException e) {
  40. throw new RuntimeException(e);
  41. }
  42. return rv;
  43. }
  44. /**
  45. * Get the bytes representing the given serialized object.
  46. */
  47. private static byte[] serialize(Object o) {
  48. if (o == null) {
  49. throw new NullPointerException("Can't serialize null");
  50. }
  51. byte[] rv = null;
  52. ByteArrayOutputStream bos = null;
  53. ObjectOutputStream os = null;
  54. try {
  55. bos = new ByteArrayOutputStream();
  56. os = new ObjectOutputStream(bos);
  57. os.writeObject(o);
  58. os.close();
  59. bos.close();
  60. rv = bos.toByteArray();
  61. } catch (IOException e) {
  62. throw new IllegalArgumentException("Non-serializable object", e);
  63. } finally {
  64. CloseUtil.close(os);
  65. CloseUtil.close(bos);
  66. }
  67. return rv;
  68. }
  69. /**
  70. * Get the object represented by the given serialized bytes.
  71. */
  72. private static Object deserialize(byte[] in) {
  73. Object rv = null;
  74. ByteArrayInputStream bis = null;
  75. ObjectInputStream is = null;
  76. try {
  77. if (in != null) {
  78. bis = new ByteArrayInputStream(in);
  79. is = new ObjectInputStream(bis);
  80. rv = is.readObject();
  81. is.close();
  82. bis.close();
  83. }
  84. } catch (IOException e) {
  85. log.warn("Caught IOException decoding "
  86. + (in == null ? 0 : in.length) + " bytes of data", e);
  87. } catch (ClassNotFoundException e) {
  88. log.warn("Caught CNFE decoding " + (in == null ? 0 : in.length)
  89. + " bytes of data", e);
  90. } finally {
  91. CloseUtil.close(is);
  92. CloseUtil.close(bis);
  93. }
  94. return rv;
  95. }
  96. /**
  97. * Compress the given array of bytes.
  98. */
  99. private static byte[] compress(byte[] in) {
  100. if (in == null) {
  101. throw new NullPointerException("Can't compress null");
  102. }
  103. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  104. GZIPOutputStream gz = null;
  105. try {
  106. gz = new GZIPOutputStream(bos);
  107. gz.write(in);
  108. } catch (IOException e) {
  109. throw new RuntimeException("IO exception compressing data", e);
  110. } finally {
  111. CloseUtil.close(gz);
  112. CloseUtil.close(bos);
  113. }
  114. byte[] rv = bos.toByteArray();
  115. log.debug("Compressed " + in.length + " bytes to " + rv.length);
  116. return rv;
  117. }
  118. /**
  119. * Decompress the given array of bytes.
  120. *
  121. * @return null if the bytes cannot be decompressed
  122. */
  123. private static byte[] decompress(byte[] in) {
  124. ByteArrayOutputStream bos = null;
  125. if (in != null) {
  126. ByteArrayInputStream bis = new ByteArrayInputStream(in);
  127. bos = new ByteArrayOutputStream();
  128. GZIPInputStream gis = null;
  129. try {
  130. gis = new GZIPInputStream(bis);
  131. byte[] buf = new byte[8192];
  132. int r = -1;
  133. while ((r = gis.read(buf)) > 0) {
  134. bos.write(buf, 0, r);
  135. }
  136. } catch (IOException e) {
  137. log.warn("Failed to decompress data", e);
  138. bos = null;
  139. } finally {
  140. CloseUtil.close(gis);
  141. CloseUtil.close(bis);
  142. CloseUtil.close(bos);
  143. }
  144. }
  145. return bos == null ? null : bos.toByteArray();
  146. }
  147. private static final class CloseUtil {
  148. private CloseUtil() { }
  149. public static void close(Closeable closeable) {
  150. if (closeable != null) {
  151. try {
  152. closeable.close();
  153. } catch (Exception e) {
  154. log.warn("Unable to close " + e.getMessage(), e);
  155. }
  156. }
  157. }
  158. }
  159. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58550
推荐阅读
相关标签
  

闽ICP备14008679号