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 *      https://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 singly occurring <LF> (linefeed) characters with <CR><LF> (carriage return followed by
026 * linefeed), which is the NETASCII standard for representing a newline. You would use this class to implement ASCII file transfers requiring conversion to
027 * NETASCII.
028 */
029public final class ToNetASCIIOutputStream extends FilterOutputStream {
030    private boolean lastWasCR;
031
032    /**
033     * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream.
034     *
035     * @param output The OutputStream to wrap.
036     */
037    public ToNetASCIIOutputStream(final OutputStream output) {
038        super(output);
039        lastWasCR = false;
040    }
041
042    /**
043     * Writes a byte array to the stream.
044     *
045     * @param buffer The byte array to write.
046     * @throws IOException If an error occurs while writing to the underlying stream.
047     */
048    @Override
049    public synchronized void write(final byte buffer[]) throws IOException {
050        write(buffer, 0, buffer.length);
051    }
052
053    /**
054     * Writes a number of bytes from a byte array to the stream starting from a given offset.
055     *
056     * @param buffer The byte array to write.
057     * @param offset The offset into the array at which to start copying data.
058     * @param length The number of bytes to write.
059     * @throws IOException If an error occurs while writing to the underlying stream.
060     */
061    @Override
062    public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
063        while (length-- > 0) {
064            write(buffer[offset++]);
065        }
066    }
067
068    /**
069     * Writes a byte to the stream. Note that a call to this method may result in multiple writes to the underlying input stream in order to convert naked
070     * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness.
071     *
072     * @param ch The byte to write.
073     * @throws IOException If an error occurs while writing to the underlying stream.
074     */
075    @Override
076    public synchronized void write(final int ch) throws IOException {
077        switch (ch) {
078        case '\r':
079            lastWasCR = true;
080            out.write('\r');
081            return;
082        case '\n':
083            if (!lastWasCR) {
084                out.write('\r');
085            }
086            // falls through$
087        default:
088            lastWasCR = false;
089            out.write(ch);
090        }
091    }
092
093}