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.

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

getIV() returns null if the algorithm doesn't use initialization vectors or if the initialization<br />

vector isn't yet set.<br />

10.6 Cipher Streams<br />

The Cipher class is the engine that powers encryption. Chapter 10 and Example 10.7 showed<br />

how this class could be used to encrypt and decrypt data read from a stream. The<br />

javax.crypto package also provides CipherInputStream and CipherOutputStream filter<br />

streams that use a Cipher object to encrypt or decrypt data passed through the stream. Like<br />

DigestInputStream and DigestOutputStream, they aren't a great deal of use in themselves.<br />

However, you can chain them in the middle of several other streams. For example, if you<br />

chain a GZIPOutputStream to a CipherOutputStream that is chained to a<br />

FileOutputStream, you can compress, encrypt and write to a file, all with a single call to<br />

write(). This is shown in Figure 10.3. Similarly, you might read from a URL with the input<br />

stream returned by openStream(), decrypt the data read with a CipherInputStream, then<br />

check the decrypted data with a MessageDigestInputStream, then finally pass it all into an<br />

InputStreamReader for conversion from ISO Latin-1 to Unicode. On the other side of the<br />

connection, a web server could read a file from its hard drive, write the file onto a socket with<br />

an output stream, calculate a digest with a DigestOutputStream, and encrypt the file with a<br />

CipherOutputStream.<br />

Figure 10.3. The CipherOutputStream in the middle of a chain of filters<br />

10.6.1 CipherInputStream<br />

CipherInputStream is a subclass of FilterInputStream.<br />

public class CipherInputStream extends FilterInputStream<br />

CipherInputStream has all the usual methods of any input stream, like read(), skip(), and<br />

close(). It overrides seven of these methods to do its filtering:<br />

public int read() throws <strong>IO</strong>Exception<br />

public int read(byte[] data) throws <strong>IO</strong>Exception<br />

public int read(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public long skip(long n) throws <strong>IO</strong>Exception<br />

public int available() throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

public boolean markSupported()<br />

These methods are all invoked much as they would be for any other input stream. However,<br />

as the data is read, the stream's Cipher object either decrypts or encrypts the data. (Assuming<br />

your program wants to work with unencrypted data, as is most commonly the case, the cipher<br />

input stream will decrypt the data.)<br />

226

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

Saved successfully!

Ooh no, something went wrong!