src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/LineInputStream.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /* FROM mail.jar */
aoqi@0 27 package com.sun.xml.internal.org.jvnet.mimepull;
aoqi@0 28
aoqi@0 29 import java.io.*;
aoqi@0 30
aoqi@0 31 /**
aoqi@0 32 * This class is to support reading CRLF terminated lines that
aoqi@0 33 * contain only US-ASCII characters from an input stream. Provides
aoqi@0 34 * functionality that is similar to the deprecated
aoqi@0 35 * <code>DataInputStream.readLine()</code>. Expected use is to read
aoqi@0 36 * lines as String objects from a RFC822 stream.
aoqi@0 37 *
aoqi@0 38 * It is implemented as a FilterInputStream, so one can just wrap
aoqi@0 39 * this class around any input stream and read bytes from this filter.
aoqi@0 40 *
aoqi@0 41 * @author John Mani
aoqi@0 42 */
aoqi@0 43
aoqi@0 44 final class LineInputStream extends FilterInputStream {
aoqi@0 45
aoqi@0 46 private char[] lineBuffer = null; // reusable byte buffer
aoqi@0 47 private static int MAX_INCR = 1024*1024; // 1MB
aoqi@0 48
aoqi@0 49 public LineInputStream(InputStream in) {
aoqi@0 50 super(in);
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * Read a line containing only ASCII characters from the input
aoqi@0 55 * stream. A line is terminated by a CR or NL or CR-NL sequence.
aoqi@0 56 * A common error is a CR-CR-NL sequence, which will also terminate
aoqi@0 57 * a line.
aoqi@0 58 * The line terminator is not returned as part of the returned
aoqi@0 59 * String. Returns null if no data is available. <p>
aoqi@0 60 *
aoqi@0 61 * This class is similar to the deprecated
aoqi@0 62 * <code>DataInputStream.readLine()</code>
aoqi@0 63 */
aoqi@0 64 public String readLine() throws IOException {
aoqi@0 65 //InputStream in = this.in;
aoqi@0 66 char[] buf = lineBuffer;
aoqi@0 67
aoqi@0 68 if (buf == null) {
aoqi@0 69 buf = lineBuffer = new char[128];
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 int c1;
aoqi@0 73 int room = buf.length;
aoqi@0 74 int offset = 0;
aoqi@0 75
aoqi@0 76 while ((c1 = in.read()) != -1) {
aoqi@0 77 if (c1 == '\n') {
aoqi@0 78 break;
aoqi@0 79 } else if (c1 == '\r') {
aoqi@0 80 // Got CR, is the next char NL ?
aoqi@0 81 boolean twoCRs = false;
aoqi@0 82 if (in.markSupported()) {
aoqi@0 83 in.mark(2);
aoqi@0 84 }
aoqi@0 85 int c2 = in.read();
aoqi@0 86 if (c2 == '\r') { // discard extraneous CR
aoqi@0 87 twoCRs = true;
aoqi@0 88 c2 = in.read();
aoqi@0 89 }
aoqi@0 90 if (c2 != '\n') {
aoqi@0 91 /*
aoqi@0 92 * If the stream supports it (which we hope will always
aoqi@0 93 * be the case), reset to after the first CR. Otherwise,
aoqi@0 94 * we wrap a PushbackInputStream around the stream so we
aoqi@0 95 * can unread the characters we don't need. The only
aoqi@0 96 * problem with that is that the caller might stop
aoqi@0 97 * reading from this LineInputStream, throw it away,
aoqi@0 98 * and then start reading from the underlying stream.
aoqi@0 99 * If that happens, the pushed back characters will be
aoqi@0 100 * lost forever.
aoqi@0 101 */
aoqi@0 102 if (in.markSupported()) {
aoqi@0 103 in.reset();
aoqi@0 104 } else {
aoqi@0 105 if (!(in instanceof PushbackInputStream)) {
aoqi@0 106 in /*= this.in*/ = new PushbackInputStream(in, 2);
aoqi@0 107 }
aoqi@0 108 if (c2 != -1) {
aoqi@0 109 ((PushbackInputStream)in).unread(c2);
aoqi@0 110 }
aoqi@0 111 if (twoCRs) {
aoqi@0 112 ((PushbackInputStream)in).unread('\r');
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116 break; // outa here.
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 // Not CR, NL or CR-NL ...
aoqi@0 120 // .. Insert the byte into our byte buffer
aoqi@0 121 if (--room < 0) { // No room, need to grow.
aoqi@0 122 if (buf.length < MAX_INCR) {
aoqi@0 123 buf = new char[buf.length * 2];
aoqi@0 124 } else {
aoqi@0 125 buf = new char[buf.length + MAX_INCR];
aoqi@0 126 }
aoqi@0 127 room = buf.length - offset - 1;
aoqi@0 128 System.arraycopy(lineBuffer, 0, buf, 0, offset);
aoqi@0 129 lineBuffer = buf;
aoqi@0 130 }
aoqi@0 131 buf[offset++] = (char)c1;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 if ((c1 == -1) && (offset == 0)) {
aoqi@0 135 return null;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 return String.copyValueOf(buf, 0, offset);
aoqi@0 139 }
aoqi@0 140 }

mercurial