Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LinkedList<Diff_match_patch.Patch> patches = // whatever...
- FileOutputStream fileStream = new FileOutputStream("foo.ser");
- ObjectOutputStream os = new ObjectOutputStream(fileStream);
- os.writeObject(patches1);
- os.close();
- FileInputStream fileInputStream = new FileInputStream("foo.ser");
- ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream);
- Object one = oInputStream.readObject();
- LinkedList<Diff_match_patch.Patch> patches3 = (LinkedList<Diff_match_patch.Patch>) one;
- os.close();
- import java.util.*;
- import java.io.*;
- /**
- * Usage sample serializing SomeClass instance
- */
- public class ToStringSample {
- public static void main( String [] args ) throws IOException,
- ClassNotFoundException {
- String string = toString( new SomeClass() );
- System.out.println(" Encoded serialized version " );
- System.out.println( string );
- SomeClass some = ( SomeClass ) fromString( string );
- System.out.println( "nnReconstituted object");
- System.out.println( some );
- }
- /** Read the object from Base64 string. */
- private static Object fromString( String s ) throws IOException ,
- ClassNotFoundException {
- byte [] data = Base64Coder.decode( s );
- ObjectInputStream ois = new ObjectInputStream(
- new ByteArrayInputStream( data ) );
- Object o = ois.readObject();
- ois.close();
- return o;
- }
- /** Write the object to a Base64 string. */
- private static String toString( Serializable o ) throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream( baos );
- oos.writeObject( o );
- oos.close();
- return new String( Base64Coder.encode( baos.toByteArray() ) );
- }
- }
- /** Test subject. A very simple class. */
- class SomeClass implements Serializable{
- int i = Integer.MAX_VALUE;
- String s = "ABCDEFGHIJKLMNOP";
- Double d = new Double( -1.0 );
- public String toString(){
- return "SomeClass instance says: Don't worry, " +
- "I'm healty. Look, my data is i = " + i + ", s = " + s + ", d = " + d;
- }
- }
- C:oreyessamplesjavaencode>javac *.java
- C:oreyessamplesjavaencode>java ToStringSample
- Encoded serialized version
- rO0ABXNyAAlTb21lQ2xhc3PSHbLk6OgfswIAA0kAAWlMAAFkdAASTG
- phdmEvbGFuZy9Eb3VibGU7TAABc3QAEkxqYXZhL2xhbmcvU3RyaW5nO3
- hwf////3NyABBqYXZhLmxhbmcuRG91YmxlgLPCSi
- lr+wQCAAFEAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQ
- uU4IsCAAB4cL/wAAAAAAAAdAAQQUJDREVGR0hJSktMTU5PUA==
- Reconstituted object
- SomeClass instance says: Don't worry, I'm healty. Look,
- my data is i = 2147483647, s = ABCDEFGHIJKLMNOP, d = -1.0
- /// Serialize an instance of an object to a string.
- public string Serialize(object instance)
- {
- StringBuilder sb = new StringBuilder();
- StringWriter sw = new StringWriter(sb);
- XmlSerializer serializer = new XmlSerializer(instance.GetType());
- serializer.Serialize(sw, instance);
- return sb.ToString();
- }
- LinkedList<Patch> patches1 = diff.patch_make(text2, text1);
- try {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ObjectOutputStream os = new ObjectOutputStream(bos);
- os.writeObject(patches1);
- String serialized_patches1 = bos.toString();
- os.close();
- ByteArrayInputStream bis = new ByteArrayInputStream(serialized_patches1.getBytes());
- ObjectInputStream oInputStream = new ObjectInputStream(bis);
- LinkedList<Patch> restored_patches1 = (LinkedList<Patch>) oInputStream.readObject();
- // patches1 equals restored_patches1
- oInputStream.close();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
Add Comment
Please, Sign In to add comment