    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.IOException;
    58  import java.io.InputStream;
    59  import java.io.PushbackInputStream;
    60
    61  /****
    62   * This class wraps an input stream, replacing all occurrences
    63   * of <CR><LF> (carriage return followed by a linefeed),
    64   * which is the NETASCII standard for representing a newline, with the
    65   * local line separator representation.  You would use this class to
    66   * implement ASCII file transfers requiring conversion from NETASCII.
    67   * <p>
    68   * <p>
    69   * @author Daniel F. Savarese
    70   ***/
    71
    72  public final class FromNetASCIIInputStream extends PushbackInputStream
    73  {
    74      static final boolean _noConversionRequired;
    75      static final String _lineSeparator;
    76      static final byte[] _lineSeparatorBytes;
    77
    78      static {
    79          _lineSeparator = System.getProperty("line.separator");
    80          _noConversionRequired = _lineSeparator.equals("\r\n");
    81          _lineSeparatorBytes = _lineSeparator.getBytes();
    82      }
    83
    84      private int __length = 0;
    85
    86      /****
    87       * Returns true if the NetASCII line separator differs from the system
    88       * line separator, false if they are the same.  This method is useful
    89       * to determine whether or not you need to instantiate a
    90       * FromNetASCIIInputStream object.
    91       * <p>
    92       * @return True if the NETASCII line separator differs from the local
    93       *   system line separator, false if they are the same.
    94       ***/
    95      public static final boolean isConversionRequired()
    96      {
    97          return !_noConversionRequired;
    98      }
    99
    100     /****
    101      * Creates a FromNetASCIIInputStream instance that wraps an existing
    102      * InputStream.
    103      ***/
    104     public FromNetASCIIInputStream(InputStream input)
    105     {
    106         super(input, _lineSeparatorBytes.length + 1);
    107     }
    108
    109
    110     private int __read() throws IOException
    111     {
    112         int ch;
    113
    114         ch = super.read();
    115
    116         if (ch == '\r')
    117         {
    118             ch = super.read();
    119             if (ch == '\n')
    120             {
    121                 unread(_lineSeparatorBytes);
    122                 ch = super.read();
    123                 // This is a kluge for read(byte[], ...) to read the right amount
    124                 --__length;
    125             }
    126             else
    127             {
    128                 if (ch != -1)
    129                     unread(ch);
    130                 return '\r';
    131             }
    132         }
    133
    134         return ch;
    135     }
    136
    137
    138     /****
    139      * Reads and returns the next byte in the stream.  If the end of the
    140      * message has been reached, returns -1.  Note that a call to this method
    141      * may result in multiple reads from the underlying input stream in order
    142      * to convert NETASCII line separators to the local line separator format.
    143      * This is transparent to the programmer and is only mentioned for
    144      * completeness.
    145      * <p>
    146      * @return The next character in the stream. Returns -1 if the end of the
    147      *          stream has been reached.
    148      * @exception IOException If an error occurs while reading the underlying
    149      *            stream.
    150      ***/
    151     public int read() throws IOException
    152     {
    153         if (_noConversionRequired)
    154             return super.read();
    155
    156         return __read();
    157     }
    158
    159
    160     /****
    161      * Reads the next number of bytes from the stream into an array and
    162      * returns the number of bytes read.  Returns -1 if the end of the
    163      * stream has been reached.
    164      * <p>
    165      * @param buffer  The byte array in which to store the data.
    166      * @return The number of bytes read. Returns -1 if the
    167      *          end of the message has been reached.
    168      * @exception IOException If an error occurs in reading the underlying
    169      *            stream.
    170      ***/
    171     public int read(byte buffer[]) throws IOException
    172     {
    173         return read(buffer, 0, buffer.length);
    174     }
    175
    176
    177     /****
    178      * Reads the next number of bytes from the stream into an array and returns
    179      * the number of bytes read.  Returns -1 if the end of the
    180      * message has been reached.  The characters are stored in the array
    181      * starting from the given offset and up to the length specified.
    182      * <p>
    183      * @param buffer The byte array in which to store the data.
    184      * @param offset  The offset into the array at which to start storing data.
    185      * @param length   The number of bytes to read.
    186      * @return The number of bytes read. Returns -1 if the
    187      *          end of the stream has been reached.
    188      * @exception IOException If an error occurs while reading the underlying
    189      *            stream.
    190      ***/
    191     public int read(byte buffer[], int offset, int length) throws IOException
    192     {
    193         int ch, off;
    194
    195         if (length < 1)
    196             return 0;
    197
    198         ch = available();
    199
    200         __length = (length > ch ? ch : length);
    201
    202         // If nothing is available, block to read only one character
    203         if (__length < 1)
    204             __length = 1;
    205
    206         if (_noConversionRequired)
    207             return super.read(buffer, offset, __length);
    208
    209         if ((ch = __read()) == -1)
    210             return -1;
    211
    212         off = offset;
    213
    214         do
    215         {
    216             buffer[offset++] = (byte)ch;
    217         }
    218         while (--__length > 0 && (ch = __read()) != -1);
    219
    220
    221         return (offset - off);
    222     }
    223
    224
    225     // PushbackInputStream in JDK 1.1.3 returns the wrong thing
    226     /****
    227      * Returns the number of bytes that can be read without blocking EXCEPT
    228      * when newline conversions have to be made somewhere within the
    229      * available block of bytes.  In other words, you really should not
    230      * rely on the value returned by this method if you are trying to avoid
    231      * blocking.
    232      ***/
    233     public int available() throws IOException
    234     {
    235         return (buf.length - pos) + in.available();
    236     }
    237
    238 }

This page was automatically generated by Maven