ToNetASCIIOutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.io;

  18. import java.io.FilterOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;

  21. /**
  22.  * This class wraps an output stream, replacing all singly occurring <LF> (linefeed) characters with <CR><LF> (carriage return followed by
  23.  * linefeed), which is the NETASCII standard for representing a newline. You would use this class to implement ASCII file transfers requiring conversion to
  24.  * NETASCII.
  25.  */

  26. public final class ToNetASCIIOutputStream extends FilterOutputStream {
  27.     private boolean lastWasCR;

  28.     /**
  29.      * Creates a ToNetASCIIOutputStream instance that wraps an existing OutputStream.
  30.      *
  31.      * @param output The OutputStream to wrap.
  32.      */
  33.     public ToNetASCIIOutputStream(final OutputStream output) {
  34.         super(output);
  35.         lastWasCR = false;
  36.     }

  37.     /**
  38.      * Writes a byte array to the stream.
  39.      *
  40.      * @param buffer The byte array to write.
  41.      * @throws IOException If an error occurs while writing to the underlying stream.
  42.      */
  43.     @Override
  44.     public synchronized void write(final byte buffer[]) throws IOException {
  45.         write(buffer, 0, buffer.length);
  46.     }

  47.     /**
  48.      * Writes a number of bytes from a byte array to the stream starting from a given offset.
  49.      *
  50.      * @param buffer The byte array to write.
  51.      * @param offset The offset into the array at which to start copying data.
  52.      * @param length The number of bytes to write.
  53.      * @throws IOException If an error occurs while writing to the underlying stream.
  54.      */
  55.     @Override
  56.     public synchronized void write(final byte buffer[], int offset, int length) throws IOException {
  57.         while (length-- > 0) {
  58.             write(buffer[offset++]);
  59.         }
  60.     }

  61.     /**
  62.      * 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
  63.      * newlines to NETASCII line separators. This is transparent to the programmer and is only mentioned for completeness.
  64.      *
  65.      * @param ch The byte to write.
  66.      * @throws IOException If an error occurs while writing to the underlying stream.
  67.      */
  68.     @Override
  69.     public synchronized void write(final int ch) throws IOException {
  70.         switch (ch) {
  71.         case '\r':
  72.             lastWasCR = true;
  73.             out.write('\r');
  74.             return;
  75.         case '\n':
  76.             if (!lastWasCR) {
  77.                 out.write('\r');
  78.             }
  79.             //$FALL-THROUGH$
  80.         default:
  81.             lastWasCR = false;
  82.             out.write(ch);
  83.         }
  84.     }

  85. }