jaroslav@163: /*
jaroslav@163: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@163: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@163: *
jaroslav@163: * This code is free software; you can redistribute it and/or modify it
jaroslav@163: * under the terms of the GNU General Public License version 2 only, as
jaroslav@163: * published by the Free Software Foundation. Oracle designates this
jaroslav@163: * particular file as subject to the "Classpath" exception as provided
jaroslav@163: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@163: *
jaroslav@163: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@163: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@163: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@163: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@163: * accompanied this code).
jaroslav@163: *
jaroslav@163: * You should have received a copy of the GNU General Public License version
jaroslav@163: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@163: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@163: *
jaroslav@163: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@163: * or visit www.oracle.com if you need additional information or have any
jaroslav@163: * questions.
jaroslav@163: */
jaroslav@163:
jaroslav@163: package java.io;
jaroslav@163:
jaroslav@560: import org.apidesign.bck2brwsr.emul.lang.System;
jaroslav@560:
jaroslav@163: /**
jaroslav@163: * A ByteArrayInputStream
contains
jaroslav@163: * an internal buffer that contains bytes that
jaroslav@163: * may be read from the stream. An internal
jaroslav@163: * counter keeps track of the next byte to
jaroslav@163: * be supplied by the read
method.
jaroslav@163: *
jaroslav@163: * Closing a ByteArrayInputStream has no effect. The methods in
jaroslav@163: * this class can be called after the stream has been closed without
jaroslav@163: * generating an IOException.
jaroslav@163: *
jaroslav@163: * @author Arthur van Hoff
jaroslav@163: * @see java.io.StringBufferInputStream
jaroslav@163: * @since JDK1.0
jaroslav@163: */
jaroslav@163: public
jaroslav@163: class ByteArrayInputStream extends InputStream {
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * An array of bytes that was provided
jaroslav@163: * by the creator of the stream. Elements buf[0]
jaroslav@163: * through buf[count-1]
are the
jaroslav@163: * only bytes that can ever be read from the
jaroslav@163: * stream; element buf[pos]
is
jaroslav@163: * the next byte to be read.
jaroslav@163: */
jaroslav@163: protected byte buf[];
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * The index of the next character to read from the input stream buffer.
jaroslav@163: * This value should always be nonnegative
jaroslav@163: * and not larger than the value of count
.
jaroslav@163: * The next byte to be read from the input stream buffer
jaroslav@163: * will be buf[pos]
.
jaroslav@163: */
jaroslav@163: protected int pos;
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * The currently marked position in the stream.
jaroslav@163: * ByteArrayInputStream objects are marked at position zero by
jaroslav@163: * default when constructed. They may be marked at another
jaroslav@163: * position within the buffer by the mark()
method.
jaroslav@163: * The current buffer position is set to this point by the
jaroslav@163: * reset()
method.
jaroslav@163: *
jaroslav@163: * If no mark has been set, then the value of mark is the offset
jaroslav@163: * passed to the constructor (or 0 if the offset was not supplied).
jaroslav@163: *
jaroslav@163: * @since JDK1.1
jaroslav@163: */
jaroslav@163: protected int mark = 0;
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * The index one greater than the last valid character in the input
jaroslav@163: * stream buffer.
jaroslav@163: * This value should always be nonnegative
jaroslav@163: * and not larger than the length of buf
.
jaroslav@163: * It is one greater than the position of
jaroslav@163: * the last byte within buf
that
jaroslav@163: * can ever be read from the input stream buffer.
jaroslav@163: */
jaroslav@163: protected int count;
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Creates a ByteArrayInputStream
jaroslav@163: * so that it uses buf
as its
jaroslav@163: * buffer array.
jaroslav@163: * The buffer array is not copied.
jaroslav@163: * The initial value of pos
jaroslav@163: * is 0
and the initial value
jaroslav@163: * of count
is the length of
jaroslav@163: * buf
.
jaroslav@163: *
jaroslav@163: * @param buf the input buffer.
jaroslav@163: */
jaroslav@163: public ByteArrayInputStream(byte buf[]) {
jaroslav@163: this.buf = buf;
jaroslav@163: this.pos = 0;
jaroslav@163: this.count = buf.length;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Creates ByteArrayInputStream
jaroslav@163: * that uses buf
as its
jaroslav@163: * buffer array. The initial value of pos
jaroslav@163: * is offset
and the initial value
jaroslav@163: * of count
is the minimum of offset+length
jaroslav@163: * and buf.length
.
jaroslav@163: * The buffer array is not copied. The buffer's mark is
jaroslav@163: * set to the specified offset.
jaroslav@163: *
jaroslav@163: * @param buf the input buffer.
jaroslav@163: * @param offset the offset in the buffer of the first byte to read.
jaroslav@163: * @param length the maximum number of bytes to read from the buffer.
jaroslav@163: */
jaroslav@163: public ByteArrayInputStream(byte buf[], int offset, int length) {
jaroslav@163: this.buf = buf;
jaroslav@163: this.pos = offset;
jaroslav@163: this.count = Math.min(offset + length, buf.length);
jaroslav@163: this.mark = offset;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Reads the next byte of data from this input stream. The value
jaroslav@163: * byte is returned as an int
in the range
jaroslav@163: * 0
to 255
. If no byte is available
jaroslav@163: * because the end of the stream has been reached, the value
jaroslav@163: * -1
is returned.
jaroslav@163: *
jaroslav@163: * This read
method
jaroslav@163: * cannot block.
jaroslav@163: *
jaroslav@163: * @return the next byte of data, or -1
if the end of the
jaroslav@163: * stream has been reached.
jaroslav@163: */
jaroslav@163: public synchronized int read() {
jaroslav@163: return (pos < count) ? (buf[pos++] & 0xff) : -1;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Reads up to len
bytes of data into an array of bytes
jaroslav@163: * from this input stream.
jaroslav@163: * If pos
equals count
,
jaroslav@163: * then -1
is returned to indicate
jaroslav@163: * end of file. Otherwise, the number k
jaroslav@163: * of bytes read is equal to the smaller of
jaroslav@163: * len
and count-pos
.
jaroslav@163: * If k
is positive, then bytes
jaroslav@163: * buf[pos]
through buf[pos+k-1]
jaroslav@163: * are copied into b[off]
through
jaroslav@163: * b[off+k-1]
in the manner performed
jaroslav@163: * by System.arraycopy
. The
jaroslav@163: * value k
is added into pos
jaroslav@163: * and k
is returned.
jaroslav@163: *
jaroslav@163: * This read
method cannot block.
jaroslav@163: *
jaroslav@163: * @param b the buffer into which the data is read.
jaroslav@163: * @param off the start offset in the destination array b
jaroslav@163: * @param len the maximum number of bytes read.
jaroslav@163: * @return the total number of bytes read into the buffer, or
jaroslav@163: * -1
if there is no more data because the end of
jaroslav@163: * the stream has been reached.
jaroslav@163: * @exception NullPointerException If b
is null
.
jaroslav@163: * @exception IndexOutOfBoundsException If off
is negative,
jaroslav@163: * len
is negative, or len
is greater than
jaroslav@163: * b.length - off
jaroslav@163: */
jaroslav@163: public synchronized int read(byte b[], int off, int len) {
jaroslav@163: if (b == null) {
jaroslav@163: throw new NullPointerException();
jaroslav@163: } else if (off < 0 || len < 0 || len > b.length - off) {
jaroslav@163: throw new IndexOutOfBoundsException();
jaroslav@163: }
jaroslav@163:
jaroslav@163: if (pos >= count) {
jaroslav@163: return -1;
jaroslav@163: }
jaroslav@163:
jaroslav@163: int avail = count - pos;
jaroslav@163: if (len > avail) {
jaroslav@163: len = avail;
jaroslav@163: }
jaroslav@163: if (len <= 0) {
jaroslav@163: return 0;
jaroslav@163: }
jaroslav@560: System.arraycopy(buf, pos, b, off, len);
jaroslav@163: pos += len;
jaroslav@163: return len;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Skips n
bytes of input from this input stream. Fewer
jaroslav@163: * bytes might be skipped if the end of the input stream is reached.
jaroslav@163: * The actual number k
jaroslav@163: * of bytes to be skipped is equal to the smaller
jaroslav@163: * of n
and count-pos
.
jaroslav@163: * The value k
is added into pos
jaroslav@163: * and k
is returned.
jaroslav@163: *
jaroslav@163: * @param n the number of bytes to be skipped.
jaroslav@163: * @return the actual number of bytes skipped.
jaroslav@163: */
jaroslav@163: public synchronized long skip(long n) {
jaroslav@163: long k = count - pos;
jaroslav@163: if (n < k) {
jaroslav@163: k = n < 0 ? 0 : n;
jaroslav@163: }
jaroslav@163:
jaroslav@163: pos += k;
jaroslav@163: return k;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Returns the number of remaining bytes that can be read (or skipped over)
jaroslav@163: * from this input stream.
jaroslav@163: *
jaroslav@163: * The value returned is count - pos
,
jaroslav@163: * which is the number of bytes remaining to be read from the input buffer.
jaroslav@163: *
jaroslav@163: * @return the number of remaining bytes that can be read (or skipped
jaroslav@163: * over) from this input stream without blocking.
jaroslav@163: */
jaroslav@163: public synchronized int available() {
jaroslav@163: return count - pos;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Tests if this InputStream
supports mark/reset. The
jaroslav@163: * markSupported
method of ByteArrayInputStream
jaroslav@163: * always returns true
.
jaroslav@163: *
jaroslav@163: * @since JDK1.1
jaroslav@163: */
jaroslav@163: public boolean markSupported() {
jaroslav@163: return true;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Set the current marked position in the stream.
jaroslav@163: * ByteArrayInputStream objects are marked at position zero by
jaroslav@163: * default when constructed. They may be marked at another
jaroslav@163: * position within the buffer by this method.
jaroslav@163: *
jaroslav@163: * If no mark has been set, then the value of the mark is the jaroslav@163: * offset passed to the constructor (or 0 if the offset was not jaroslav@163: * supplied). jaroslav@163: * jaroslav@163: *
Note: The readAheadLimit
for this class
jaroslav@163: * has no meaning.
jaroslav@163: *
jaroslav@163: * @since JDK1.1
jaroslav@163: */
jaroslav@163: public void mark(int readAheadLimit) {
jaroslav@163: mark = pos;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Resets the buffer to the marked position. The marked position
jaroslav@163: * is 0 unless another position was marked or an offset was specified
jaroslav@163: * in the constructor.
jaroslav@163: */
jaroslav@163: public synchronized void reset() {
jaroslav@163: pos = mark;
jaroslav@163: }
jaroslav@163:
jaroslav@163: /**
jaroslav@163: * Closing a ByteArrayInputStream has no effect. The methods in
jaroslav@163: * this class can be called after the stream has been closed without
jaroslav@163: * generating an IOException.
jaroslav@163: *
jaroslav@163: */ jaroslav@163: public void close() throws IOException { jaroslav@163: } jaroslav@163: jaroslav@163: }