PageRenderTime 205ms CodeModel.GetById 81ms app.highlight 3ms RepoModel.GetById 119ms app.codeStats 0ms

/src/mpv5/utils/ui/Serializer.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 46 lines | 34 code | 4 blank | 8 comment | 0 complexity | 94ee3532da9997cba6c6970d7c6da54a MD5 | raw file
 1/*
 2 * To change this template, choose Tools | Templates
 3 * and open the template in the editor.
 4 */
 5package mpv5.utils.ui;
 6
 7import java.beans.XMLDecoder;
 8import java.beans.XMLEncoder;
 9import java.io.ByteArrayInputStream;
10import java.io.ByteArrayOutputStream;
11import java.io.Serializable;
12import java.io.UnsupportedEncodingException;
13import mpv5.logging.Log;
14
15/**
16 *
17 * @author Andreas
18 */
19public class Serializer {
20
21    public static String serialize(Serializable whatever) {
22        ByteArrayOutputStream io = new ByteArrayOutputStream();
23        XMLEncoder encoder = new XMLEncoder(io);
24        encoder.writeObject(whatever);
25        encoder.flush();
26        encoder.close();
27        try {
28            return io.toString("UTF-8");
29        } catch (UnsupportedEncodingException ex) {
30            return "";
31        }
32    }
33
34    public static Serializable deserialize(String whatever) {
35        try {
36            ByteArrayInputStream io = new ByteArrayInputStream(whatever.getBytes("UTF-8"));
37            XMLDecoder decoder = new XMLDecoder(io);
38            Serializable obj = (Serializable) decoder.readObject();
39            decoder.close();
40            return obj;
41        } catch (Exception e) {
42            Log.Debug(e);
43            return null;
44        }
45    }
46}