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.

Example 11.6. Person<br />

import java.util.*;<br />

import java.io.*;<br />

public class Person implements Serializable, ObjectInputValidation {<br />

}<br />

static Hashtable thePeople = new Hashtable();<br />

String name;<br />

String ss;<br />

public Person(String name, String ss) {<br />

}<br />

this.name = name;<br />

this.ss = ss;<br />

thePeople.put(ss, name);<br />

private void readObject(ObjectInputStream in)<br />

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

}<br />

in.registerValidation(this, 5);<br />

in.defaultReadObject();<br />

public void validateObject() throws InvalidObjectException {<br />

}<br />

if (thePeople.containsKey(this.ss)) {<br />

throw new InvalidObjectException(this.name + " already exists");<br />

}<br />

else {<br />

thePeople.put(this.ss, this.name);<br />

}<br />

public String toString() {<br />

return this.name + "\t" + this.ss;<br />

}<br />

11.12 Sealed Objects<br />

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

The JCE standard extension to <strong>Java</strong> 2, discussed in the last chapter, provides a SealedObject<br />

class that lets you encrypt objects written onto an object output stream using any available<br />

cipher. Most of the time, I suspect, you'll either encrypt the entire object output stream by<br />

chaining it to a cipher output stream, or you won't encrypt anything at all. However, if there's<br />

some reason to encrypt only some of the objects you're writing to the stream, you can make<br />

them sealed objects.<br />

The javax.crypto.SealedObject class wraps a serializable object in an encrypted digital<br />

lockbox. The sealed object is serializable so it can be written onto object output streams and<br />

read from object input streams as normal. However, the object inside the sealed object can<br />

only be deserialized by someone who knows the key.<br />

263

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

Saved successfully!

Ooh no, something went wrong!