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 * https://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 public final class ToNetASCIIOutputStream extends FilterOutputStream {
30 private boolean lastWasCR;
31
32 /**
33 * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream.
34 *
35 * @param output The OutputStream to wrap.
36 */
37 public ToNetASCIIOutputStream(final OutputStream output) {
38 super(output);
39 lastWasCR = false;
40 }
41
42 /**
43 * Writes a byte array to the stream.
44 *
45 * @param buffer The byte array to write.
46 * @throws IOException If an error occurs while writing to the underlying stream.
47 */
48 @Override
49 public synchronized void write(final byte buffer[]) throws IOException {
50 write(buffer, 0, buffer.length);
51 }
52
53 /**
54 * Writes a number of bytes from a byte array to the stream starting from a given offset.
55 *
56 * @param buffer The byte array to write.
57 * @param offset The offset into the array at which to start copying data.
58 * @param length The number of bytes to write.
59 * @throws IOException If an error occurs while writing to the underlying stream.
60 */
61 @Override
62 public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
63 while (length-- > 0) {
64 write(buffer[offset++]);
65 }
66 }
67
68 /**
69 * 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
70 * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness.
71 *
72 * @param ch The byte to write.
73 * @throws IOException If an error occurs while writing to the underlying stream.
74 */
75 @Override
76 public synchronized void write(final int ch) throws IOException {
77 switch (ch) {
78 case '\r':
79 lastWasCR = true;
80 out.write('\r');
81 return;
82 case '\n':
83 if (!lastWasCR) {
84 out.write('\r');
85 }
86 // falls through$
87 default:
88 lastWasCR = false;
89 out.write(ch);
90 }
91 }
92
93 }