    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
    026 * of &lt;CR&gt;&lt;LF&gt; (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 *
    036 *
    037 */
    038
    039public final class FromNetASCIIOutputStream extends FilterOutputStream
    040{
    041    private boolean lastWasCR;
    042
    043    /**
    044     * Creates a FromNetASCIIOutputStream instance that wraps an existing
    045     * OutputStream.
    046     *
    047     * @param output  The OutputStream to wrap.
    048     */
    049    public FromNetASCIIOutputStream(final OutputStream output)
    050    {
    051        super(output);
    052        lastWasCR = false;
    053    }
    054
    055
    056    private void writeInt(final 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     *
    094     * @param ch The byte to write.
    095     * @throws IOException If an error occurs while writing to the underlying
    096     *            stream.
    097     */
    098    @Override
    099    public synchronized void write(final int ch)
    100    throws IOException
    101    {
    102        if (FromNetASCIIInputStream._noConversionRequired)
    103        {
    104            out.write(ch);
    105            return ;
    106        }
    107
    108        writeInt(ch);
    109    }
    110
    111
    112    /**
    113     * Writes a byte array to the stream.
    114     *
    115     * @param buffer  The byte array to write.
    116     * @throws IOException If an error occurs while writing to the underlying
    117     *            stream.
    118     */
    119    @Override
    120    public synchronized void write(final 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     *
    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     * @throws IOException If an error occurs while writing to the underlying
    135     *            stream.
    136     */
    137    @Override
    138    public synchronized void write(final 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            writeInt(buffer[offset++]);
    151        }
    152    }
    153
    154
    155    /**
    156     * Closes the stream, writing all pending data.
    157     *
    158     * @throws 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}