001package serialization.custom; 002import java.util.LinkedList; 003import java.io.IOException; 004import java.io.FileNotFoundException; 005import java.io.FileOutputStream; 006import java.io.ObjectOutputStream; 007import java.io.FileInputStream; 008import java.io.ObjectInputStream; 009 010class IntList2 implements IntList 011{ 012 private LinkedList<Integer> v; 013 //private void setV(LinkedList<Integer> v) { v = v; } 014 015 public IntList2() { v = new LinkedList<Integer>(); } 016 public void addBack(int i) { v.addLast(i); } 017 public void addFront(int i) { v.addFirst(i); } 018 public int removeFront() { return v.removeFirst(); } 019 public int removeBack() { return v.removeLast(); } 020 public boolean isEmpty() { return v.size() == 0; } 021 022 private void writeObject(ObjectOutputStream out) throws IOException 023 { System.out.println("call specialized writer"); 024 out.writeInt(v.size()); 025 for (Integer i : v ) 026 out.writeInt(i); 027 } 028 029 private void readObject(ObjectInputStream in) throws IOException 030 { System.out.println("call specialized reader"); 031 v = new LinkedList<Integer>(); 032 int size = in.readInt(); 033 for (int i = 0; i<size; i++) 034 addBack(in.readInt()); 035 } 036} 037 038public class Main2 { 039 public static void main(String[] args) 040 throws IOException, FileNotFoundException, ClassNotFoundException 041 { 042 IntList L = new IntList2(); 043 for (int i=0; i<1000; i++) 044 L.addFront(i); 045 046 ObjectOutputStream os = new ObjectOutputStream (new FileOutputStream("out2.dat")); 047 os.writeObject(L); 048 os.flush(); 049 os.close(); 050 051 ObjectInputStream in = new ObjectInputStream (new FileInputStream("out2.dat")); 052 IntList V = (IntList) in.readObject(); 053 in.close(); 054 } 055}