1 /*
   2  * Copyright (c) 1997, 2017, 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      * @return line.
  69      *
  70      * @throws IOException if an I/O error occurs.
  71      */
  72     public String readLine() throws IOException {
  73         InputStream in = this.in;
  74         char[] buf = lineBuffer;
  75 
  76         if (buf == null)
  77             buf = lineBuffer = new char[128];
  78 
  79         int c1;
  80         int room = buf.length;
  81         int offset = 0;
  82 
  83         while ((c1 = in.read()) != -1) {
  84             if (c1 == '\n') // Got NL, outa here.
  85                 break;
  86             else if (c1 == '\r') {
  87                 // Got CR, is the next char NL ?
  88                 int c2 = in.read();
  89                 if (c2 == '\r')         // discard extraneous CR
  90                     c2 = in.read();
  91                 if (c2 != '\n') {
  92                     // If not NL, push it back
  93                     if (!(in instanceof PushbackInputStream))
  94                         in = this.in = new PushbackInputStream(in);
  95                     ((PushbackInputStream)in).unread(c2);
  96                 }
  97                 break; // outa here.
  98             }
  99 
 100             // Not CR, NL or CR-NL ...
 101             // .. Insert the byte into our byte buffer
 102             if (--room < 0) { // No room, need to grow.
 103                 buf = new char[offset + 128];
 104                 room = buf.length - offset - 1;
 105                 System.arraycopy(lineBuffer, 0, buf, 0, offset);
 106                 lineBuffer = buf;
 107             }
 108             buf[offset++] = (char)c1;
 109         }
 110 
 111         if ((c1 == -1) && (offset == 0))
 112             return null;
 113 
 114         return String.copyValueOf(buf, 0, offset);
 115     }
 116 }