- public class Test1 {
- final static ObjectStreamClass desc = ObjectStreamClass.lookupAny(MyBean.class);
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- File f = new File("f://f"+System.currentTimeMillis()+".ser");
- FileOutputStream fos = new FileOutputStream(f);
- java.io.ObjectOutputStream oos = new MyObjectOutputStream(fos);
- oos.writeObject(new MyBean((byte)1));
- oos.writeObject(new MyBean((byte)2));
- oos.flush();
- oos.close();
- FileInputStream fis = new FileInputStream(f);
- java.io.ObjectInputStream ois = new MyObjectInputStream(fis);
- System.out.println(ois.readObject());
- System.out.println(ois.readObject());
- ois.close();
- }
- static
- class MyObjectOutputStream extends ObjectOutputStream{
- public MyObjectOutputStream(OutputStream out) throws IOException {
- super(out);
- }
- @Override
- protected void writeStreamHeader() throws IOException{}
- @Override
- protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException{}
- }
- static
- class MyObjectInputStream extends ObjectInputStream{
- public MyObjectInputStream(InputStream in) throws IOException {
- super(in);
- }
- @Override
- protected void readStreamHeader() throws IOException, StreamCorruptedException{}
- @Override
- protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException{
- return desc;
- }
- }
- }
- class MyBean implements Serializable{
- private static final long serialVersionUID = 0L;
- public MyBean(byte id){
- this.i = id;
- }
- byte i=0;
- @Override
- public String toString(){
- return this.getClass().getName()+":"+i;
- }
- }