/src/mpv5/utils/ui/Serializer.java
Java | 46 lines | 34 code | 4 blank | 8 comment | 0 complexity | 94ee3532da9997cba6c6970d7c6da54a MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package mpv5.utils.ui;
- import java.beans.XMLDecoder;
- import java.beans.XMLEncoder;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.Serializable;
- import java.io.UnsupportedEncodingException;
- import mpv5.logging.Log;
- /**
- *
- * @author Andreas
- */
- public class Serializer {
- public static String serialize(Serializable whatever) {
- ByteArrayOutputStream io = new ByteArrayOutputStream();
- XMLEncoder encoder = new XMLEncoder(io);
- encoder.writeObject(whatever);
- encoder.flush();
- encoder.close();
- try {
- return io.toString("UTF-8");
- } catch (UnsupportedEncodingException ex) {
- return "";
- }
- }
- public static Serializable deserialize(String whatever) {
- try {
- ByteArrayInputStream io = new ByteArrayInputStream(whatever.getBytes("UTF-8"));
- XMLDecoder decoder = new XMLDecoder(io);
- Serializable obj = (Serializable) decoder.readObject();
- decoder.close();
- return obj;
- } catch (Exception e) {
- Log.Debug(e);
- return null;
- }
- }
- }