1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/io/LineNumberReader.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,281 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +
1.32 +/**
1.33 + * A buffered character-input stream that keeps track of line numbers. This
1.34 + * class defines methods {@link #setLineNumber(int)} and {@link
1.35 + * #getLineNumber()} for setting and getting the current line number
1.36 + * respectively.
1.37 + *
1.38 + * <p> By default, line numbering begins at 0. This number increments at every
1.39 + * <a href="#lt">line terminator</a> as the data is read, and can be changed
1.40 + * with a call to <tt>setLineNumber(int)</tt>. Note however, that
1.41 + * <tt>setLineNumber(int)</tt> does not actually change the current position in
1.42 + * the stream; it only changes the value that will be returned by
1.43 + * <tt>getLineNumber()</tt>.
1.44 + *
1.45 + * <p> A line is considered to be <a name="lt">terminated</a> by any one of a
1.46 + * line feed ('\n'), a carriage return ('\r'), or a carriage return followed
1.47 + * immediately by a linefeed.
1.48 + *
1.49 + * @author Mark Reinhold
1.50 + * @since JDK1.1
1.51 + */
1.52 +
1.53 +public class LineNumberReader extends BufferedReader {
1.54 +
1.55 + /** The current line number */
1.56 + private int lineNumber = 0;
1.57 +
1.58 + /** The line number of the mark, if any */
1.59 + private int markedLineNumber; // Defaults to 0
1.60 +
1.61 + /** If the next character is a line feed, skip it */
1.62 + private boolean skipLF;
1.63 +
1.64 + /** The skipLF flag when the mark was set */
1.65 + private boolean markedSkipLF;
1.66 +
1.67 + /**
1.68 + * Create a new line-numbering reader, using the default input-buffer
1.69 + * size.
1.70 + *
1.71 + * @param in
1.72 + * A Reader object to provide the underlying stream
1.73 + */
1.74 + public LineNumberReader(Reader in) {
1.75 + super(in);
1.76 + }
1.77 +
1.78 + /**
1.79 + * Create a new line-numbering reader, reading characters into a buffer of
1.80 + * the given size.
1.81 + *
1.82 + * @param in
1.83 + * A Reader object to provide the underlying stream
1.84 + *
1.85 + * @param sz
1.86 + * An int specifying the size of the buffer
1.87 + */
1.88 + public LineNumberReader(Reader in, int sz) {
1.89 + super(in, sz);
1.90 + }
1.91 +
1.92 + /**
1.93 + * Set the current line number.
1.94 + *
1.95 + * @param lineNumber
1.96 + * An int specifying the line number
1.97 + *
1.98 + * @see #getLineNumber
1.99 + */
1.100 + public void setLineNumber(int lineNumber) {
1.101 + this.lineNumber = lineNumber;
1.102 + }
1.103 +
1.104 + /**
1.105 + * Get the current line number.
1.106 + *
1.107 + * @return The current line number
1.108 + *
1.109 + * @see #setLineNumber
1.110 + */
1.111 + public int getLineNumber() {
1.112 + return lineNumber;
1.113 + }
1.114 +
1.115 + /**
1.116 + * Read a single character. <a href="#lt">Line terminators</a> are
1.117 + * compressed into single newline ('\n') characters. Whenever a line
1.118 + * terminator is read the current line number is incremented.
1.119 + *
1.120 + * @return The character read, or -1 if the end of the stream has been
1.121 + * reached
1.122 + *
1.123 + * @throws IOException
1.124 + * If an I/O error occurs
1.125 + */
1.126 + public int read() throws IOException {
1.127 + synchronized (lock) {
1.128 + int c = super.read();
1.129 + if (skipLF) {
1.130 + if (c == '\n')
1.131 + c = super.read();
1.132 + skipLF = false;
1.133 + }
1.134 + switch (c) {
1.135 + case '\r':
1.136 + skipLF = true;
1.137 + case '\n': /* Fall through */
1.138 + lineNumber++;
1.139 + return '\n';
1.140 + }
1.141 + return c;
1.142 + }
1.143 + }
1.144 +
1.145 + /**
1.146 + * Read characters into a portion of an array. Whenever a <a
1.147 + * href="#lt">line terminator</a> is read the current line number is
1.148 + * incremented.
1.149 + *
1.150 + * @param cbuf
1.151 + * Destination buffer
1.152 + *
1.153 + * @param off
1.154 + * Offset at which to start storing characters
1.155 + *
1.156 + * @param len
1.157 + * Maximum number of characters to read
1.158 + *
1.159 + * @return The number of bytes read, or -1 if the end of the stream has
1.160 + * already been reached
1.161 + *
1.162 + * @throws IOException
1.163 + * If an I/O error occurs
1.164 + */
1.165 + public int read(char cbuf[], int off, int len) throws IOException {
1.166 + synchronized (lock) {
1.167 + int n = super.read(cbuf, off, len);
1.168 +
1.169 + for (int i = off; i < off + n; i++) {
1.170 + int c = cbuf[i];
1.171 + if (skipLF) {
1.172 + skipLF = false;
1.173 + if (c == '\n')
1.174 + continue;
1.175 + }
1.176 + switch (c) {
1.177 + case '\r':
1.178 + skipLF = true;
1.179 + case '\n': /* Fall through */
1.180 + lineNumber++;
1.181 + break;
1.182 + }
1.183 + }
1.184 +
1.185 + return n;
1.186 + }
1.187 + }
1.188 +
1.189 + /**
1.190 + * Read a line of text. Whenever a <a href="#lt">line terminator</a> is
1.191 + * read the current line number is incremented.
1.192 + *
1.193 + * @return A String containing the contents of the line, not including
1.194 + * any <a href="#lt">line termination characters</a>, or
1.195 + * <tt>null</tt> if the end of the stream has been reached
1.196 + *
1.197 + * @throws IOException
1.198 + * If an I/O error occurs
1.199 + */
1.200 + public String readLine() throws IOException {
1.201 + synchronized (lock) {
1.202 + String l = super.readLine(skipLF);
1.203 + skipLF = false;
1.204 + if (l != null)
1.205 + lineNumber++;
1.206 + return l;
1.207 + }
1.208 + }
1.209 +
1.210 + /** Maximum skip-buffer size */
1.211 + private static final int maxSkipBufferSize = 8192;
1.212 +
1.213 + /** Skip buffer, null until allocated */
1.214 + private char skipBuffer[] = null;
1.215 +
1.216 + /**
1.217 + * Skip characters.
1.218 + *
1.219 + * @param n
1.220 + * The number of characters to skip
1.221 + *
1.222 + * @return The number of characters actually skipped
1.223 + *
1.224 + * @throws IOException
1.225 + * If an I/O error occurs
1.226 + *
1.227 + * @throws IllegalArgumentException
1.228 + * If <tt>n</tt> is negative
1.229 + */
1.230 + public long skip(long n) throws IOException {
1.231 + if (n < 0)
1.232 + throw new IllegalArgumentException("skip() value is negative");
1.233 + int nn = (int) Math.min(n, maxSkipBufferSize);
1.234 + synchronized (lock) {
1.235 + if ((skipBuffer == null) || (skipBuffer.length < nn))
1.236 + skipBuffer = new char[nn];
1.237 + long r = n;
1.238 + while (r > 0) {
1.239 + int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
1.240 + if (nc == -1)
1.241 + break;
1.242 + r -= nc;
1.243 + }
1.244 + return n - r;
1.245 + }
1.246 + }
1.247 +
1.248 + /**
1.249 + * Mark the present position in the stream. Subsequent calls to reset()
1.250 + * will attempt to reposition the stream to this point, and will also reset
1.251 + * the line number appropriately.
1.252 + *
1.253 + * @param readAheadLimit
1.254 + * Limit on the number of characters that may be read while still
1.255 + * preserving the mark. After reading this many characters,
1.256 + * attempting to reset the stream may fail.
1.257 + *
1.258 + * @throws IOException
1.259 + * If an I/O error occurs
1.260 + */
1.261 + public void mark(int readAheadLimit) throws IOException {
1.262 + synchronized (lock) {
1.263 + super.mark(readAheadLimit);
1.264 + markedLineNumber = lineNumber;
1.265 + markedSkipLF = skipLF;
1.266 + }
1.267 + }
1.268 +
1.269 + /**
1.270 + * Reset the stream to the most recent mark.
1.271 + *
1.272 + * @throws IOException
1.273 + * If the stream has not been marked, or if the mark has been
1.274 + * invalidated
1.275 + */
1.276 + public void reset() throws IOException {
1.277 + synchronized (lock) {
1.278 + super.reset();
1.279 + lineNumber = markedLineNumber;
1.280 + skipLF = markedSkipLF;
1.281 + }
1.282 + }
1.283 +
1.284 +}