SerializeUtils.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.kmall.admin.cuspay.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.*;
  5. public class SerializeUtils {
  6. private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class);
  7. public static Object deserialize(byte[] bytes) {
  8. return deserialize(bytes, Object.class);
  9. }
  10. /**
  11. * 反序列化
  12. * @param bytes
  13. * @return
  14. */
  15. public static <T> T deserialize(byte[] bytes, Class<T>... clazz) {
  16. Object result = null;
  17. if (isEmpty(bytes)) {
  18. return null;
  19. }
  20. try {
  21. ByteArrayInputStream byteStream = null;
  22. ObjectInputStream objectInputStream = null;
  23. try {
  24. try {
  25. byteStream = new ByteArrayInputStream(bytes);
  26. objectInputStream = new ObjectInputStream(byteStream);
  27. result = objectInputStream.readObject();
  28. }
  29. catch (ClassNotFoundException ex) {
  30. throw new Exception("Failed to deserialize object type", ex);
  31. }finally {
  32. close(objectInputStream);
  33. close(byteStream);
  34. }
  35. }
  36. catch (Throwable ex) {
  37. throw new Exception("Failed to deserialize", ex);
  38. }finally {
  39. close(objectInputStream);
  40. close(byteStream);
  41. }
  42. } catch (Exception e) {
  43. logger.error("Failed to deserialize",e);
  44. }
  45. return (T)result;
  46. }
  47. public static boolean isEmpty(byte[] data) {
  48. return (data == null || data.length == 0);
  49. }
  50. public static byte[] serialize(Object object) {
  51. return serialize(object, Object.class);
  52. }
  53. /**
  54. * 序列化
  55. * @param object
  56. * @return
  57. */
  58. public static <T> byte[] serialize(T object, Class<T> clazz) {
  59. byte[] result = null;
  60. if (object == null) {
  61. return new byte[0];
  62. }
  63. try {
  64. ByteArrayOutputStream byteStream = null;
  65. ObjectOutputStream objectOutputStream = null;
  66. try {
  67. if (!(object instanceof Serializable)) {
  68. throw new IllegalArgumentException(SerializeUtils.class.getSimpleName() + " requires a Serializable payload " +
  69. "but received an object of type [" + object.getClass().getName() + "]");
  70. }
  71. byteStream = new ByteArrayOutputStream(128);
  72. objectOutputStream = new ObjectOutputStream(byteStream);
  73. objectOutputStream.writeObject(object);
  74. objectOutputStream.flush();
  75. result = byteStream.toByteArray();
  76. }
  77. catch (Throwable ex) {
  78. throw new Exception("Failed to serialize", ex);
  79. }finally {
  80. close(objectOutputStream);
  81. close(byteStream);
  82. }
  83. } catch (Exception ex) {
  84. logger.error("Failed to serialize",ex);
  85. }
  86. return result;
  87. }
  88. private static void close(Closeable closeable) {
  89. if (closeable != null) {
  90. try {
  91. closeable.close();
  92. } catch (IOException e) {
  93. logger.error("close stream error");
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. }