1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package gnu.inet.ftp;
21
22 import java.io.*;
23
24 /***
25 * The <code>AsciiOutputStream</code> class acts as a filter to convert outgoing
26 * ASCII FTP streams from the system's local ASCII format by filtering and
27 * converting line termination strings. Note that the class as currently
28 * written will only handle <code>\r\n</code> or single character line
29 * termination strings; a more technically correct implementation would be
30 * preferable.
31 */
32 public class AsciiOutputStream extends FilterOutputStream {
33
34 protected boolean active;
35 protected int eol;
36
37 /***
38 * Creates an AsciiOutputStream by passing <code>out</code> to its
39 * superclass' constructor and determining the system-specific line
40 * termination string.
41 *
42 * @param out the underlying output stream.
43 *
44 * @throws Exception if system line separator is longer than 1 char and is
45 * not <code>\r\n</code>
46 */
47 public AsciiOutputStream(OutputStream out) throws Exception {
48 super(out);
49
50 String lineSeparator = System.getProperty("line.separator");
51 if (lineSeparator.equals("\r\n")) {
52 active = false;
53 } else if (lineSeparator.length() > 1) {
54 throw new Exception("System line separator longer than 1 char");
55 } else {
56 active = true;
57 eol = lineSeparator.charAt(0);
58 }
59 }
60
61 /***
62 * Writes the specified byte to this output stream.
63 *
64 * @param b the byte.
65 *
66 * @throws IOException if an I/O error occurs.
67 */
68 public void write(int b) throws IOException {
69 if (active && b == eol) {
70 out.write('\r');
71 out.write('\n');
72 } else {
73 out.write(b);
74 }
75 }
76
77 /***
78 * Writes b.length bytes to this output stream.
79 *
80 * @param b the data to be written.
81 *
82 * @throws IOException if an I/O error occurs.
83 */
84 public void write(byte[] b) throws IOException {
85 write(b, 0, b.length);
86 }
87
88 /***
89 * Writes <code>len</code> bytes from the specified byte array starting at
90 * offset <code>off</code> to this output stream.
91 *
92 * @param b the data.
93 * @param off the start offset in the data.
94 * @param len the number of bytes to write.
95 *
96 * @throws IOException if an I/O error occurs.
97 */
98 public void write(byte[] b, int off, int len) throws IOException {
99 for (int i=off; i<off+len; i++)
100 write(b[i]);
101 }
102
103 }