23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

when deserialized elementCount will be set to numSerializable<br />

out.writeInt(numSerializable);<br />

for (int i = 0; i < elementCount; i++) {<br />

if (elementData[i] instanceof Serializable) {<br />

out.writeObject(elementData[i]);<br />

}<br />

}<br />

}<br />

<strong>Java</strong> I/O<br />

This still isn't a perfect solution. The vector may contain an object that implements<br />

Serializable but isn't serializable: for example, a hash table that contains a socket. It seems<br />

as if it would be a better solution to catch any such NotSerializableExceptions inside the<br />

readExternal() method, then write null, possibly after backing the stream up to the<br />

beginning of the element that threw the exception (using mark() and reset() and an extra<br />

buffered stream if necessary). However, my tests showed that you cannot catch a<br />

NotSerializableException inside the writeExternal() method. I can see no reason why<br />

this should be the case. It's probably a result of how serialization is implemented by native<br />

code in the virtual machine, so the exception isn't thrown exactly where the <strong>Java</strong> code<br />

indicates it is. (I suspect this should be classified as a bug.)<br />

11.9 Resolving Classes<br />

The readObject() method of java.io.ObjectInputStream only creates new objects from<br />

known classes. It doesn't load classes. If a class for an object can't be found, readObject()<br />

throws a ClassNotFoundException. It specifically does not attempt to read the class data<br />

from the object stream. This is limiting for some things you might want to do, particularly<br />

RMI. Therefore, trusted subclasses of ObjectInputStream may be allowed to load classes<br />

from the stream or some other source like a URL. Specifically, a class is trusted if, and only<br />

if, it was loaded from the local class path; that is, the ClassLoader object returned by<br />

getClassLoader() is null.<br />

Two protected methods are involved. The first is the annotateClass() method of<br />

ObjectOutputStream :<br />

protected void annotateClass(Class c) throws <strong>IO</strong>Exception<br />

In ObjectOutputStream this is a do-nothing method. A subclass of ObjectOutputStream<br />

can provide a different implementation that provides data for the class. For instance, this<br />

might be the byte code of the class itself or a URL where the class can be found.<br />

Standard object input streams cannot read and resolve the class data written by<br />

annotateClass(). For each subclass of ObjectOutputStream that overrides<br />

annotateClass(), there will normally be a corresponding subclass of ObjectInputStream<br />

that implements the resolveClass() method:<br />

protected Class resolveClass(ObjectStreamClass v)<br />

throws <strong>IO</strong>Exception, ClassNotFoundException<br />

In java.io.ObjectInputStream, this is a do-nothing method. A subclass of<br />

ObjectInputStream can provide an implementation that loads a class based on the data read<br />

from the stream. For instance, if annotateClass() wrote byte code to the stream, then<br />

260

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!