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
018package org.apache.commons.net.io;
019
020import java.io.FilterOutputStream;
021import java.io.IOException;
022import java.io.OutputStream;
023
024/**
025 * This class wraps an output stream, replacing all occurrences of <CR><LF> (carriage return followed by a linefeed), which is the NETASCII standard
026 * for representing a newline, with the local line separator representation. You would use this class to implement ASCII file transfers requiring conversion
027 * from NETASCII.
028 * <p>
029 * Because of the translation process, a call to <code>flush()</code> will not flush the last byte written if that byte was a carriage return. A call to
030 * {@link #close close() }, however, will flush the carriage return.
031 *
032 *
033 */
034
035public final class FromNetASCIIOutputStream extends FilterOutputStream {
036    private boolean lastWasCR;
037
038    /**
039     * Creates a FromNetASCIIOutputStream instance that wraps an existing OutputStream.
040     *
041     * @param output The OutputStream to wrap.
042     */
043    public FromNetASCIIOutputStream(final OutputStream output) {
044        super(output);
045        lastWasCR = false;
046    }
047
048    /**
049     * Closes the stream, writing all pending data.
050     *
051     * @throws IOException If an error occurs while closing the stream.
052     */
053    @Override
054    public synchronized void close() throws IOException {
055        if (FromNetASCIIInputStream._noConversionRequired) {
056            super.close();
057            return;
058        }
059
060        if (lastWasCR) {
061            out.write('\r');
062        }
063        super.close();
064    }
065
066    /**
067     * Writes a byte array to the stream.
068     *
069     * @param buffer The byte array to write.
070     * @throws IOException If an error occurs while writing to the underlying stream.
071     */
072    @Override
073    public synchronized void write(final byte buffer[]) throws IOException {
074        write(buffer, 0, buffer.length);
075    }
076
077    /**
078     * Writes a number of bytes from a byte array to the stream starting from a given offset.
079     *
080     * @param buffer The byte array to write.
081     * @param offset The offset into the array at which to start copying data.
082     * @param length The number of bytes to write.
083     * @throws IOException If an error occurs while writing to the underlying stream.
084     */
085    @Override
086    public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
087        if (FromNetASCIIInputStream._noConversionRequired) {
088            // FilterOutputStream method is very slow.
089            // super.write(buffer, offset, length);
090            out.write(buffer, offset, length);
091            return;
092        }
093
094        while (length-- > 0) {
095            writeInt(buffer[offset++]);
096        }
097    }
098
099    /**
100     * Writes a byte to the stream. Note that a call to this method might not actually write a byte to the underlying stream until a subsequent character is
101     * written, from which it can be determined if a NETASCII line separator was encountered. This is transparent to the programmer and is only mentioned for
102     * completeness.
103     *
104     * @param ch The byte to write.
105     * @throws IOException If an error occurs while writing to the underlying stream.
106     */
107    @Override
108    public synchronized void write(final int ch) throws IOException {
109        if (FromNetASCIIInputStream._noConversionRequired) {
110            out.write(ch);
111            return;
112        }
113
114        writeInt(ch);
115    }
116
117    private void writeInt(final int ch) throws IOException {
118        switch (ch) {
119        case '\r':
120            lastWasCR = true;
121            // Don't write anything. We need to see if next one is linefeed
122            break;
123        case '\n':
124            if (lastWasCR) {
125                out.write(FromNetASCIIInputStream._lineSeparatorBytes);
126                lastWasCR = false;
127                break;
128            }
129            out.write('\n');
130            break;
131        default:
132            if (lastWasCR) {
133                out.write('\r');
134                lastWasCR = false;
135            }
136            out.write(ch);
137            break;
138        }
139    }
140}