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
033public final class FromNetASCIIOutputStream extends FilterOutputStream {
034    private boolean lastWasCR;
035
036    /**
037     * Creates a FromNetASCIIOutputStream instance that wraps an existing OutputStream.
038     *
039     * @param output The OutputStream to wrap.
040     */
041    public FromNetASCIIOutputStream(final OutputStream output) {
042        super(output);
043        lastWasCR = false;
044    }
045
046    /**
047     * Closes the stream, writing all pending data.
048     *
049     * @throws IOException If an error occurs while closing the stream.
050     */
051    @Override
052    public synchronized void close() throws IOException {
053        if (FromNetASCIIInputStream._noConversionRequired) {
054            super.close();
055            return;
056        }
057
058        if (lastWasCR) {
059            out.write('\r');
060        }
061        super.close();
062    }
063
064    /**
065     * Writes a byte array to the stream.
066     *
067     * @param buffer The byte array to write.
068     * @throws IOException If an error occurs while writing to the underlying stream.
069     */
070    @Override
071    public synchronized void write(final byte buffer[]) throws IOException {
072        write(buffer, 0, buffer.length);
073    }
074
075    /**
076     * Writes a number of bytes from a byte array to the stream starting from a given offset.
077     *
078     * @param buffer The byte array to write.
079     * @param offset The offset into the array at which to start copying data.
080     * @param length The number of bytes to write.
081     * @throws IOException If an error occurs while writing to the underlying stream.
082     */
083    @Override
084    public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
085        if (FromNetASCIIInputStream._noConversionRequired) {
086            // FilterOutputStream method is very slow.
087            // super.write(buffer, offset, length);
088            out.write(buffer, offset, length);
089            return;
090        }
091
092        while (length-- > 0) {
093            writeInt(buffer[offset++]);
094        }
095    }
096
097    /**
098     * 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
099     * 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
100     * completeness.
101     *
102     * @param ch The byte to write.
103     * @throws IOException If an error occurs while writing to the underlying stream.
104     */
105    @Override
106    public synchronized void write(final int ch) throws IOException {
107        if (FromNetASCIIInputStream._noConversionRequired) {
108            out.write(ch);
109            return;
110        }
111
112        writeInt(ch);
113    }
114
115    private void writeInt(final int ch) throws IOException {
116        switch (ch) {
117        case '\r':
118            lastWasCR = true;
119            // Don't write anything. We need to see if next one is linefeed
120            break;
121        case '\n':
122            if (lastWasCR) {
123                out.write(FromNetASCIIInputStream._lineSeparatorBytes);
124                lastWasCR = false;
125                break;
126            }
127            out.write('\n');
128            break;
129        default:
130            if (lastWasCR) {
131                out.write('\r');
132                lastWasCR = false;
133            }
134            out.write(ch);
135            break;
136        }
137    }
138}