aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* FROM mail.jar */ aoqi@0: package com.sun.xml.internal.org.jvnet.mimepull; aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: /** aoqi@0: * This class is to support reading CRLF terminated lines that aoqi@0: * contain only US-ASCII characters from an input stream. Provides aoqi@0: * functionality that is similar to the deprecated aoqi@0: * DataInputStream.readLine(). Expected use is to read aoqi@0: * lines as String objects from a RFC822 stream. aoqi@0: * aoqi@0: * It is implemented as a FilterInputStream, so one can just wrap aoqi@0: * this class around any input stream and read bytes from this filter. aoqi@0: * aoqi@0: * @author John Mani aoqi@0: */ aoqi@0: aoqi@0: final class LineInputStream extends FilterInputStream { aoqi@0: aoqi@0: private char[] lineBuffer = null; // reusable byte buffer aoqi@0: private static int MAX_INCR = 1024*1024; // 1MB aoqi@0: aoqi@0: public LineInputStream(InputStream in) { aoqi@0: super(in); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Read a line containing only ASCII characters from the input aoqi@0: * stream. A line is terminated by a CR or NL or CR-NL sequence. aoqi@0: * A common error is a CR-CR-NL sequence, which will also terminate aoqi@0: * a line. aoqi@0: * The line terminator is not returned as part of the returned aoqi@0: * String. Returns null if no data is available.

aoqi@0: * aoqi@0: * This class is similar to the deprecated aoqi@0: * DataInputStream.readLine() aoqi@0: */ aoqi@0: public String readLine() throws IOException { aoqi@0: //InputStream in = this.in; aoqi@0: char[] buf = lineBuffer; aoqi@0: aoqi@0: if (buf == null) { aoqi@0: buf = lineBuffer = new char[128]; aoqi@0: } aoqi@0: aoqi@0: int c1; aoqi@0: int room = buf.length; aoqi@0: int offset = 0; aoqi@0: aoqi@0: while ((c1 = in.read()) != -1) { aoqi@0: if (c1 == '\n') { aoqi@0: break; aoqi@0: } else if (c1 == '\r') { aoqi@0: // Got CR, is the next char NL ? aoqi@0: boolean twoCRs = false; aoqi@0: if (in.markSupported()) { aoqi@0: in.mark(2); aoqi@0: } aoqi@0: int c2 = in.read(); aoqi@0: if (c2 == '\r') { // discard extraneous CR aoqi@0: twoCRs = true; aoqi@0: c2 = in.read(); aoqi@0: } aoqi@0: if (c2 != '\n') { aoqi@0: /* aoqi@0: * If the stream supports it (which we hope will always aoqi@0: * be the case), reset to after the first CR. Otherwise, aoqi@0: * we wrap a PushbackInputStream around the stream so we aoqi@0: * can unread the characters we don't need. The only aoqi@0: * problem with that is that the caller might stop aoqi@0: * reading from this LineInputStream, throw it away, aoqi@0: * and then start reading from the underlying stream. aoqi@0: * If that happens, the pushed back characters will be aoqi@0: * lost forever. aoqi@0: */ aoqi@0: if (in.markSupported()) { aoqi@0: in.reset(); aoqi@0: } else { aoqi@0: if (!(in instanceof PushbackInputStream)) { aoqi@0: in /*= this.in*/ = new PushbackInputStream(in, 2); aoqi@0: } aoqi@0: if (c2 != -1) { aoqi@0: ((PushbackInputStream)in).unread(c2); aoqi@0: } aoqi@0: if (twoCRs) { aoqi@0: ((PushbackInputStream)in).unread('\r'); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: break; // outa here. aoqi@0: } aoqi@0: aoqi@0: // Not CR, NL or CR-NL ... aoqi@0: // .. Insert the byte into our byte buffer aoqi@0: if (--room < 0) { // No room, need to grow. aoqi@0: if (buf.length < MAX_INCR) { aoqi@0: buf = new char[buf.length * 2]; aoqi@0: } else { aoqi@0: buf = new char[buf.length + MAX_INCR]; aoqi@0: } aoqi@0: room = buf.length - offset - 1; aoqi@0: System.arraycopy(lineBuffer, 0, buf, 0, offset); aoqi@0: lineBuffer = buf; aoqi@0: } aoqi@0: buf[offset++] = (char)c1; aoqi@0: } aoqi@0: aoqi@0: if ((c1 == -1) && (offset == 0)) { aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: return String.copyValueOf(buf, 0, offset); aoqi@0: } aoqi@0: }