jtulach@147: /*
jtulach@147: * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@147: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@147: *
jtulach@147: * This code is free software; you can redistribute it and/or modify it
jtulach@147: * under the terms of the GNU General Public License version 2 only, as
jtulach@147: * published by the Free Software Foundation. Oracle designates this
jtulach@147: * particular file as subject to the "Classpath" exception as provided
jtulach@147: * by Oracle in the LICENSE file that accompanied this code.
jtulach@147: *
jtulach@147: * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@147: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@147: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jtulach@147: * version 2 for more details (a copy is included in the LICENSE file that
jtulach@147: * accompanied this code).
jtulach@147: *
jtulach@147: * You should have received a copy of the GNU General Public License version
jtulach@147: * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@147: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@147: *
jtulach@147: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@147: * or visit www.oracle.com if you need additional information or have any
jtulach@147: * questions.
jtulach@147: */
jtulach@147:
jtulach@147: package java.io;
jtulach@147:
jaroslav@560: import org.apidesign.bck2brwsr.emul.lang.System;
jaroslav@560:
jtulach@147: /**
jtulach@147: * A PushbackInputStream
adds
jtulach@147: * functionality to another input stream, namely
jtulach@147: * the ability to "push back" or "unread"
jtulach@147: * one byte. This is useful in situations where
jtulach@147: * it is convenient for a fragment of code
jtulach@147: * to read an indefinite number of data bytes
jtulach@147: * that are delimited by a particular byte
jtulach@147: * value; after reading the terminating byte,
jtulach@147: * the code fragment can "unread" it, so that
jtulach@147: * the next read operation on the input stream
jtulach@147: * will reread the byte that was pushed back.
jtulach@147: * For example, bytes representing the characters
jtulach@147: * constituting an identifier might be terminated
jtulach@147: * by a byte representing an operator character;
jtulach@147: * a method whose job is to read just an identifier
jtulach@147: * can read until it sees the operator and
jtulach@147: * then push the operator back to be re-read.
jtulach@147: *
jtulach@147: * @author David Connelly
jtulach@147: * @author Jonathan Payne
jtulach@147: * @since JDK1.0
jtulach@147: */
jtulach@147: public
jtulach@147: class PushbackInputStream extends FilterInputStream {
jtulach@147: /**
jtulach@147: * The pushback buffer.
jtulach@147: * @since JDK1.1
jtulach@147: */
jtulach@147: protected byte[] buf;
jtulach@147:
jtulach@147: /**
jtulach@147: * The position within the pushback buffer from which the next byte will
jtulach@147: * be read. When the buffer is empty, pos
is equal to
jtulach@147: * buf.length
; when the buffer is full, pos
is
jtulach@147: * equal to zero.
jtulach@147: *
jtulach@147: * @since JDK1.1
jtulach@147: */
jtulach@147: protected int pos;
jtulach@147:
jtulach@147: /**
jtulach@147: * Check to make sure that this stream has not been closed
jtulach@147: */
jtulach@147: private void ensureOpen() throws IOException {
jtulach@147: if (in == null)
jtulach@147: throw new IOException("Stream closed");
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Creates a PushbackInputStream
jtulach@147: * with a pushback buffer of the specified size
,
jtulach@147: * and saves its argument, the input stream
jtulach@147: * in
, for later use. Initially,
jtulach@147: * there is no pushed-back byte (the field
jtulach@147: * pushBack
is initialized to
jtulach@147: * -1
).
jtulach@147: *
jtulach@147: * @param in the input stream from which bytes will be read.
jtulach@147: * @param size the size of the pushback buffer.
jtulach@147: * @exception IllegalArgumentException if size is <= 0
jtulach@147: * @since JDK1.1
jtulach@147: */
jtulach@147: public PushbackInputStream(InputStream in, int size) {
jtulach@147: super(in);
jtulach@147: if (size <= 0) {
jtulach@147: throw new IllegalArgumentException("size <= 0");
jtulach@147: }
jtulach@147: this.buf = new byte[size];
jtulach@147: this.pos = size;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Creates a PushbackInputStream
jtulach@147: * and saves its argument, the input stream
jtulach@147: * in
, for later use. Initially,
jtulach@147: * there is no pushed-back byte (the field
jtulach@147: * pushBack
is initialized to
jtulach@147: * -1
).
jtulach@147: *
jtulach@147: * @param in the input stream from which bytes will be read.
jtulach@147: */
jtulach@147: public PushbackInputStream(InputStream in) {
jtulach@147: this(in, 1);
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Reads the next byte of data from this input stream. The value
jtulach@147: * byte is returned as an int
in the range
jtulach@147: * 0
to 255
. If no byte is available
jtulach@147: * because the end of the stream has been reached, the value
jtulach@147: * -1
is returned. This method blocks until input data
jtulach@147: * is available, the end of the stream is detected, or an exception
jtulach@147: * is thrown.
jtulach@147: *
jtulach@147: *
This method returns the most recently pushed-back byte, if there is
jtulach@147: * one, and otherwise calls the read
method of its underlying
jtulach@147: * input stream and returns whatever value that method returns.
jtulach@147: *
jtulach@147: * @return the next byte of data, or -1
if the end of the
jtulach@147: * stream has been reached.
jtulach@147: * @exception IOException if this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method,
jtulach@147: * or an I/O error occurs.
jtulach@147: * @see java.io.InputStream#read()
jtulach@147: */
jtulach@147: public int read() throws IOException {
jtulach@147: ensureOpen();
jtulach@147: if (pos < buf.length) {
jtulach@147: return buf[pos++] & 0xff;
jtulach@147: }
jtulach@147: return super.read();
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Reads up to len
bytes of data from this input stream into
jtulach@147: * an array of bytes. This method first reads any pushed-back bytes; after
jtulach@147: * that, if fewer than len
bytes have been read then it
jtulach@147: * reads from the underlying input stream. If len
is not zero, the method
jtulach@147: * blocks until at least 1 byte of input is available; otherwise, no
jtulach@147: * bytes are read and 0
is returned.
jtulach@147: *
jtulach@147: * @param b the buffer into which the data is read.
jtulach@147: * @param off the start offset in the destination array b
jtulach@147: * @param len the maximum number of bytes read.
jtulach@147: * @return the total number of bytes read into the buffer, or
jtulach@147: * -1
if there is no more data because the end of
jtulach@147: * the stream has been reached.
jtulach@147: * @exception NullPointerException If b
is null
.
jtulach@147: * @exception IndexOutOfBoundsException If off
is negative,
jtulach@147: * len
is negative, or len
is greater than
jtulach@147: * b.length - off
jtulach@147: * @exception IOException if this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method,
jtulach@147: * or an I/O error occurs.
jtulach@147: * @see java.io.InputStream#read(byte[], int, int)
jtulach@147: */
jtulach@147: public int read(byte[] b, int off, int len) throws IOException {
jtulach@147: ensureOpen();
jtulach@147: if (b == null) {
jtulach@147: throw new NullPointerException();
jtulach@147: } else if (off < 0 || len < 0 || len > b.length - off) {
jtulach@147: throw new IndexOutOfBoundsException();
jtulach@147: } else if (len == 0) {
jtulach@147: return 0;
jtulach@147: }
jtulach@147:
jtulach@147: int avail = buf.length - pos;
jtulach@147: if (avail > 0) {
jtulach@147: if (len < avail) {
jtulach@147: avail = len;
jtulach@147: }
jaroslav@560: System.arraycopy(buf, pos, b, off, avail);
jtulach@147: pos += avail;
jtulach@147: off += avail;
jtulach@147: len -= avail;
jtulach@147: }
jtulach@147: if (len > 0) {
jtulach@147: len = super.read(b, off, len);
jtulach@147: if (len == -1) {
jtulach@147: return avail == 0 ? -1 : avail;
jtulach@147: }
jtulach@147: return avail + len;
jtulach@147: }
jtulach@147: return avail;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Pushes back a byte by copying it to the front of the pushback buffer.
jtulach@147: * After this method returns, the next byte to be read will have the value
jtulach@147: * (byte)b
.
jtulach@147: *
jtulach@147: * @param b the int
value whose low-order
jtulach@147: * byte is to be pushed back.
jtulach@147: * @exception IOException If there is not enough room in the pushback
jtulach@147: * buffer for the byte, or this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method.
jtulach@147: */
jtulach@147: public void unread(int b) throws IOException {
jtulach@147: ensureOpen();
jtulach@147: if (pos == 0) {
jtulach@147: throw new IOException("Push back buffer is full");
jtulach@147: }
jtulach@147: buf[--pos] = (byte)b;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Pushes back a portion of an array of bytes by copying it to the front
jtulach@147: * of the pushback buffer. After this method returns, the next byte to be
jtulach@147: * read will have the value b[off]
, the byte after that will
jtulach@147: * have the value b[off+1]
, and so forth.
jtulach@147: *
jtulach@147: * @param b the byte array to push back.
jtulach@147: * @param off the start offset of the data.
jtulach@147: * @param len the number of bytes to push back.
jtulach@147: * @exception IOException If there is not enough room in the pushback
jtulach@147: * buffer for the specified number of bytes,
jtulach@147: * or this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method.
jtulach@147: * @since JDK1.1
jtulach@147: */
jtulach@147: public void unread(byte[] b, int off, int len) throws IOException {
jtulach@147: ensureOpen();
jtulach@147: if (len > pos) {
jtulach@147: throw new IOException("Push back buffer is full");
jtulach@147: }
jtulach@147: pos -= len;
jaroslav@560: System.arraycopy(b, off, buf, pos, len);
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Pushes back an array of bytes by copying it to the front of the
jtulach@147: * pushback buffer. After this method returns, the next byte to be read
jtulach@147: * will have the value b[0]
, the byte after that will have the
jtulach@147: * value b[1]
, and so forth.
jtulach@147: *
jtulach@147: * @param b the byte array to push back
jtulach@147: * @exception IOException If there is not enough room in the pushback
jtulach@147: * buffer for the specified number of bytes,
jtulach@147: * or this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method.
jtulach@147: * @since JDK1.1
jtulach@147: */
jtulach@147: public void unread(byte[] b) throws IOException {
jtulach@147: unread(b, 0, b.length);
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Returns an estimate of the number of bytes that can be read (or
jtulach@147: * skipped over) from this input stream without blocking by the next
jtulach@147: * invocation of a method for this input stream. The next invocation might be
jtulach@147: * the same thread or another thread. A single read or skip of this
jtulach@147: * many bytes will not block, but may read or skip fewer bytes.
jtulach@147: *
jtulach@147: *
The method returns the sum of the number of bytes that have been
jtulach@147: * pushed back and the value returned by {@link
jtulach@147: * java.io.FilterInputStream#available available}.
jtulach@147: *
jtulach@147: * @return the number of bytes that can be read (or skipped over) from
jtulach@147: * the input stream without blocking.
jtulach@147: * @exception IOException if this input stream has been closed by
jtulach@147: * invoking its {@link #close()} method,
jtulach@147: * or an I/O error occurs.
jtulach@147: * @see java.io.FilterInputStream#in
jtulach@147: * @see java.io.InputStream#available()
jtulach@147: */
jtulach@147: public int available() throws IOException {
jtulach@147: ensureOpen();
jtulach@147: int n = buf.length - pos;
jtulach@147: int avail = super.available();
jtulach@147: return n > (Integer.MAX_VALUE - avail)
jtulach@147: ? Integer.MAX_VALUE
jtulach@147: : n + avail;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Skips over and discards n
bytes of data from this
jtulach@147: * input stream. The skip
method may, for a variety of
jtulach@147: * reasons, end up skipping over some smaller number of bytes,
jtulach@147: * possibly zero. If n
is negative, no bytes are skipped.
jtulach@147: *
jtulach@147: *
The skip
method of PushbackInputStream
jtulach@147: * first skips over the bytes in the pushback buffer, if any. It then
jtulach@147: * calls the skip
method of the underlying input stream if
jtulach@147: * more bytes need to be skipped. The actual number of bytes skipped
jtulach@147: * is returned.
jtulach@147: *
jtulach@147: * @param n {@inheritDoc}
jtulach@147: * @return {@inheritDoc}
jtulach@147: * @exception IOException if the stream does not support seek,
jtulach@147: * or the stream has been closed by
jtulach@147: * invoking its {@link #close()} method,
jtulach@147: * or an I/O error occurs.
jtulach@147: * @see java.io.FilterInputStream#in
jtulach@147: * @see java.io.InputStream#skip(long n)
jtulach@147: * @since 1.2
jtulach@147: */
jtulach@147: public long skip(long n) throws IOException {
jtulach@147: ensureOpen();
jtulach@147: if (n <= 0) {
jtulach@147: return 0;
jtulach@147: }
jtulach@147:
jtulach@147: long pskip = buf.length - pos;
jtulach@147: if (pskip > 0) {
jtulach@147: if (n < pskip) {
jtulach@147: pskip = n;
jtulach@147: }
jtulach@147: pos += pskip;
jtulach@147: n -= pskip;
jtulach@147: }
jtulach@147: if (n > 0) {
jtulach@147: pskip += super.skip(n);
jtulach@147: }
jtulach@147: return pskip;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Tests if this input stream supports the mark
and
jtulach@147: * reset
methods, which it does not.
jtulach@147: *
jtulach@147: * @return false
, since this class does not support the
jtulach@147: * mark
and reset
methods.
jtulach@147: * @see java.io.InputStream#mark(int)
jtulach@147: * @see java.io.InputStream#reset()
jtulach@147: */
jtulach@147: public boolean markSupported() {
jtulach@147: return false;
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Marks the current position in this input stream.
jtulach@147: *
jtulach@147: *
The mark
method of PushbackInputStream
jtulach@147: * does nothing.
jtulach@147: *
jtulach@147: * @param readlimit the maximum limit of bytes that can be read before
jtulach@147: * the mark position becomes invalid.
jtulach@147: * @see java.io.InputStream#reset()
jtulach@147: */
jtulach@147: public synchronized void mark(int readlimit) {
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Repositions this stream to the position at the time the
jtulach@147: * mark
method was last called on this input stream.
jtulach@147: *
jtulach@147: *
The method reset
for class
jtulach@147: * PushbackInputStream
does nothing except throw an
jtulach@147: * IOException
.
jtulach@147: *
jtulach@147: * @exception IOException if this method is invoked.
jtulach@147: * @see java.io.InputStream#mark(int)
jtulach@147: * @see java.io.IOException
jtulach@147: */
jtulach@147: public synchronized void reset() throws IOException {
jtulach@147: throw new IOException("mark/reset not supported");
jtulach@147: }
jtulach@147:
jtulach@147: /**
jtulach@147: * Closes this input stream and releases any system resources
jtulach@147: * associated with the stream.
jtulach@147: * Once the stream has been closed, further read(), unread(),
jtulach@147: * available(), reset(), or skip() invocations will throw an IOException.
jtulach@147: * Closing a previously closed stream has no effect.
jtulach@147: *
jtulach@147: * @exception IOException if an I/O error occurs.
jtulach@147: */
jtulach@147: public synchronized void close() throws IOException {
jtulach@147: if (in == null)
jtulach@147: return;
jtulach@147: in.close();
jtulach@147: in = null;
jtulach@147: buf = null;
jtulach@147: }
jtulach@147: }