com.duoec.doc.doclet.util.SerializeUtils Maven / Gradle / Ivy
The newest version!
package com.duoec.doc.doclet.util;
import java.io.*;
import java.util.Base64;
/**
* @author xuwenzhen
*/
public class SerializeUtils {
private SerializeUtils() {
}
public static String serializeToString(Object obj) {
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
) {
oos.writeObject(obj);
return Base64.getEncoder().encodeToString(bos.toByteArray());
} catch (IOException e) {
throw new RuntimeException("序列化对象失败!", e);
}
}
public static T deserializeFromString(String serializedString) {
byte[] data = Base64.getDecoder().decode(serializedString);
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) {
return (T) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("反序列化对象失败!", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy