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