001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.net.io;
019
020 import java.io.FilterOutputStream;
021 import java.io.IOException;
022 import java.io.OutputStream;
023
024 /***
025 * This class wraps an output stream, replacing all occurrences
026 * of <CR><LF> (carriage return followed by a linefeed),
027 * which is the NETASCII standard for representing a newline, with the
028 * local line separator representation. You would use this class to
029 * implement ASCII file transfers requiring conversion from NETASCII.
030 * <p>
031 * Because of the translation process, a call to <code>flush()</code> will
032 * not flush the last byte written if that byte was a carriage
033 * return. A call to {@link #close close() }, however, will
034 * flush the carriage return.
035 * <p>
036 * <p>
037 ***/
038
039 public final class FromNetASCIIOutputStream extends FilterOutputStream
040 {
041 private boolean __lastWasCR;
042
043 /***
044 * Creates a FromNetASCIIOutputStream instance that wraps an existing
045 * OutputStream.
046 * <p>
047 * @param output The OutputStream to wrap.
048 ***/
049 public FromNetASCIIOutputStream(OutputStream output)
050 {
051 super(output);
052 __lastWasCR = false;
053 }
054
055
056 private void __write(int ch) throws IOException
057 {
058 switch (ch)
059 {
060 case '\r':
061 __lastWasCR = true;
062 // Don't write anything. We need to see if next one is linefeed
063 break;
064 case '\n':
065 if (__lastWasCR)
066 {
067 out.write(FromNetASCIIInputStream._lineSeparatorBytes);
068 __lastWasCR = false;
069 break;
070 }
071 __lastWasCR = false;
072 out.write('\n');
073 break;
074 default:
075 if (__lastWasCR)
076 {
077 out.write('\r');
078 __lastWasCR = false;
079 }
080 out.write(ch);
081 break;
082 }
083 }
084
085
086 /***
087 * Writes a byte to the stream. Note that a call to this method
088 * might not actually write a byte to the underlying stream until a
089 * subsequent character is written, from which it can be determined if
090 * a NETASCII line separator was encountered.
091 * This is transparent to the programmer and is only mentioned for
092 * completeness.
093 * <p>
094 * @param ch The byte to write.
095 * @exception IOException If an error occurs while writing to the underlying
096 * stream.
097 ***/
098 @Override
099 public synchronized void write(int ch)
100 throws IOException
101 {
102 if (FromNetASCIIInputStream._noConversionRequired)
103 {
104 out.write(ch);
105 return ;
106 }
107
108 __write(ch);
109 }
110
111
112 /***
113 * Writes a byte array to the stream.
114 * <p>
115 * @param buffer The byte array to write.
116 * @exception IOException If an error occurs while writing to the underlying
117 * stream.
118 ***/
119 @Override
120 public synchronized void write(byte buffer[])
121 throws IOException
122 {
123 write(buffer, 0, buffer.length);
124 }
125
126
127 /***
128 * Writes a number of bytes from a byte array to the stream starting from
129 * a given offset.
130 * <p>
131 * @param buffer The byte array to write.
132 * @param offset The offset into the array at which to start copying data.
133 * @param length The number of bytes to write.
134 * @exception IOException If an error occurs while writing to the underlying
135 * stream.
136 ***/
137 @Override
138 public synchronized void write(byte buffer[], int offset, int length)
139 throws IOException
140 {
141 if (FromNetASCIIInputStream._noConversionRequired)
142 {
143 // FilterOutputStream method is very slow.
144 //super.write(buffer, offset, length);
145 out.write(buffer, offset, length);
146 return ;
147 }
148
149 while (length-- > 0) {
150 __write(buffer[offset++]);
151 }
152 }
153
154
155 /***
156 * Closes the stream, writing all pending data.
157 * <p>
158 * @exception IOException If an error occurs while closing the stream.
159 ***/
160 @Override
161 public synchronized void close()
162 throws IOException
163 {
164 if (FromNetASCIIInputStream._noConversionRequired)
165 {
166 super.close();
167 return ;
168 }
169
170 if (__lastWasCR) {
171 out.write('\r');
172 }
173 super.close();
174 }
175 }