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