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 singly occurring <LF> (linefeed) characters with <CR><LF> (carriage return followed by
26   * linefeed), which is the NETASCII standard for representing a newline. You would use this class to implement ASCII file transfers requiring conversion to
27   * NETASCII.
28   */
29  
30  public final class ToNetASCIIOutputStream extends FilterOutputStream {
31      private boolean lastWasCR;
32  
33      /**
34       * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream.
35       *
36       * @param output The OutputStream to wrap.
37       */
38      public ToNetASCIIOutputStream(final OutputStream output) {
39          super(output);
40          lastWasCR = false;
41      }
42  
43      /**
44       * Writes a byte array to the stream.
45       *
46       * @param buffer The byte array to write.
47       * @throws IOException If an error occurs while writing to the underlying stream.
48       */
49      @Override
50      public synchronized void write(final byte buffer[]) throws IOException {
51          write(buffer, 0, buffer.length);
52      }
53  
54      /**
55       * Writes a number of bytes from a byte array to the stream starting from a given offset.
56       *
57       * @param buffer The byte array to write.
58       * @param offset The offset into the array at which to start copying data.
59       * @param length The number of bytes to write.
60       * @throws IOException If an error occurs while writing to the underlying stream.
61       */
62      @Override
63      public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
64          while (length-- > 0) {
65              write(buffer[offset++]);
66          }
67      }
68  
69      /**
70       * Writes a byte to the stream. Note that a call to this method may result in multiple writes to the underlying input stream in order to convert naked
71       * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness.
72       *
73       * @param ch The byte to write.
74       * @throws IOException If an error occurs while writing to the underlying stream.
75       */
76      @Override
77      public synchronized void write(final int ch) throws IOException {
78          switch (ch) {
79          case '\r':
80              lastWasCR = true;
81              out.write('\r');
82              return;
83          case '\n':
84              if (!lastWasCR) {
85                  out.write('\r');
86              }
87              //$FALL-THROUGH$
88          default:
89              lastWasCR = false;
90              out.write(ch);
91          }
92      }
93  
94  }