Path: blob/master/src/java.base/share/classes/java/io/LineNumberInputStream.java
41152 views
/*1* Copyright (c) 1995, 2019, 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 1.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@Deprecated49public class LineNumberInputStream extends FilterInputStream {50int pushBack = -1;51int lineNumber;52int markLineNumber;53int markPushBack = -1;5455/**56* Constructs a newline number input stream that reads its input57* from the specified input stream.58*59* @param in the underlying input stream.60*/61public LineNumberInputStream(InputStream in) {62super(in);63}6465/**66* Reads the next byte of data from this input stream. The value67* byte is returned as an {@code int} in the range68* {@code 0} to {@code 255}. If no byte is available69* because the end of the stream has been reached, the value70* {@code -1} is returned. This method blocks until input data71* is available, the end of the stream is detected, or an exception72* is thrown.73* <p>74* The {@code read} method of75* {@code LineNumberInputStream} calls the {@code read}76* method of the underlying input stream. It checks for carriage77* returns and newline characters in the input, and modifies the78* current line number as appropriate. A carriage-return character or79* a carriage return followed by a newline character are both80* converted into a single newline character.81*82* @return the next byte of data, or {@code -1} if the end of this83* stream is reached.84* @throws IOException if an I/O error occurs.85* @see java.io.FilterInputStream#in86* @see java.io.LineNumberInputStream#getLineNumber()87*/88@SuppressWarnings("fallthrough")89public int read() throws IOException {90int c = pushBack;9192if (c != -1) {93pushBack = -1;94} else {95c = in.read();96}9798switch (c) {99case '\r':100pushBack = in.read();101if (pushBack == '\n') {102pushBack = -1;103}104case '\n':105lineNumber++;106return '\n';107}108return c;109}110111/**112* Reads up to {@code len} bytes of data from this input stream113* into an array of bytes. This method blocks until some input is available.114* <p>115* The {@code read} method of116* {@code LineNumberInputStream} repeatedly calls the117* {@code read} method of zero arguments to fill in the byte array.118*119* @param b the buffer into which the data is read.120* @param off the start offset of the data.121* @param len the maximum number of bytes read.122* @return the total number of bytes read into the buffer, or123* {@code -1} if there is no more data because the end of124* this stream has been reached.125* @throws IOException if an I/O error occurs.126* @see java.io.LineNumberInputStream#read()127*/128public int read(byte b[], int off, int len) throws IOException {129if (b == null) {130throw new NullPointerException();131} else if ((off < 0) || (off > b.length) || (len < 0) ||132((off + len) > b.length) || ((off + len) < 0)) {133throw new IndexOutOfBoundsException();134} else if (len == 0) {135return 0;136}137138int c = read();139if (c == -1) {140return -1;141}142b[off] = (byte)c;143144int i = 1;145try {146for (; i < len ; i++) {147c = read();148if (c == -1) {149break;150}151if (b != null) {152b[off + i] = (byte)c;153}154}155} catch (IOException ee) {156}157return i;158}159160/**161* Skips over and discards {@code n} bytes of data from this162* input stream. The {@code skip} method may, for a variety of163* reasons, end up skipping over some smaller number of bytes,164* possibly {@code 0}. The actual number of bytes skipped is165* returned. If {@code n} is negative, no bytes are skipped.166* <p>167* The {@code skip} method of {@code LineNumberInputStream} creates168* a byte array and then repeatedly reads into it until169* {@code n} bytes have been read or the end of the stream has170* been reached.171*172* @param n the number of bytes to be skipped.173* @return the actual number of bytes skipped.174* @throws IOException if an I/O error occurs.175* @see java.io.FilterInputStream#in176*/177public long skip(long n) throws IOException {178int chunk = 2048;179long remaining = n;180byte data[];181int nr;182183if (n <= 0) {184return 0;185}186187data = new byte[chunk];188while (remaining > 0) {189nr = read(data, 0, (int) Math.min(chunk, remaining));190if (nr < 0) {191break;192}193remaining -= nr;194}195196return n - remaining;197}198199/**200* Sets the line number to the specified argument.201*202* @param lineNumber the new line number.203* @see #getLineNumber204*/205public void setLineNumber(int lineNumber) {206this.lineNumber = lineNumber;207}208209/**210* Returns the current line number.211*212* @return the current line number.213* @see #setLineNumber214*/215public int getLineNumber() {216return lineNumber;217}218219220/**221* Returns the number of bytes that can be read from this input222* stream without blocking.223* <p>224* Note that if the underlying input stream is able to supply225* <i>k</i> input characters without blocking, the226* {@code LineNumberInputStream} can guarantee only to provide227* <i>k</i>/2 characters without blocking, because the228* <i>k</i> characters from the underlying input stream might229* consist of <i>k</i>/2 pairs of {@code '\u005Cr'} and230* {@code '\u005Cn'}, which are converted to just231* <i>k</i>/2 {@code '\u005Cn'} characters.232*233* @return the number of bytes that can be read from this input stream234* without blocking.235* @throws IOException if an I/O error occurs.236* @see java.io.FilterInputStream#in237*/238public int available() throws IOException {239return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1;240}241242/**243* Marks the current position in this input stream. A subsequent244* call to the {@code reset} method repositions this stream at245* the last marked position so that subsequent reads re-read the same bytes.246* <p>247* The {@code mark} method of248* {@code LineNumberInputStream} remembers the current line249* number in a private variable, and then calls the {@code mark}250* method of the underlying input stream.251*252* @param readlimit the maximum limit of bytes that can be read before253* the mark position becomes invalid.254* @see java.io.FilterInputStream#in255* @see java.io.LineNumberInputStream#reset()256*/257public void mark(int readlimit) {258markLineNumber = lineNumber;259markPushBack = pushBack;260in.mark(readlimit);261}262263/**264* Repositions this stream to the position at the time the265* {@code mark} method was last called on this input stream.266* <p>267* The {@code reset} method of268* {@code LineNumberInputStream} resets the line number to be269* the line number at the time the {@code mark} method was270* called, and then calls the {@code reset} method of the271* underlying input stream.272* <p>273* Stream marks are intended to be used in274* situations where you need to read ahead a little to see what's in275* the stream. Often this is most easily done by invoking some276* general parser. If the stream is of the type handled by the277* parser, it just chugs along happily. If the stream is not of278* that type, the parser should toss an exception when it fails,279* which, if it happens within readlimit bytes, allows the outer280* code to reset the stream and try another parser.281*282* @throws IOException if an I/O error occurs.283* @see java.io.FilterInputStream#in284* @see java.io.LineNumberInputStream#mark(int)285*/286public void reset() throws IOException {287lineNumber = markLineNumber;288pushBack = markPushBack;289in.reset();290}291}292293294