Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
|
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2003, 06:52 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 12 ex1 Serializing objects

I just don't get the solution (downloaded from this site). I had trouble with the StreamCorruptedException so I read the author's solution which said that this would be a problem as two object headers would be written to the file. What I don't understand is
a) why we didn't have this same problem in the SerializeObjects class (page 478) and
b) how the ReuseObjectOutputStream in the solution avoids the writing of the header by calling "super(out)".
Please can somebody explain in simple terms.

Gill BC
__________________
Gill BC
 
Old December 14th, 2003, 12:23 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Someone has answered this for me now. The constructor for ObjectOutputStream creates the header.
public ObjectOutputStream(OutputStream out) throws IOException {
    verifySubclass();
    bout = new BlockDataOutputStream(out);
    handles = new HandleTable(10, (float) 3.00);
    subs = new ReplaceTable(10, (float) 3.00);
    enableOverride = false;
    writeStreamHeader();
    bout.setBlockDataMode(true);
    }
protected void writeStreamHeader() throws IOException {
    bout.writeShort(STREAM_MAGIC);
    bout.writeShort(STREAM_VERSION);
}
So when we create an instance of the ReuseobjectOutputStream class, the constructor for ObjectOutputStream is called but when it gets to the line writeStreamHeader(), this method has been overriden in the subclass. Hence the version of this method in the ReuseobjectOutputStream class is used instead and no header is written.
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.IOException;
public class ReuseObjectOutputStream extends ObjectOutputStream {
    public ReuseObjectOutputStream(OutputStream out) throws IOException {
      super(out);
    }
    protected void writeStreamHeader() throws IOException {
      reset();
    }
  }



Gill BC





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM
BJS 2nd edition Chap 3 ex1 Appleby JSP Basics 0 January 9th, 2007 12:47 AM
Errors on Chapter 12 example(12.8) sonnie ASP.NET 2.0 Professional 2 June 7th, 2006 10:55 AM
Chapter 12 Koga VB.NET 2002/2003 Basics 0 November 2nd, 2004 11:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.