View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.io;
19  
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  /**
25   * This class wraps an output stream, replacing all occurrences of <CR><LF> (carriage return followed by a linefeed), which is the NETASCII standard
26   * for representing a newline, with the local line separator representation. You would use this class to implement ASCII file transfers requiring conversion
27   * from NETASCII.
28   * <p>
29   * Because of the translation process, a call to <code>flush()</code> will not flush the last byte written if that byte was a carriage return. A call to
30   * {@link #close close() }, however, will flush the carriage return.
31   */
32  
33  public final class FromNetASCIIOutputStream extends FilterOutputStream {
34      private boolean lastWasCR;
35  
36      /**
37       * Creates a FromNetASCIIOutputStream instance that wraps an existing OutputStream.
38       *
39       * @param output The OutputStream to wrap.
40       */
41      public FromNetASCIIOutputStream(final OutputStream output) {
42          super(output);
43          lastWasCR = false;
44      }
45  
46      /**
47       * Closes the stream, writing all pending data.
48       *
49       * @throws IOException If an error occurs while closing the stream.
50       */
51      @Override
52      public synchronized void close() throws IOException {
53          if (FromNetASCIIInputStream._noConversionRequired) {
54              super.close();
55              return;
56          }
57  
58          if (lastWasCR) {
59              out.write('\r');
60          }
61          super.close();
62      }
63  
64      /**
65       * Writes a byte array to the stream.
66       *
67       * @param buffer The byte array to write.
68       * @throws IOException If an error occurs while writing to the underlying stream.
69       */
70      @Override
71      public synchronized void write(final byte buffer[]) throws IOException {
72          write(buffer, 0, buffer.length);
73      }
74  
75      /**
76       * Writes a number of bytes from a byte array to the stream starting from a given offset.
77       *
78       * @param buffer The byte array to write.
79       * @param offset The offset into the array at which to start copying data.
80       * @param length The number of bytes to write.
81       * @throws IOException If an error occurs while writing to the underlying stream.
82       */
83      @Override
84      public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
85          if (FromNetASCIIInputStream._noConversionRequired) {
86              // FilterOutputStream method is very slow.
87              // super.write(buffer, offset, length);
88              out.write(buffer, offset, length);
89              return;
90          }
91  
92          while (length-- > 0) {
93              writeInt(buffer[offset++]);
94          }
95      }
96  
97      /**
98       * Writes a byte to the stream. Note that a call to this method might not actually write a byte to the underlying stream until a subsequent character is
99       * written, from which it can be determined if a NETASCII line separator was encountered. This is transparent to the programmer and is only mentioned for
100      * completeness.
101      *
102      * @param ch The byte to write.
103      * @throws IOException If an error occurs while writing to the underlying stream.
104      */
105     @Override
106     public synchronized void write(final int ch) throws IOException {
107         if (FromNetASCIIInputStream._noConversionRequired) {
108             out.write(ch);
109             return;
110         }
111 
112         writeInt(ch);
113     }
114 
115     private void writeInt(final int ch) throws IOException {
116         switch (ch) {
117         case '\r':
118             lastWasCR = true;
119             // Don't write anything. We need to see if next one is linefeed
120             break;
121         case '\n':
122             if (lastWasCR) {
123                 out.write(FromNetASCIIInputStream._lineSeparatorBytes);
124                 lastWasCR = false;
125                 break;
126             }
127             out.write('\n');
128             break;
129         default:
130             if (lastWasCR) {
131                 out.write('\r');
132                 lastWasCR = false;
133             }
134             out.write(ch);
135             break;
136         }
137     }
138 }