ToNetASCIIInputStream.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.FilterInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;

  21. import org.apache.commons.net.util.NetConstants;

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

  27. public final class ToNetASCIIInputStream extends FilterInputStream {
  28.     private static final int NOTHING_SPECIAL = 0;
  29.     private static final int LAST_WAS_CR = 1;
  30.     private static final int LAST_WAS_NL = 2;
  31.     private int status;

  32.     /**
  33.      * Creates a ToNetASCIIInputStream instance that wraps an existing InputStream.
  34.      *
  35.      * @param input The InputStream to wrap.
  36.      */
  37.     public ToNetASCIIInputStream(final InputStream input) {
  38.         super(input);
  39.         status = NOTHING_SPECIAL;
  40.     }

  41.     @Override
  42.     public int available() throws IOException {
  43.         final int result;

  44.         result = in.available();

  45.         if (status == LAST_WAS_NL) {
  46.             return result + 1;
  47.         }

  48.         return result;
  49.     }

  50.     /** Returns false. Mark is not supported. */
  51.     @Override
  52.     public boolean markSupported() {
  53.         return false;
  54.     }

  55.     /**
  56.      * Reads and returns the next byte in the stream. If the end of the message has been reached, returns -1.
  57.      *
  58.      * @return The next character in the stream. Returns -1 if the end of the stream has been reached.
  59.      * @throws IOException If an error occurs while reading the underlying stream.
  60.      */
  61.     @Override
  62.     public int read() throws IOException {
  63.         final int ch;

  64.         if (status == LAST_WAS_NL) {
  65.             status = NOTHING_SPECIAL;
  66.             return '\n';
  67.         }

  68.         ch = in.read();

  69.         switch (ch) {
  70.         case '\r':
  71.             status = LAST_WAS_CR;
  72.             return '\r';
  73.         case '\n':
  74.             if (status != LAST_WAS_CR) {
  75.                 status = LAST_WAS_NL;
  76.                 return '\r';
  77.             }
  78.             //$FALL-THROUGH$
  79.         default:
  80.             status = NOTHING_SPECIAL;
  81.             return ch;
  82.         }
  83.         // statement not reached
  84.         // return ch;
  85.     }

  86.     /**
  87.      * Reads the next number of bytes from the stream into an array and returns the number of bytes read. Returns -1 if the end of the stream has been reached.
  88.      *
  89.      * @param buffer The byte array in which to store the data.
  90.      * @return The number of bytes read. Returns -1 if the end of the message has been reached.
  91.      * @throws IOException If an error occurs in reading the underlying stream.
  92.      */
  93.     @Override
  94.     public int read(final byte[] buffer) throws IOException {
  95.         return read(buffer, 0, buffer.length);
  96.     }

  97.     /**
  98.      * Reads the next number of bytes from the stream into an array and returns the number of bytes read. Returns -1 if the end of the message has been reached.
  99.      * The characters are stored in the array starting from the given offset and up to the length specified.
  100.      *
  101.      * @param buffer The byte array in which to store the data.
  102.      * @param offset The offset into the array at which to start storing data.
  103.      * @param length The number of bytes to read.
  104.      * @return The number of bytes read. Returns -1 if the end of the stream has been reached.
  105.      * @throws IOException If an error occurs while reading the underlying stream.
  106.      */
  107.     @Override
  108.     public int read(final byte[] buffer, int offset, int length) throws IOException {
  109.         int ch;
  110.         final int off;

  111.         if (length < 1) {
  112.             return 0;
  113.         }

  114.         ch = available();

  115.         if (length > ch) {
  116.             length = ch;
  117.         }

  118.         // If nothing is available, block to read only one character
  119.         if (length < 1) {
  120.             length = 1;
  121.         }

  122.         if ((ch = read()) == NetConstants.EOS) {
  123.             return NetConstants.EOS;
  124.         }

  125.         off = offset;

  126.         do {
  127.             buffer[offset++] = (byte) ch;
  128.         } while (--length > 0 && (ch = read()) != NetConstants.EOS);

  129.         return offset - off;
  130.     }
  131. }