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.

}<br />

}<br />

else if (args[firstFile].equals("-password")) {<br />

password = args[firstFile+1];<br />

firstFile++;<br />

}<br />

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

for (int i = firstFile; i < args.length; i++) {<br />

try {<br />

InputStream in = new FileInputStream(args[i]);<br />

dump(in, System.out, mode, bigEndian, deflated, gzipped, password);<br />

}<br />

if (i < args.length-1) { // more files to dump<br />

System.out.println();<br />

System.out.println("--------------------------------------");<br />

System.out.println();<br />

}<br />

}<br />

catch (<strong>IO</strong>Exception e) {<br />

System.err.println(e);<br />

e.printStackTrace();<br />

}<br />

public static void dump(InputStream in, OutputStream out, int mode,<br />

boolean bigEndian, boolean deflated, boolean gzipped, String password)<br />

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

// The reference variable in may point to several different objects<br />

// within the space of the next few lines.<br />

if (password != null) {<br />

// Create a key.<br />

try {<br />

byte[] desKeyData = password.getBytes();<br />

DESKeySpec desKeySpec = new DESKeySpec(desKeyData);<br />

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />

SecretKey desKey = keyFactory.generateSecret(desKeySpec);<br />

// Use Data Encryption Standard.<br />

Cipher des = Cipher.getInstance("DES/ECB/PKCS5Padding");<br />

des.init(Cipher.DECRYPT_MODE, desKey);<br />

in = new CipherInputStream(in, des);<br />

}<br />

catch (GeneralSecurityException e) {<br />

throw new <strong>IO</strong>Exception(e.getMessage());<br />

}<br />

}<br />

if (deflated) {<br />

in = new InflaterInputStream(in);<br />

}<br />

else if (gzipped) {<br />

in = new GZIPInputStream(in);<br />

}<br />

233

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

Saved successfully!

Ooh no, something went wrong!