View Javadoc

1   // AsciiOutputStream.java
2   // $Id: AsciiOutputStream.java,v 1.2 2006/02/20 04:52:11 sjardine Exp $
3   //
4   // Copyright 2002 Innovation Software Group, LLC - http://www.innovationsw.com
5   //
6   // This library is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU Library General Public
8   // License as published by the Free Software Foundation; either
9   // version 2 of the License, or (at your option) any later version.
10  //
11  // This library is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  // Library General Public License for more details.
15  //
16  // You should have received a copy of the GNU Library General Public
17  // License along with this library; if not, write to the Free
18  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }