1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/mini/src/main/java/java/io/PushbackInputStream.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,385 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2011, 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 +import org.apidesign.bck2brwsr.emul.lang.System;
1.32 +
1.33 +/**
1.34 + * A <code>PushbackInputStream</code> adds
1.35 + * functionality to another input stream, namely
1.36 + * the ability to "push back" or "unread"
1.37 + * one byte. This is useful in situations where
1.38 + * it is convenient for a fragment of code
1.39 + * to read an indefinite number of data bytes
1.40 + * that are delimited by a particular byte
1.41 + * value; after reading the terminating byte,
1.42 + * the code fragment can "unread" it, so that
1.43 + * the next read operation on the input stream
1.44 + * will reread the byte that was pushed back.
1.45 + * For example, bytes representing the characters
1.46 + * constituting an identifier might be terminated
1.47 + * by a byte representing an operator character;
1.48 + * a method whose job is to read just an identifier
1.49 + * can read until it sees the operator and
1.50 + * then push the operator back to be re-read.
1.51 + *
1.52 + * @author David Connelly
1.53 + * @author Jonathan Payne
1.54 + * @since JDK1.0
1.55 + */
1.56 +public
1.57 +class PushbackInputStream extends FilterInputStream {
1.58 + /**
1.59 + * The pushback buffer.
1.60 + * @since JDK1.1
1.61 + */
1.62 + protected byte[] buf;
1.63 +
1.64 + /**
1.65 + * The position within the pushback buffer from which the next byte will
1.66 + * be read. When the buffer is empty, <code>pos</code> is equal to
1.67 + * <code>buf.length</code>; when the buffer is full, <code>pos</code> is
1.68 + * equal to zero.
1.69 + *
1.70 + * @since JDK1.1
1.71 + */
1.72 + protected int pos;
1.73 +
1.74 + /**
1.75 + * Check to make sure that this stream has not been closed
1.76 + */
1.77 + private void ensureOpen() throws IOException {
1.78 + if (in == null)
1.79 + throw new IOException("Stream closed");
1.80 + }
1.81 +
1.82 + /**
1.83 + * Creates a <code>PushbackInputStream</code>
1.84 + * with a pushback buffer of the specified <code>size</code>,
1.85 + * and saves its argument, the input stream
1.86 + * <code>in</code>, for later use. Initially,
1.87 + * there is no pushed-back byte (the field
1.88 + * <code>pushBack</code> is initialized to
1.89 + * <code>-1</code>).
1.90 + *
1.91 + * @param in the input stream from which bytes will be read.
1.92 + * @param size the size of the pushback buffer.
1.93 + * @exception IllegalArgumentException if size is <= 0
1.94 + * @since JDK1.1
1.95 + */
1.96 + public PushbackInputStream(InputStream in, int size) {
1.97 + super(in);
1.98 + if (size <= 0) {
1.99 + throw new IllegalArgumentException("size <= 0");
1.100 + }
1.101 + this.buf = new byte[size];
1.102 + this.pos = size;
1.103 + }
1.104 +
1.105 + /**
1.106 + * Creates a <code>PushbackInputStream</code>
1.107 + * and saves its argument, the input stream
1.108 + * <code>in</code>, for later use. Initially,
1.109 + * there is no pushed-back byte (the field
1.110 + * <code>pushBack</code> is initialized to
1.111 + * <code>-1</code>).
1.112 + *
1.113 + * @param in the input stream from which bytes will be read.
1.114 + */
1.115 + public PushbackInputStream(InputStream in) {
1.116 + this(in, 1);
1.117 + }
1.118 +
1.119 + /**
1.120 + * Reads the next byte of data from this input stream. The value
1.121 + * byte is returned as an <code>int</code> in the range
1.122 + * <code>0</code> to <code>255</code>. If no byte is available
1.123 + * because the end of the stream has been reached, the value
1.124 + * <code>-1</code> is returned. This method blocks until input data
1.125 + * is available, the end of the stream is detected, or an exception
1.126 + * is thrown.
1.127 + *
1.128 + * <p> This method returns the most recently pushed-back byte, if there is
1.129 + * one, and otherwise calls the <code>read</code> method of its underlying
1.130 + * input stream and returns whatever value that method returns.
1.131 + *
1.132 + * @return the next byte of data, or <code>-1</code> if the end of the
1.133 + * stream has been reached.
1.134 + * @exception IOException if this input stream has been closed by
1.135 + * invoking its {@link #close()} method,
1.136 + * or an I/O error occurs.
1.137 + * @see java.io.InputStream#read()
1.138 + */
1.139 + public int read() throws IOException {
1.140 + ensureOpen();
1.141 + if (pos < buf.length) {
1.142 + return buf[pos++] & 0xff;
1.143 + }
1.144 + return super.read();
1.145 + }
1.146 +
1.147 + /**
1.148 + * Reads up to <code>len</code> bytes of data from this input stream into
1.149 + * an array of bytes. This method first reads any pushed-back bytes; after
1.150 + * that, if fewer than <code>len</code> bytes have been read then it
1.151 + * reads from the underlying input stream. If <code>len</code> is not zero, the method
1.152 + * blocks until at least 1 byte of input is available; otherwise, no
1.153 + * bytes are read and <code>0</code> is returned.
1.154 + *
1.155 + * @param b the buffer into which the data is read.
1.156 + * @param off the start offset in the destination array <code>b</code>
1.157 + * @param len the maximum number of bytes read.
1.158 + * @return the total number of bytes read into the buffer, or
1.159 + * <code>-1</code> if there is no more data because the end of
1.160 + * the stream has been reached.
1.161 + * @exception NullPointerException If <code>b</code> is <code>null</code>.
1.162 + * @exception IndexOutOfBoundsException If <code>off</code> is negative,
1.163 + * <code>len</code> is negative, or <code>len</code> is greater than
1.164 + * <code>b.length - off</code>
1.165 + * @exception IOException if this input stream has been closed by
1.166 + * invoking its {@link #close()} method,
1.167 + * or an I/O error occurs.
1.168 + * @see java.io.InputStream#read(byte[], int, int)
1.169 + */
1.170 + public int read(byte[] b, int off, int len) throws IOException {
1.171 + ensureOpen();
1.172 + if (b == null) {
1.173 + throw new NullPointerException();
1.174 + } else if (off < 0 || len < 0 || len > b.length - off) {
1.175 + throw new IndexOutOfBoundsException();
1.176 + } else if (len == 0) {
1.177 + return 0;
1.178 + }
1.179 +
1.180 + int avail = buf.length - pos;
1.181 + if (avail > 0) {
1.182 + if (len < avail) {
1.183 + avail = len;
1.184 + }
1.185 + System.arraycopy(buf, pos, b, off, avail);
1.186 + pos += avail;
1.187 + off += avail;
1.188 + len -= avail;
1.189 + }
1.190 + if (len > 0) {
1.191 + len = super.read(b, off, len);
1.192 + if (len == -1) {
1.193 + return avail == 0 ? -1 : avail;
1.194 + }
1.195 + return avail + len;
1.196 + }
1.197 + return avail;
1.198 + }
1.199 +
1.200 + /**
1.201 + * Pushes back a byte by copying it to the front of the pushback buffer.
1.202 + * After this method returns, the next byte to be read will have the value
1.203 + * <code>(byte)b</code>.
1.204 + *
1.205 + * @param b the <code>int</code> value whose low-order
1.206 + * byte is to be pushed back.
1.207 + * @exception IOException If there is not enough room in the pushback
1.208 + * buffer for the byte, or this input stream has been closed by
1.209 + * invoking its {@link #close()} method.
1.210 + */
1.211 + public void unread(int b) throws IOException {
1.212 + ensureOpen();
1.213 + if (pos == 0) {
1.214 + throw new IOException("Push back buffer is full");
1.215 + }
1.216 + buf[--pos] = (byte)b;
1.217 + }
1.218 +
1.219 + /**
1.220 + * Pushes back a portion of an array of bytes by copying it to the front
1.221 + * of the pushback buffer. After this method returns, the next byte to be
1.222 + * read will have the value <code>b[off]</code>, the byte after that will
1.223 + * have the value <code>b[off+1]</code>, and so forth.
1.224 + *
1.225 + * @param b the byte array to push back.
1.226 + * @param off the start offset of the data.
1.227 + * @param len the number of bytes to push back.
1.228 + * @exception IOException If there is not enough room in the pushback
1.229 + * buffer for the specified number of bytes,
1.230 + * or this input stream has been closed by
1.231 + * invoking its {@link #close()} method.
1.232 + * @since JDK1.1
1.233 + */
1.234 + public void unread(byte[] b, int off, int len) throws IOException {
1.235 + ensureOpen();
1.236 + if (len > pos) {
1.237 + throw new IOException("Push back buffer is full");
1.238 + }
1.239 + pos -= len;
1.240 + System.arraycopy(b, off, buf, pos, len);
1.241 + }
1.242 +
1.243 + /**
1.244 + * Pushes back an array of bytes by copying it to the front of the
1.245 + * pushback buffer. After this method returns, the next byte to be read
1.246 + * will have the value <code>b[0]</code>, the byte after that will have the
1.247 + * value <code>b[1]</code>, and so forth.
1.248 + *
1.249 + * @param b the byte array to push back
1.250 + * @exception IOException If there is not enough room in the pushback
1.251 + * buffer for the specified number of bytes,
1.252 + * or this input stream has been closed by
1.253 + * invoking its {@link #close()} method.
1.254 + * @since JDK1.1
1.255 + */
1.256 + public void unread(byte[] b) throws IOException {
1.257 + unread(b, 0, b.length);
1.258 + }
1.259 +
1.260 + /**
1.261 + * Returns an estimate of the number of bytes that can be read (or
1.262 + * skipped over) from this input stream without blocking by the next
1.263 + * invocation of a method for this input stream. The next invocation might be
1.264 + * the same thread or another thread. A single read or skip of this
1.265 + * many bytes will not block, but may read or skip fewer bytes.
1.266 + *
1.267 + * <p> The method returns the sum of the number of bytes that have been
1.268 + * pushed back and the value returned by {@link
1.269 + * java.io.FilterInputStream#available available}.
1.270 + *
1.271 + * @return the number of bytes that can be read (or skipped over) from
1.272 + * the input stream without blocking.
1.273 + * @exception IOException if this input stream has been closed by
1.274 + * invoking its {@link #close()} method,
1.275 + * or an I/O error occurs.
1.276 + * @see java.io.FilterInputStream#in
1.277 + * @see java.io.InputStream#available()
1.278 + */
1.279 + public int available() throws IOException {
1.280 + ensureOpen();
1.281 + int n = buf.length - pos;
1.282 + int avail = super.available();
1.283 + return n > (Integer.MAX_VALUE - avail)
1.284 + ? Integer.MAX_VALUE
1.285 + : n + avail;
1.286 + }
1.287 +
1.288 + /**
1.289 + * Skips over and discards <code>n</code> bytes of data from this
1.290 + * input stream. The <code>skip</code> method may, for a variety of
1.291 + * reasons, end up skipping over some smaller number of bytes,
1.292 + * possibly zero. If <code>n</code> is negative, no bytes are skipped.
1.293 + *
1.294 + * <p> The <code>skip</code> method of <code>PushbackInputStream</code>
1.295 + * first skips over the bytes in the pushback buffer, if any. It then
1.296 + * calls the <code>skip</code> method of the underlying input stream if
1.297 + * more bytes need to be skipped. The actual number of bytes skipped
1.298 + * is returned.
1.299 + *
1.300 + * @param n {@inheritDoc}
1.301 + * @return {@inheritDoc}
1.302 + * @exception IOException if the stream does not support seek,
1.303 + * or the stream has been closed by
1.304 + * invoking its {@link #close()} method,
1.305 + * or an I/O error occurs.
1.306 + * @see java.io.FilterInputStream#in
1.307 + * @see java.io.InputStream#skip(long n)
1.308 + * @since 1.2
1.309 + */
1.310 + public long skip(long n) throws IOException {
1.311 + ensureOpen();
1.312 + if (n <= 0) {
1.313 + return 0;
1.314 + }
1.315 +
1.316 + long pskip = buf.length - pos;
1.317 + if (pskip > 0) {
1.318 + if (n < pskip) {
1.319 + pskip = n;
1.320 + }
1.321 + pos += pskip;
1.322 + n -= pskip;
1.323 + }
1.324 + if (n > 0) {
1.325 + pskip += super.skip(n);
1.326 + }
1.327 + return pskip;
1.328 + }
1.329 +
1.330 + /**
1.331 + * Tests if this input stream supports the <code>mark</code> and
1.332 + * <code>reset</code> methods, which it does not.
1.333 + *
1.334 + * @return <code>false</code>, since this class does not support the
1.335 + * <code>mark</code> and <code>reset</code> methods.
1.336 + * @see java.io.InputStream#mark(int)
1.337 + * @see java.io.InputStream#reset()
1.338 + */
1.339 + public boolean markSupported() {
1.340 + return false;
1.341 + }
1.342 +
1.343 + /**
1.344 + * Marks the current position in this input stream.
1.345 + *
1.346 + * <p> The <code>mark</code> method of <code>PushbackInputStream</code>
1.347 + * does nothing.
1.348 + *
1.349 + * @param readlimit the maximum limit of bytes that can be read before
1.350 + * the mark position becomes invalid.
1.351 + * @see java.io.InputStream#reset()
1.352 + */
1.353 + public synchronized void mark(int readlimit) {
1.354 + }
1.355 +
1.356 + /**
1.357 + * Repositions this stream to the position at the time the
1.358 + * <code>mark</code> method was last called on this input stream.
1.359 + *
1.360 + * <p> The method <code>reset</code> for class
1.361 + * <code>PushbackInputStream</code> does nothing except throw an
1.362 + * <code>IOException</code>.
1.363 + *
1.364 + * @exception IOException if this method is invoked.
1.365 + * @see java.io.InputStream#mark(int)
1.366 + * @see java.io.IOException
1.367 + */
1.368 + public synchronized void reset() throws IOException {
1.369 + throw new IOException("mark/reset not supported");
1.370 + }
1.371 +
1.372 + /**
1.373 + * Closes this input stream and releases any system resources
1.374 + * associated with the stream.
1.375 + * Once the stream has been closed, further read(), unread(),
1.376 + * available(), reset(), or skip() invocations will throw an IOException.
1.377 + * Closing a previously closed stream has no effect.
1.378 + *
1.379 + * @exception IOException if an I/O error occurs.
1.380 + */
1.381 + public synchronized void close() throws IOException {
1.382 + if (in == null)
1.383 + return;
1.384 + in.close();
1.385 + in = null;
1.386 + buf = null;
1.387 + }
1.388 +}