code_junkie

How to serialize an object into a string

Nov 11th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. LinkedList<Diff_match_patch.Patch> patches = // whatever...
  2. FileOutputStream fileStream = new FileOutputStream("foo.ser");
  3. ObjectOutputStream os = new ObjectOutputStream(fileStream);
  4. os.writeObject(patches1);
  5. os.close();
  6.  
  7. FileInputStream fileInputStream = new FileInputStream("foo.ser");
  8. ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
  9. Object one = oInputStream.readObject();
  10. LinkedList<Diff_match_patch.Patch> patches3 = (LinkedList<Diff_match_patch.Patch>) one;
  11. os.close();
  12.  
  13. import java.util.*;
  14. import java.io.*;
  15.  
  16. /**
  17. * Usage sample serializing SomeClass instance
  18. */
  19. public class ToStringSample {
  20. public static void main( String [] args ) throws IOException,
  21. ClassNotFoundException {
  22. String string = toString( new SomeClass() );
  23. System.out.println(" Encoded serialized version " );
  24. System.out.println( string );
  25. SomeClass some = ( SomeClass ) fromString( string );
  26. System.out.println( "nnReconstituted object");
  27. System.out.println( some );
  28.  
  29.  
  30. }
  31.  
  32. /** Read the object from Base64 string. */
  33. private static Object fromString( String s ) throws IOException ,
  34. ClassNotFoundException {
  35. byte [] data = Base64Coder.decode( s );
  36. ObjectInputStream ois = new ObjectInputStream(
  37. new ByteArrayInputStream( data ) );
  38. Object o = ois.readObject();
  39. ois.close();
  40. return o;
  41. }
  42.  
  43. /** Write the object to a Base64 string. */
  44. private static String toString( Serializable o ) throws IOException {
  45. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46. ObjectOutputStream oos = new ObjectOutputStream( baos );
  47. oos.writeObject( o );
  48. oos.close();
  49. return new String( Base64Coder.encode( baos.toByteArray() ) );
  50. }
  51. }
  52.  
  53. /** Test subject. A very simple class. */
  54. class SomeClass implements Serializable{
  55. int i = Integer.MAX_VALUE;
  56. String s = "ABCDEFGHIJKLMNOP";
  57. Double d = new Double( -1.0 );
  58. public String toString(){
  59. return "SomeClass instance says: Don't worry, " +
  60. "I'm healty. Look, my data is i = " + i + ", s = " + s + ", d = " + d;
  61. }
  62. }
  63.  
  64. C:oreyessamplesjavaencode>javac *.java
  65.  
  66. C:oreyessamplesjavaencode>java ToStringSample
  67. Encoded serialized version
  68. rO0ABXNyAAlTb21lQ2xhc3PSHbLk6OgfswIAA0kAAWlMAAFkdAASTG
  69. phdmEvbGFuZy9Eb3VibGU7TAABc3QAEkxqYXZhL2xhbmcvU3RyaW5nO3
  70. hwf////3NyABBqYXZhLmxhbmcuRG91YmxlgLPCSi
  71. lr+wQCAAFEAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQ
  72. uU4IsCAAB4cL/wAAAAAAAAdAAQQUJDREVGR0hJSktMTU5PUA==
  73.  
  74.  
  75. Reconstituted object
  76. SomeClass instance says: Don't worry, I'm healty. Look,
  77. my data is i = 2147483647, s = ABCDEFGHIJKLMNOP, d = -1.0
  78.  
  79. /// Serialize an instance of an object to a string.
  80. public string Serialize(object instance)
  81. {
  82. StringBuilder sb = new StringBuilder();
  83. StringWriter sw = new StringWriter(sb);
  84. XmlSerializer serializer = new XmlSerializer(instance.GetType());
  85. serializer.Serialize(sw, instance);
  86. return sb.ToString();
  87. }
  88.  
  89. LinkedList<Patch> patches1 = diff.patch_make(text2, text1);
  90. try {
  91. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  92. ObjectOutputStream os = new ObjectOutputStream(bos);
  93. os.writeObject(patches1);
  94. String serialized_patches1 = bos.toString();
  95. os.close();
  96.  
  97.  
  98. ByteArrayInputStream bis = new ByteArrayInputStream(serialized_patches1.getBytes());
  99. ObjectInputStream oInputStream = new ObjectInputStream(bis);
  100. LinkedList<Patch> restored_patches1 = (LinkedList<Patch>) oInputStream.readObject();
  101.  
  102.  
  103.  
  104. // patches1 equals restored_patches1
  105. oInputStream.close();
  106. } catch(Exception ex) {
  107. ex.printStackTrace();
  108. }
Add Comment
Please, Sign In to add comment