All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sghd.common.utils.codec.SerializeUtil Maven / Gradle / Ivy

The newest version!
package com.sghd.common.utils.codec;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Copyright (c) 水果互动
* All Rights Reserved
* * 对象序列化工具类 * * @date 2018年4月25日 * @author wanggj([email protected]) */ @SuppressWarnings("unchecked") public class SerializeUtil { public static final String CHARSET = "utf-8"; /** * 将对象序列化为字节 * * @param object * @return */ public static byte[] serialize(T object) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(object); byte[] bytes = bos.toByteArray(); oos.close(); bos.close(); return bytes; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 将字节反序列化为对象 * * @param bytes * @return */ public static T serialize(byte[] bytes) { try { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); Object object = ois.readObject(); return (T) object; } catch (Exception e) { e.printStackTrace(); return null; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy