Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/LineNumberInputStream.java
38829 views
/*1* Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* This class is an input stream filter that provides the added29* functionality of keeping track of the current line number.30* <p>31* A line is a sequence of bytes ending with a carriage return32* character ({@code '\u005Cr'}), a newline character33* ({@code '\u005Cn'}), or a carriage return character followed34* immediately by a linefeed character. In all three cases, the line35* terminating character(s) are returned as a single newline character.36* <p>37* The line number begins at {@code 0}, and is incremented by38* {@code 1} when a {@code read} returns a newline character.39*40* @author Arthur van Hoff41* @see java.io.LineNumberReader42* @since JDK1.043* @deprecated This class incorrectly assumes that bytes adequately represent44* characters. As of JDK 1.1, the preferred way to operate on45* character streams is via the new character-stream classes, which46* include a class for counting line numbers.47*/48@Deprecated49public50class LineNumberInputStream extends FilterInputStream {51int pushBack = -1;52int lineNumber;53int markLineNumber;54int markPushBack = -1;5556/**57* Constructs a newline number input stream that reads its input58* from the specified input stream.59*60* @param in the underlying input stream.61*/62public LineNumberInputStream(InputStream in) {63super(in);64}6566/**67* Reads the next byte of data from this input stream. The value68* byte is returned as an {@code int} in the range69* {@code 0} to {@code 255}. If no byte is available70* because the end of the stream has been reached, the value71* {@code -1} is returned. This method blocks until input data72* is available, the end of the stream is detected, or an exception73* is thrown.74* <p>75* The {@code read} method of76* {@code LineNumberInputStream} calls the {@code read}77* method of the underlying input stream. It checks for carriage78* returns and newline characters in the input, and modifies the79* current line number as appropriate. A carriage-return character or80* a carriage return followed by a newline character are both81* converted into a single newline character.82*83* @return the next byte of data, or {@code -1} if the end of this84* stream is reached.85* @exception IOException if an I/O error occurs.86* @see java.io.FilterInputStream#in87* @see java.io.LineNumberInputStream#getLineNumber()88*/89@SuppressWarnings("fallthrough")90public int read() throws IOException {91int c = pushBack;9293if (c != -1) {94pushBack = -1;95} else {96c = in.read();97}9899switch (c) {100case '\r':101pushBack = in.read();102if (pushBack == '\n') {103pushBack = -1;104}105case '\n':106lineNumber++;107return '\n';108}109return c;110}111112/**113* Reads up to {@code len} bytes of data from this input stream114* into an array of bytes. This method blocks until some input is available.115* <p>116* The {@code read} method of117* {@code LineNumberInputStream} repeatedly calls the118* {@code read} method of zero arguments to fill in the byte array.119*120* @param b the buffer into which the data is read.121* @param off the start offset of the data.122* @param len the maximum number of bytes read.123* @return the total number of bytes read into the buffer, or124* {@code -1} if there is no more data because the end of125* this stream has been reached.126* @exception IOException if an I/O error occurs.127* @see java.io.LineNumberInputStream#read()128*/129public int read(byte b[], int off, int len) throws IOException {130if (b == null) {131throw new NullPointerException();132} else if ((off < 0) || (off > b.length) || (len < 0) ||133((off + len) > b.length) || ((off + len) < 0)) {134throw new IndexOutOfBoundsException();135} else if (len == 0) {136return 0;137}138139int c = read();140if (c == -1) {141return -1;142}143b[off] = (byte)c;144145int i = 1;146try {147for (; i < len ; i++) {148c = read();149if (c == -1) {150break;151}152if (b != null) {153b[off + i] = (byte)c;154}155}156} catch (IOException ee) {157}158return i;159}160161/**162* Skips over and discards {@code n} bytes of data from this163* input stream. The {@code skip} method may, for a variety of164* reasons, end up skipping over some smaller number of bytes,165* possibly {@code 0}. The actual number of bytes skipped is166* returned. If {@code n} is negative, no bytes are skipped.167* <p>168* The {@code skip} method of {@code LineNumberInputStream} creates169* a byte array and then repeatedly reads into it until170* {@code n} bytes have been read or the end of the stream has171* been reached.172*173* @param n the number of bytes to be skipped.174* @return the actual number of bytes skipped.175* @exception IOException if an I/O error occurs.176* @see java.io.FilterInputStream#in177*/178public long skip(long n) throws IOException {179int chunk = 2048;180long remaining = n;181byte data[];182int nr;183184if (n <= 0) {185return 0;186}187188data = new byte[chunk];189while (remaining > 0) {190nr = read(data, 0, (int) Math.min(chunk, remaining));191if (nr < 0) {192break;193}194remaining -= nr;195}196197return n - remaining;198}199200/**201* Sets the line number to the specified argument.202*203* @param lineNumber the new line number.204* @see #getLineNumber205*/206public void setLineNumber(int lineNumber) {207this.lineNumber = lineNumber;208}209210/**211* Returns the current line number.212*213* @return the current line number.214* @see #setLineNumber215*/216public int getLineNumber() {217return lineNumber;218}219220221/**222* Returns the number of bytes that can be read from this input223* stream without blocking.224* <p>225* Note that if the underlying input stream is able to supply226* <i>k</i> input characters without blocking, the227* {@code LineNumberInputStream} can guarantee only to provide228* <i>k</i>/2 characters without blocking, because the229* <i>k</i> characters from the underlying input stream might230* consist of <i>k</i>/2 pairs of {@code '\u005Cr'} and231* {@code '\u005Cn'}, which are converted to just232* <i>k</i>/2 {@code '\u005Cn'} characters.233*234* @return the number of bytes that can be read from this input stream235* without blocking.236* @exception IOException if an I/O error occurs.237* @see java.io.FilterInputStream#in238*/239public int available() throws IOException {240return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1;241}242243/**244* Marks the current position in this input stream. A subsequent245* call to the {@code reset} method repositions this stream at246* the last marked position so that subsequent reads re-read the same bytes.247* <p>248* The {@code mark} method of249* {@code LineNumberInputStream} remembers the current line250* number in a private variable, and then calls the {@code mark}251* method of the underlying input stream.252*253* @param readlimit the maximum limit of bytes that can be read before254* the mark position becomes invalid.255* @see java.io.FilterInputStream#in256* @see java.io.LineNumberInputStream#reset()257*/258public void mark(int readlimit) {259markLineNumber = lineNumber;260markPushBack = pushBack;261in.mark(readlimit);262}263264/**265* Repositions this stream to the position at the time the266* {@code mark} method was last called on this input stream.267* <p>268* The {@code reset} method of269* {@code LineNumberInputStream} resets the line number to be270* the line number at the time the {@code mark} method was271* called, and then calls the {@code reset} method of the272* underlying input stream.273* <p>274* Stream marks are intended to be used in275* situations where you need to read ahead a little to see what's in276* the stream. Often this is most easily done by invoking some277* general parser. If the stream is of the type handled by the278* parser, it just chugs along happily. If the stream is not of279* that type, the parser should toss an exception when it fails,280* which, if it happens within readlimit bytes, allows the outer281* code to reset the stream and try another parser.282*283* @exception IOException if an I/O error occurs.284* @see java.io.FilterInputStream#in285* @see java.io.LineNumberInputStream#mark(int)286*/287public void reset() throws IOException {288lineNumber = markLineNumber;289pushBack = markPushBack;290in.reset();291}292}293294295