01: /**
02: * Title: <p>
03: * Description: <p>
04: * Copyright: Copyright (c) <p>
05: * Company: <p>
06: * @author
07: * @version 1.0
08: */package org.skunk.util.io;
09:
10: import java.io.*;
11:
12: /**
13: * this is a blatant ripoff of E.R.Harold's PrintableOutputStream,
14: * from his OReilly Java IO book.
15: */
16: public class AsciiOutputStream extends FilterOutputStream {
17:
18: public AsciiOutputStream(OutputStream out) {
19: super (out);
20: }
21:
22: public void write(int b) throws IOException {
23: //permit CR, LF, and tab; otherwise only printing ASCII characters
24: if ((b == 10 || b == 13 || b == 9) || (b >= 32 && b <= 126))
25: out.write(b);
26: else
27: out.write('?');
28: }
29:
30: public void write(byte[] data, int offset, int length)
31: throws IOException {
32: for (int i = offset; i < offset + length; i++)
33: this .write(data[i]);
34: }
35:
36: public static void copy(InputStream in, OutputStream out)
37: throws IOException {
38: synchronized (in) {
39: synchronized (out) {
40: byte[] buffer = new byte[1024];
41: while (true) {
42: int bytesRead = in.read(buffer);
43: if (bytesRead == -1)
44: break;
45: out.write(buffer, 0, bytesRead);
46: }
47: }
48: }
49: }
50:
51: public static void main(String[] args) throws IOException {
52: if (args.length != 1) {
53: System.out.println("please supply a path to a binary file");
54: System.exit(1);
55: }
56: String filename = args[0];
57: InputStream in = new BufferedInputStream(new FileInputStream(
58: filename));
59: OutputStream out = new AsciiOutputStream(System.out);
60: copy(in, out);
61: }
62: }
|