Guest User

Untitled

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public static String serializeObject(Object obj) {
  2.  
  3. String out = null;
  4. if (obj != null) {
  5. try {
  6. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  7. ObjectOutputStream oos = new ObjectOutputStream(baos);
  8. oos.writeObject(obj);
  9. oos.close();
  10. out = new String(baos.toByteArray(), "UTF8");
  11. } catch (IOException e) {
  12. return null;
  13. }
  14. }
  15. return out;
  16. }
  17.  
  18. public static Object deserializeObject(String str) {
  19.  
  20. Object out = null;
  21. if (str != null) {
  22. try {
  23. byte[] bytes = str.getBytes();
  24. ByteArrayInputStream bios = new ByteArrayInputStream(bytes);
  25. ObjectInputStream ois = new ObjectInputStream(bios);
  26. out = ois.readObject();
  27. ois.close();
  28. } catch (IOException e) {
  29. return null;
  30. }catch (ClassNotFoundException e) {
  31. return null;
  32. }
  33. }
  34. return out;
  35. }
Add Comment
Please, Sign In to add comment