    View Javadoc
    1   package org.apache.commons.net.io;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.FilterInputStream;
    58  import java.io.IOException;
    59  import java.io.InputStream;
    60
    61  /****
    62   * This class wraps an input stream, replacing all singly occurring
    63   * <LF> (linefeed) characters with <CR><LF> (carriage return
    64   * followed by linefeed), which is the NETASCII standard for representing
    65   * a newline.
    66   * You would use this class to implement ASCII file transfers requiring
    67   * conversion to NETASCII.
    68   * <p>
    69   * <p>
    70   * @author Daniel F. Savarese
    71   ***/
    72
    73  public final class ToNetASCIIInputStream extends FilterInputStream
    74  {
    75      private static final int __NOTHING_SPECIAL = 0;
    76      private static final int __LAST_WAS_CR = 1;
    77      private static final int __LAST_WAS_NL = 2;
    78      private int __status;
    79
    80      /****
    81       * Creates a ToNetASCIIInputStream instance that wraps an existing
    82       * InputStream.
    83       * <p>
    84       * @param input  The InputStream to .
    85       ***/
    86      public ToNetASCIIInputStream(InputStream input)
    87      {
    88          super(input);
    89          __status = __NOTHING_SPECIAL;
    90      }
    91
    92
    93      /****
    94       * Reads and returns the next byte in the stream.  If the end of the
    95       * message has been reached, returns -1.
    96       * <p>
    97       * @return The next character in the stream. Returns -1 if the end of the
    98       *          stream has been reached.
    99       * @exception IOException If an error occurs while reading the underlying
    100      *            stream.
    101      ***/
    102     public int read() throws IOException
    103     {
    104         int ch;
    105
    106         if (__status == __LAST_WAS_NL)
    107         {
    108             __status = __NOTHING_SPECIAL;
    109             return '\n';
    110         }
    111
    112         ch = in.read();
    113
    114         switch (ch)
    115         {
    116         case '\r':
    117             __status = __LAST_WAS_CR;
    118             return '\r';
    119         case '\n':
    120             if (__status != __LAST_WAS_CR)
    121             {
    122                 __status = __LAST_WAS_NL;
    123                 return '\r';
    124             }
    125             // else fall through
    126         default:
    127             __status = __NOTHING_SPECIAL;
    128             return ch;
    129         }
    130         // statement not reached
    131         //return ch;
    132     }
    133
    134
    135     /****
    136      * Reads the next number of bytes from the stream into an array and
    137      * returns the number of bytes read.  Returns -1 if the end of the
    138      * stream has been reached.
    139      * <p>
    140      * @param buffer  The byte array in which to store the data.
    141      * @return The number of bytes read. Returns -1 if the
    142      *          end of the message has been reached.
    143      * @exception IOException If an error occurs in reading the underlying
    144      *            stream.
    145      ***/
    146     public int read(byte buffer[]) throws IOException
    147     {
    148         return read(buffer, 0, buffer.length);
    149     }
    150
    151
    152     /****
    153      * Reads the next number of bytes from the stream into an array and returns
    154      * the number of bytes read.  Returns -1 if the end of the
    155      * message has been reached.  The characters are stored in the array
    156      * starting from the given offset and up to the length specified.
    157      * <p>
    158      * @param buffer The byte array in which to store the data.
    159      * @param offset  The offset into the array at which to start storing data.
    160      * @param length   The number of bytes to read.
    161      * @return The number of bytes read. Returns -1 if the
    162      *          end of the stream has been reached.
    163      * @exception IOException If an error occurs while reading the underlying
    164      *            stream.
    165      ***/
    166     public int read(byte buffer[], int offset, int length) throws IOException
    167     {
    168         int ch, off;
    169
    170         if (length < 1)
    171             return 0;
    172
    173         ch = available();
    174
    175         if (length > ch)
    176             length = ch;
    177
    178         // If nothing is available, block to read only one character
    179         if (length < 1)
    180             length = 1;
    181
    182         if ((ch = read()) == -1)
    183             return -1;
    184
    185         off = offset;
    186
    187         do
    188         {
    189             buffer[offset++] = (byte)ch;
    190         }
    191         while (--length > 0 && (ch = read()) != -1);
    192
    193         return (offset - off);
    194     }
    195
    196     /**** Returns false.  Mark is not supported. ***/
    197     public boolean markSupported()
    198     {
    199         return false;
    200     }
    201
    202     public int available() throws IOException
    203     {
    204         int result;
    205
    206         result = in.available();
    207
    208         if (__status == __LAST_WAS_NL)
    209             return (result + 1);
    210
    211         return result;
    212     }
    213 }

This page was automatically generated by Maven