123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package com.nuliji.tools.shiro;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.*;
- /**
- * Created by gaojie on 2017/11/7.
- */
- public class SerializeUtils {
- private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class);
- /**
- * 反序列化
- * @param bytes
- * @return
- */
- public static Object deserialize(byte[] bytes) {
- Object result = null;
- if (isEmpty(bytes)) {
- return null;
- }
- try {
- ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
- try {
- ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
- try {
- result = objectInputStream.readObject();
- }
- catch (ClassNotFoundException ex) {
- throw new Exception("Failed to deserialize object type", ex);
- }
- }
- catch (Throwable ex) {
- throw new Exception("Failed to deserialize", ex);
- }
- } catch (Exception e) {
- logger.error("Failed to deserialize",e);
- }
- return result;
- }
- public static boolean isEmpty(byte[] data) {
- return (data == null || data.length == 0);
- }
- /**
- * 序列化
- * @param object
- * @return
- */
- public static byte[] serialize(Object object) {
- byte[] result = null;
- if (object == null) {
- return new byte[0];
- }
- try {
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
- try {
- if (!(object instanceof Serializable)) {
- throw new IllegalArgumentException(SerializeUtils.class.getSimpleName() + " requires a Serializable payload " +
- "but received an object of type [" + object.getClass().getName() + "]");
- }
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
- objectOutputStream.writeObject(object);
- objectOutputStream.flush();
- result = byteStream.toByteArray();
- }
- catch (Throwable ex) {
- throw new Exception("Failed to serialize", ex);
- }
- } catch (Exception ex) {
- logger.error("Failed to serialize",ex);
- }
- return result;
- }
- }
|