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 * @author Daniel F. Savarese
34 ***/
35
36 public final class ToNetASCIIOutputStream extends FilterOutputStream
37 {
38 private boolean __lastWasCR;
39
40 /***
41 * Creates a ToNetASCIIOutputStream instance that wraps an existing
42 * OutputStream.
43 * <p>
44 * @param output The OutputStream to wrap.
45 ***/
46 public ToNetASCIIOutputStream(OutputStream output)
47 {
48 super(output);
49 __lastWasCR = false;
50 }
51
52
53 /***
54 * Writes a byte to the stream. Note that a call to this method
55 * may result in multiple writes to the underlying input stream in order
56 * to convert naked newlines to NETASCII line separators.
57 * This is transparent to the programmer and is only mentioned for
58 * completeness.
59 * <p>
60 * @param ch The byte to write.
61 * @exception IOException If an error occurs while writing to the underlying
62 * stream.
63 ***/
64 @Override
65 public synchronized void write(int ch)
66 throws IOException
67 {
68 switch (ch)
69 {
70 case '\r':
71 __lastWasCR = true;
72 out.write('\r');
73 return ;
74 case '\n':
75 if (!__lastWasCR)
76 out.write('\r');
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 }