rt/emul/compact/src/main/java/java/io/LineNumberInputStream.java
branchjdk7-b147
changeset 1334 588d5bf7a560
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/io/LineNumberInputStream.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,292 @@
     1.4 +/*
     1.5 + * Copyright (c) 1995, 2004, 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 + * This class is an input stream filter that provides the added
    1.33 + * functionality of keeping track of the current line number.
    1.34 + * <p>
    1.35 + * A line is a sequence of bytes ending with a carriage return
    1.36 + * character (<code>'&#92;r'</code>), a newline character
    1.37 + * (<code>'&#92;n'</code>), or a carriage return character followed
    1.38 + * immediately by a linefeed character. In all three cases, the line
    1.39 + * terminating character(s) are returned as a single newline character.
    1.40 + * <p>
    1.41 + * The line number begins at <code>0</code>, and is incremented by
    1.42 + * <code>1</code> when a <code>read</code> returns a newline character.
    1.43 + *
    1.44 + * @author     Arthur van Hoff
    1.45 + * @see        java.io.LineNumberReader
    1.46 + * @since      JDK1.0
    1.47 + * @deprecated This class incorrectly assumes that bytes adequately represent
    1.48 + *             characters.  As of JDK&nbsp;1.1, the preferred way to operate on
    1.49 + *             character streams is via the new character-stream classes, which
    1.50 + *             include a class for counting line numbers.
    1.51 + */
    1.52 +@Deprecated
    1.53 +public
    1.54 +class LineNumberInputStream extends FilterInputStream {
    1.55 +    int pushBack = -1;
    1.56 +    int lineNumber;
    1.57 +    int markLineNumber;
    1.58 +    int markPushBack = -1;
    1.59 +
    1.60 +    /**
    1.61 +     * Constructs a newline number input stream that reads its input
    1.62 +     * from the specified input stream.
    1.63 +     *
    1.64 +     * @param      in   the underlying input stream.
    1.65 +     */
    1.66 +    public LineNumberInputStream(InputStream in) {
    1.67 +        super(in);
    1.68 +    }
    1.69 +
    1.70 +    /**
    1.71 +     * Reads the next byte of data from this input stream. The value
    1.72 +     * byte is returned as an <code>int</code> in the range
    1.73 +     * <code>0</code> to <code>255</code>. If no byte is available
    1.74 +     * because the end of the stream has been reached, the value
    1.75 +     * <code>-1</code> is returned. This method blocks until input data
    1.76 +     * is available, the end of the stream is detected, or an exception
    1.77 +     * is thrown.
    1.78 +     * <p>
    1.79 +     * The <code>read</code> method of
    1.80 +     * <code>LineNumberInputStream</code> calls the <code>read</code>
    1.81 +     * method of the underlying input stream. It checks for carriage
    1.82 +     * returns and newline characters in the input, and modifies the
    1.83 +     * current line number as appropriate. A carriage-return character or
    1.84 +     * a carriage return followed by a newline character are both
    1.85 +     * converted into a single newline character.
    1.86 +     *
    1.87 +     * @return     the next byte of data, or <code>-1</code> if the end of this
    1.88 +     *             stream is reached.
    1.89 +     * @exception  IOException  if an I/O error occurs.
    1.90 +     * @see        java.io.FilterInputStream#in
    1.91 +     * @see        java.io.LineNumberInputStream#getLineNumber()
    1.92 +     */
    1.93 +    public int read() throws IOException {
    1.94 +        int c = pushBack;
    1.95 +
    1.96 +        if (c != -1) {
    1.97 +            pushBack = -1;
    1.98 +        } else {
    1.99 +            c = in.read();
   1.100 +        }
   1.101 +
   1.102 +        switch (c) {
   1.103 +          case '\r':
   1.104 +            pushBack = in.read();
   1.105 +            if (pushBack == '\n') {
   1.106 +                pushBack = -1;
   1.107 +            }
   1.108 +          case '\n':
   1.109 +            lineNumber++;
   1.110 +            return '\n';
   1.111 +        }
   1.112 +        return c;
   1.113 +    }
   1.114 +
   1.115 +    /**
   1.116 +     * Reads up to <code>len</code> bytes of data from this input stream
   1.117 +     * into an array of bytes. This method blocks until some input is available.
   1.118 +     * <p>
   1.119 +     * The <code>read</code> method of
   1.120 +     * <code>LineNumberInputStream</code> repeatedly calls the
   1.121 +     * <code>read</code> method of zero arguments to fill in the byte array.
   1.122 +     *
   1.123 +     * @param      b     the buffer into which the data is read.
   1.124 +     * @param      off   the start offset of the data.
   1.125 +     * @param      len   the maximum number of bytes read.
   1.126 +     * @return     the total number of bytes read into the buffer, or
   1.127 +     *             <code>-1</code> if there is no more data because the end of
   1.128 +     *             this stream has been reached.
   1.129 +     * @exception  IOException  if an I/O error occurs.
   1.130 +     * @see        java.io.LineNumberInputStream#read()
   1.131 +     */
   1.132 +    public int read(byte b[], int off, int len) throws IOException {
   1.133 +        if (b == null) {
   1.134 +            throw new NullPointerException();
   1.135 +        } else if ((off < 0) || (off > b.length) || (len < 0) ||
   1.136 +                   ((off + len) > b.length) || ((off + len) < 0)) {
   1.137 +            throw new IndexOutOfBoundsException();
   1.138 +        } else if (len == 0) {
   1.139 +            return 0;
   1.140 +        }
   1.141 +
   1.142 +        int c = read();
   1.143 +        if (c == -1) {
   1.144 +            return -1;
   1.145 +        }
   1.146 +        b[off] = (byte)c;
   1.147 +
   1.148 +        int i = 1;
   1.149 +        try {
   1.150 +            for (; i < len ; i++) {
   1.151 +                c = read();
   1.152 +                if (c == -1) {
   1.153 +                    break;
   1.154 +                }
   1.155 +                if (b != null) {
   1.156 +                    b[off + i] = (byte)c;
   1.157 +                }
   1.158 +            }
   1.159 +        } catch (IOException ee) {
   1.160 +        }
   1.161 +        return i;
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * Skips over and discards <code>n</code> bytes of data from this
   1.166 +     * input stream. The <code>skip</code> method may, for a variety of
   1.167 +     * reasons, end up skipping over some smaller number of bytes,
   1.168 +     * possibly <code>0</code>. The actual number of bytes skipped is
   1.169 +     * returned.  If <code>n</code> is negative, no bytes are skipped.
   1.170 +     * <p>
   1.171 +     * The <code>skip</code> method of <code>LineNumberInputStream</code> creates
   1.172 +     * a byte array and then repeatedly reads into it until
   1.173 +     * <code>n</code> bytes have been read or the end of the stream has
   1.174 +     * been reached.
   1.175 +     *
   1.176 +     * @param      n   the number of bytes to be skipped.
   1.177 +     * @return     the actual number of bytes skipped.
   1.178 +     * @exception  IOException  if an I/O error occurs.
   1.179 +     * @see        java.io.FilterInputStream#in
   1.180 +     */
   1.181 +    public long skip(long n) throws IOException {
   1.182 +        int chunk = 2048;
   1.183 +        long remaining = n;
   1.184 +        byte data[];
   1.185 +        int nr;
   1.186 +
   1.187 +        if (n <= 0) {
   1.188 +            return 0;
   1.189 +        }
   1.190 +
   1.191 +        data = new byte[chunk];
   1.192 +        while (remaining > 0) {
   1.193 +            nr = read(data, 0, (int) Math.min(chunk, remaining));
   1.194 +            if (nr < 0) {
   1.195 +                break;
   1.196 +            }
   1.197 +            remaining -= nr;
   1.198 +        }
   1.199 +
   1.200 +        return n - remaining;
   1.201 +    }
   1.202 +
   1.203 +    /**
   1.204 +     * Sets the line number to the specified argument.
   1.205 +     *
   1.206 +     * @param      lineNumber   the new line number.
   1.207 +     * @see #getLineNumber
   1.208 +     */
   1.209 +    public void setLineNumber(int lineNumber) {
   1.210 +        this.lineNumber = lineNumber;
   1.211 +    }
   1.212 +
   1.213 +    /**
   1.214 +     * Returns the current line number.
   1.215 +     *
   1.216 +     * @return     the current line number.
   1.217 +     * @see #setLineNumber
   1.218 +     */
   1.219 +    public int getLineNumber() {
   1.220 +        return lineNumber;
   1.221 +    }
   1.222 +
   1.223 +
   1.224 +    /**
   1.225 +     * Returns the number of bytes that can be read from this input
   1.226 +     * stream without blocking.
   1.227 +     * <p>
   1.228 +     * Note that if the underlying input stream is able to supply
   1.229 +     * <i>k</i> input characters without blocking, the
   1.230 +     * <code>LineNumberInputStream</code> can guarantee only to provide
   1.231 +     * <i>k</i>/2 characters without blocking, because the
   1.232 +     * <i>k</i> characters from the underlying input stream might
   1.233 +     * consist of <i>k</i>/2 pairs of <code>'&#92;r'</code> and
   1.234 +     * <code>'&#92;n'</code>, which are converted to just
   1.235 +     * <i>k</i>/2 <code>'&#92;n'</code> characters.
   1.236 +     *
   1.237 +     * @return     the number of bytes that can be read from this input stream
   1.238 +     *             without blocking.
   1.239 +     * @exception  IOException  if an I/O error occurs.
   1.240 +     * @see        java.io.FilterInputStream#in
   1.241 +     */
   1.242 +    public int available() throws IOException {
   1.243 +        return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1;
   1.244 +    }
   1.245 +
   1.246 +    /**
   1.247 +     * Marks the current position in this input stream. A subsequent
   1.248 +     * call to the <code>reset</code> method repositions this stream at
   1.249 +     * the last marked position so that subsequent reads re-read the same bytes.
   1.250 +     * <p>
   1.251 +     * The <code>mark</code> method of
   1.252 +     * <code>LineNumberInputStream</code> remembers the current line
   1.253 +     * number in a private variable, and then calls the <code>mark</code>
   1.254 +     * method of the underlying input stream.
   1.255 +     *
   1.256 +     * @param   readlimit   the maximum limit of bytes that can be read before
   1.257 +     *                      the mark position becomes invalid.
   1.258 +     * @see     java.io.FilterInputStream#in
   1.259 +     * @see     java.io.LineNumberInputStream#reset()
   1.260 +     */
   1.261 +    public void mark(int readlimit) {
   1.262 +        markLineNumber = lineNumber;
   1.263 +        markPushBack   = pushBack;
   1.264 +        in.mark(readlimit);
   1.265 +    }
   1.266 +
   1.267 +    /**
   1.268 +     * Repositions this stream to the position at the time the
   1.269 +     * <code>mark</code> method was last called on this input stream.
   1.270 +     * <p>
   1.271 +     * The <code>reset</code> method of
   1.272 +     * <code>LineNumberInputStream</code> resets the line number to be
   1.273 +     * the line number at the time the <code>mark</code> method was
   1.274 +     * called, and then calls the <code>reset</code> method of the
   1.275 +     * underlying input stream.
   1.276 +     * <p>
   1.277 +     * Stream marks are intended to be used in
   1.278 +     * situations where you need to read ahead a little to see what's in
   1.279 +     * the stream. Often this is most easily done by invoking some
   1.280 +     * general parser. If the stream is of the type handled by the
   1.281 +     * parser, it just chugs along happily. If the stream is not of
   1.282 +     * that type, the parser should toss an exception when it fails,
   1.283 +     * which, if it happens within readlimit bytes, allows the outer
   1.284 +     * code to reset the stream and try another parser.
   1.285 +     *
   1.286 +     * @exception  IOException  if an I/O error occurs.
   1.287 +     * @see        java.io.FilterInputStream#in
   1.288 +     * @see        java.io.LineNumberInputStream#mark(int)
   1.289 +     */
   1.290 +    public void reset() throws IOException {
   1.291 +        lineNumber = markLineNumber;
   1.292 +        pushBack   = markPushBack;
   1.293 +        in.reset();
   1.294 +    }
   1.295 +}