jaxws/src/java.xml.soap/share/classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/LineInputStream.java
changeset 28641 6b05689b7445
parent 28640 01e4ca94fb0d
parent 28505 7574ac3bb6c1
child 28642 a42fefc69922
equal deleted inserted replaced
28640:01e4ca94fb0d 28641:6b05689b7445
     1 /*
       
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @(#)LineInputStream.java   1.7 03/01/07
       
    28  */
       
    29 
       
    30 
       
    31 
       
    32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
       
    33 
       
    34 import java.io.*;
       
    35 
       
    36 /**
       
    37  * This class is to support reading CRLF terminated lines that
       
    38  * contain only US-ASCII characters from an input stream. Provides
       
    39  * functionality that is similar to the deprecated
       
    40  * <code>DataInputStream.readLine()</code>. Expected use is to read
       
    41  * lines as String objects from a RFC822 stream.
       
    42  *
       
    43  * It is implemented as a FilterInputStream, so one can just wrap
       
    44  * this class around any input stream and read bytes from this filter.
       
    45  *
       
    46  * @author John Mani
       
    47  */
       
    48 
       
    49 public final class LineInputStream extends FilterInputStream {
       
    50 
       
    51     private char[] lineBuffer = null; // reusable byte buffer
       
    52 
       
    53     public LineInputStream(InputStream in) {
       
    54         super(in);
       
    55     }
       
    56 
       
    57     /**
       
    58      * Read a line containing only ASCII characters from the input
       
    59      * stream. A line is terminated by a CR or NL or CR-NL sequence.
       
    60      * A common error is a CR-CR-NL sequence, which will also terminate
       
    61      * a line.
       
    62      * The line terminator is not returned as part of the returned
       
    63      * String. Returns null if no data is available. <p>
       
    64      *
       
    65      * This class is similar to the deprecated
       
    66      * <code>DataInputStream.readLine()</code>
       
    67      */
       
    68     public String readLine() throws IOException {
       
    69         InputStream in = this.in;
       
    70         char[] buf = lineBuffer;
       
    71 
       
    72         if (buf == null)
       
    73             buf = lineBuffer = new char[128];
       
    74 
       
    75         int c1;
       
    76         int room = buf.length;
       
    77         int offset = 0;
       
    78 
       
    79         while ((c1 = in.read()) != -1) {
       
    80             if (c1 == '\n') // Got NL, outa here.
       
    81                 break;
       
    82             else if (c1 == '\r') {
       
    83                 // Got CR, is the next char NL ?
       
    84                 int c2 = in.read();
       
    85                 if (c2 == '\r')         // discard extraneous CR
       
    86                     c2 = in.read();
       
    87                 if (c2 != '\n') {
       
    88                     // If not NL, push it back
       
    89                     if (!(in instanceof PushbackInputStream))
       
    90                         in = this.in = new PushbackInputStream(in);
       
    91                     ((PushbackInputStream)in).unread(c2);
       
    92                 }
       
    93                 break; // outa here.
       
    94             }
       
    95 
       
    96             // Not CR, NL or CR-NL ...
       
    97             // .. Insert the byte into our byte buffer
       
    98             if (--room < 0) { // No room, need to grow.
       
    99                 buf = new char[offset + 128];
       
   100                 room = buf.length - offset - 1;
       
   101                 System.arraycopy(lineBuffer, 0, buf, 0, offset);
       
   102                 lineBuffer = buf;
       
   103             }
       
   104             buf[offset++] = (char)c1;
       
   105         }
       
   106 
       
   107         if ((c1 == -1) && (offset == 0))
       
   108             return null;
       
   109 
       
   110         return String.copyValueOf(buf, 0, offset);
       
   111     }
       
   112 }