001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net.io;
017    
018    import java.io.FilterOutputStream;
019    import java.io.IOException;
020    import java.io.OutputStream;
021    
022    /***
023     * This class wraps an output stream, replacing all singly occurring
024     * <LF> (linefeed) characters with <CR><LF> (carriage return
025     * followed by linefeed), which is the NETASCII standard for representing
026     * a newline.
027     * You would use this class to implement ASCII file transfers requiring
028     * conversion to NETASCII.
029     * <p>
030     * <p>
031     * @author Daniel F. Savarese
032     ***/
033    
034    public final class ToNetASCIIOutputStream extends FilterOutputStream
035    {
036        private boolean __lastWasCR;
037    
038        /***
039         * Creates a ToNetASCIIOutputStream instance that wraps an existing
040         * OutputStream.
041         * <p>
042         * @param output  The OutputStream to wrap.
043         ***/
044        public ToNetASCIIOutputStream(OutputStream output)
045        {
046            super(output);
047            __lastWasCR = false;
048        }
049    
050    
051        /***
052         * Writes a byte to the stream.    Note that a call to this method
053         * may result in multiple writes to the underlying input stream in order
054         * to convert naked newlines to NETASCII line separators.
055         * This is transparent to the programmer and is only mentioned for
056         * completeness.
057         * <p>
058         * @param ch The byte to write.
059         * @exception IOException If an error occurs while writing to the underlying
060         *            stream.
061         ***/
062        public synchronized void write(int ch)
063        throws IOException
064        {
065            switch (ch)
066            {
067            case '\r':
068                __lastWasCR = true;
069                out.write('\r');
070                return ;
071            case '\n':
072                if (!__lastWasCR)
073                    out.write('\r');
074                // Fall through
075            default:
076                __lastWasCR = false;
077                out.write(ch);
078                return ;
079            }
080        }
081    
082    
083        /***
084         * Writes a byte array to the stream.
085         * <p>
086         * @param buffer  The byte array to write.
087         * @exception IOException If an error occurs while writing to the underlying
088         *            stream.
089         ***/
090        public synchronized void write(byte buffer[])
091        throws IOException
092        {
093            write(buffer, 0, buffer.length);
094        }
095    
096    
097        /***
098         * Writes a number of bytes from a byte array to the stream starting from
099         * a given offset.
100         * <p>
101         * @param buffer  The byte array to write.
102         * @param offset  The offset into the array at which to start copying data.
103         * @param length  The number of bytes to write.
104         * @exception IOException If an error occurs while writing to the underlying
105         *            stream.
106         ***/
107        public synchronized void write(byte buffer[], int offset, int length)
108        throws IOException
109        {
110            while (length-- > 0)
111                write(buffer[offset++]);
112        }
113    
114    }