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