PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 2ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package mpv5.utils.ui;
  6. import java.beans.XMLDecoder;
  7. import java.beans.XMLEncoder;
  8. import java.io.ByteArrayInputStream;
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.Serializable;
  11. import java.io.UnsupportedEncodingException;
  12. import mpv5.logging.Log;
  13. /**
  14. *
  15. * @author Andreas
  16. */
  17. public class Serializer {
  18. public static String serialize(Serializable whatever) {
  19. ByteArrayOutputStream io = new ByteArrayOutputStream();
  20. XMLEncoder encoder = new XMLEncoder(io);
  21. encoder.writeObject(whatever);
  22. encoder.flush();
  23. encoder.close();
  24. try {
  25. return io.toString("UTF-8");
  26. } catch (UnsupportedEncodingException ex) {
  27. return "";
  28. }
  29. }
  30. public static Serializable deserialize(String whatever) {
  31. try {
  32. ByteArrayInputStream io = new ByteArrayInputStream(whatever.getBytes("UTF-8"));
  33. XMLDecoder decoder = new XMLDecoder(io);
  34. Serializable obj = (Serializable) decoder.readObject();
  35. decoder.close();
  36. return obj;
  37. } catch (Exception e) {
  38. Log.Debug(e);
  39. return null;
  40. }
  41. }
  42. }