Results 1 to 6 of 6
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default source for OutputStream.writeObject()

    can someone post the source code for that method? I try to ctrl + click to view it in Eclipse but I get a No Source Code error saying the jar file has no source attachment.

  2. #2
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    It took me a few minutes to figure this out, and I could be wrong, but your looking for the class ObjectOutputStream and not just OutputStream. (which you may have known)

    You may have ran into problems because OutputStream doesn't have a writeObject method.

    Though changing it to ObjectOutputStream didn't give me results when I ctrl-clicked it, I think it's a step in the right direction.

    The JavaDocs may be able to help you from here. I'd post a link if I met the 20 post requirement

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    ya I meant ObjectOutputStream.writeObject() lol.
    ObjectOutput (Java 2 Platform SE v1.4.2)) is the link to that api but it doesn't show the source for writeObject().

    My IDE just gives
    Java Code:
    public final void writeObject(Object obj)
                           throws IOException
    Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written. Default serialization for a class can be overridden using the writeObject and the readObject methods. Objects referenced by this object are written transitively so that a complete equivalent graph of objects can be reconstructed by an ObjectInputStream. 
    Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, which is left in an indeterminate state, and it is up to the caller to ignore or recover the stream state. 
    
    
    Specified by:
    writeObject in interface ObjectOutput
    Parameters:
    obj - the object to be written 
    Throws: 
    InvalidClassException - Something is wrong with a class used by serialization. 
    NotSerializableException - Some object to be serialized does not implement the java.io.Serializable interface. 
    IOException - Any exception thrown by the underlying OutputStream.
    on mouseover. I'm not sure when I managed to break ctrl + click but I did and now I need it :(

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    The sources are in a standard install:
    Java Code:
    C:\Program Files\Java\jdk1.6.0\src.zip
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    This is the code for writeObject()
    Java Code:
     /**
         * Write the specified object to the ObjectOutputStream.  The class of the
         * object, the signature of the class, and the values of the non-transient
         * and non-static fields of the class and all of its supertypes are
         * written.  Default serialization for a class can be overridden using the
         * writeObject and the readObject methods.  Objects referenced by this
         * object are written transitively so that a complete equivalent graph of
         * objects can be reconstructed by an ObjectInputStream.
         *
         * <p>Exceptions are thrown for problems with the OutputStream and for
         * classes that should not be serialized.  All exceptions are fatal to the
         * OutputStream, which is left in an indeterminate state, and it is up to
         * the caller to ignore or recover the stream state.
         *
         * @throws	InvalidClassException Something is wrong with a class used by
         *		serialization.
         * @throws	NotSerializableException Some object to be serialized does not
         *		implement the java.io.Serializable interface.
         * @throws	IOException Any exception thrown by the underlying
         * 		OutputStream.
         */
        public final void writeObject(Object obj) throws IOException {
    	if (enableOverride) {
    	    writeObjectOverride(obj);
    	    return;
    	}
    	try {
    	    writeObject0(obj, false);
    	} catch (IOException ex) {
    	    if (depth == 0) {
    		writeFatalException(ex);
    	    }
    	    throw ex;
    	}
        }
    and this is readObject() from ObjectInputStream
    Java Code:
        public final Object readObject() 
    	throws IOException, ClassNotFoundException
        {
    	if (enableOverride) {
    	    return readObjectOverride();
    	}
    
    	// if nested read, passHandle contains handle of enclosing object
    	int outerHandle = passHandle;
    	try {
    	    Object obj = readObject0(false);
    	    handles.markDependency(outerHandle, passHandle);
    	    ClassNotFoundException ex = handles.lookupException(passHandle);
    	    if (ex != null) {
    		throw ex;
    	    }
    	    if (depth == 0) {
    		vlist.doCallbacks();
    	    }
    	    return obj;
    	} finally {
    	    passHandle = outerHandle;
    	    if (closed && depth == 0) {
    		clear();
    	    }
    	}
        }
    The calls to writeObject0() are a private class within ObjectOutputStream. What it does is when the stream tries to write the object writeObject0 is called, which will increment depth++, break the object into class, handle, string, etc; then it does an iterative check(?) to see if the object exists already and if it can be replaced
    Java Code:
    // check for replacement object
    	    Object orig = obj;
    	    Class cl = obj.getClass();
    	    ObjectStreamClass desc;
    	    for (;;) {
    		// REMIND: skip this check for strings/arrays?
    		Class repCl;
    		desc = ObjectStreamClass.lookup(cl, true);
    		if (!desc.hasWriteReplaceMethod() ||
    		    (obj = desc.invokeWriteReplace(obj)) == null ||
    		    (repCl = obj.getClass()) == cl)
    		{
    		    break;
    		}
    		cl = repCl;
    	    }
    	    if (enableReplace) {
    		Object rep = replaceObject(obj);
    		if (rep != obj && rep != null) {
    		    cl = rep.getClass();
    		    desc = ObjectStreamClass.lookup(cl, true);
    		}
    		obj = rep;
    	    }
    
    	    // if object replaced, run through original checks a second time
    	    if (obj != orig) {
    		subs.assign(orig, obj);
    		if (obj == null) {
    		    writeNull();
    		    return;
    		} else if (!unshared && (h = handles.lookup(obj)) != -1) {
    		    writeHandle(h);
    		    return;
    		} else if (obj instanceof Class) {
    		    writeClass((Class) obj, unshared);
    		    return;
    		} else if (obj instanceof ObjectStreamClass) {
    		    writeClassDesc((ObjectStreamClass) obj, unshared);
    		    return;
    		}
    	    }
    From what I can tell the readObject() method has no way to auto increment through the file(say with something like hasNext() != null). Does this mean I'm going to have to override the readObject() method to include some sort of counter?

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Well short version is that is what I would do anyway. I have done that many times, and then see the code somewhere in the libs - only diff is now I understand it and know what it does...

    Seems a trivial way to discover something, and is said to be re-inventing the wheel. Actually, what that is saying is front-line contemplation is not time-effective when study of established can provide momentum.

    What I would to is dig into read object and figure how to isolate the various Objects,.. iow if they do not do it for you - then it's full-tilt-boogie.

    To do otherwise may end up with a lot of books read and nothing done. There are several authors I have found on the web that are masters at something like this, "Java Tips" at the left nav bar is one of them. Try that first, then burn some midnight oil on your question, read the code ( source code ) slowly, once.

    I did one of these recently, ended up with several thousand lines of sources ~ then found it to be written as a class with exactly the same name I had chosen, started work on something else......one day I will go back and see if the lib version does what my version does...
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Outputstream not streaming.
    By dantestein in forum Networking
    Replies: 2
    Last Post: 03-14-2009, 08:36 AM
  2. Replies: 1
    Last Post: 11-28-2008, 07:27 PM
  3. interrupting urlconnection's outputstream
    By zynasis in forum Networking
    Replies: 5
    Last Post: 10-02-2008, 01:49 AM
  4. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 11:13 PM
  5. HashMap to share OutputStream
    By gabriel in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •