emul/mini/src/main/java/java/io/ByteArrayInputStream.java
branchemul
changeset 554 05224402145d
parent 164 eb7c3802b3b7
child 560 53fafe384803
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/java/io/ByteArrayInputStream.java	Wed Jan 23 20:39:23 2013 +0100
     1.3 @@ -0,0 +1,283 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2010, 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 + * A <code>ByteArrayInputStream</code> contains
    1.33 + * an internal buffer that contains bytes that
    1.34 + * may be read from the stream. An internal
    1.35 + * counter keeps track of the next byte to
    1.36 + * be supplied by the <code>read</code> method.
    1.37 + * <p>
    1.38 + * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
    1.39 + * this class can be called after the stream has been closed without
    1.40 + * generating an <tt>IOException</tt>.
    1.41 + *
    1.42 + * @author  Arthur van Hoff
    1.43 + * @see     java.io.StringBufferInputStream
    1.44 + * @since   JDK1.0
    1.45 + */
    1.46 +public
    1.47 +class ByteArrayInputStream extends InputStream {
    1.48 +
    1.49 +    /**
    1.50 +     * An array of bytes that was provided
    1.51 +     * by the creator of the stream. Elements <code>buf[0]</code>
    1.52 +     * through <code>buf[count-1]</code> are the
    1.53 +     * only bytes that can ever be read from the
    1.54 +     * stream;  element <code>buf[pos]</code> is
    1.55 +     * the next byte to be read.
    1.56 +     */
    1.57 +    protected byte buf[];
    1.58 +
    1.59 +    /**
    1.60 +     * The index of the next character to read from the input stream buffer.
    1.61 +     * This value should always be nonnegative
    1.62 +     * and not larger than the value of <code>count</code>.
    1.63 +     * The next byte to be read from the input stream buffer
    1.64 +     * will be <code>buf[pos]</code>.
    1.65 +     */
    1.66 +    protected int pos;
    1.67 +
    1.68 +    /**
    1.69 +     * The currently marked position in the stream.
    1.70 +     * ByteArrayInputStream objects are marked at position zero by
    1.71 +     * default when constructed.  They may be marked at another
    1.72 +     * position within the buffer by the <code>mark()</code> method.
    1.73 +     * The current buffer position is set to this point by the
    1.74 +     * <code>reset()</code> method.
    1.75 +     * <p>
    1.76 +     * If no mark has been set, then the value of mark is the offset
    1.77 +     * passed to the constructor (or 0 if the offset was not supplied).
    1.78 +     *
    1.79 +     * @since   JDK1.1
    1.80 +     */
    1.81 +    protected int mark = 0;
    1.82 +
    1.83 +    /**
    1.84 +     * The index one greater than the last valid character in the input
    1.85 +     * stream buffer.
    1.86 +     * This value should always be nonnegative
    1.87 +     * and not larger than the length of <code>buf</code>.
    1.88 +     * It  is one greater than the position of
    1.89 +     * the last byte within <code>buf</code> that
    1.90 +     * can ever be read  from the input stream buffer.
    1.91 +     */
    1.92 +    protected int count;
    1.93 +
    1.94 +    /**
    1.95 +     * Creates a <code>ByteArrayInputStream</code>
    1.96 +     * so that it  uses <code>buf</code> as its
    1.97 +     * buffer array.
    1.98 +     * The buffer array is not copied.
    1.99 +     * The initial value of <code>pos</code>
   1.100 +     * is <code>0</code> and the initial value
   1.101 +     * of  <code>count</code> is the length of
   1.102 +     * <code>buf</code>.
   1.103 +     *
   1.104 +     * @param   buf   the input buffer.
   1.105 +     */
   1.106 +    public ByteArrayInputStream(byte buf[]) {
   1.107 +        this.buf = buf;
   1.108 +        this.pos = 0;
   1.109 +        this.count = buf.length;
   1.110 +    }
   1.111 +
   1.112 +    /**
   1.113 +     * Creates <code>ByteArrayInputStream</code>
   1.114 +     * that uses <code>buf</code> as its
   1.115 +     * buffer array. The initial value of <code>pos</code>
   1.116 +     * is <code>offset</code> and the initial value
   1.117 +     * of <code>count</code> is the minimum of <code>offset+length</code>
   1.118 +     * and <code>buf.length</code>.
   1.119 +     * The buffer array is not copied. The buffer's mark is
   1.120 +     * set to the specified offset.
   1.121 +     *
   1.122 +     * @param   buf      the input buffer.
   1.123 +     * @param   offset   the offset in the buffer of the first byte to read.
   1.124 +     * @param   length   the maximum number of bytes to read from the buffer.
   1.125 +     */
   1.126 +    public ByteArrayInputStream(byte buf[], int offset, int length) {
   1.127 +        this.buf = buf;
   1.128 +        this.pos = offset;
   1.129 +        this.count = Math.min(offset + length, buf.length);
   1.130 +        this.mark = offset;
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Reads the next byte of data from this input stream. The value
   1.135 +     * byte is returned as an <code>int</code> in the range
   1.136 +     * <code>0</code> to <code>255</code>. If no byte is available
   1.137 +     * because the end of the stream has been reached, the value
   1.138 +     * <code>-1</code> is returned.
   1.139 +     * <p>
   1.140 +     * This <code>read</code> method
   1.141 +     * cannot block.
   1.142 +     *
   1.143 +     * @return  the next byte of data, or <code>-1</code> if the end of the
   1.144 +     *          stream has been reached.
   1.145 +     */
   1.146 +    public synchronized int read() {
   1.147 +        return (pos < count) ? (buf[pos++] & 0xff) : -1;
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Reads up to <code>len</code> bytes of data into an array of bytes
   1.152 +     * from this input stream.
   1.153 +     * If <code>pos</code> equals <code>count</code>,
   1.154 +     * then <code>-1</code> is returned to indicate
   1.155 +     * end of file. Otherwise, the  number <code>k</code>
   1.156 +     * of bytes read is equal to the smaller of
   1.157 +     * <code>len</code> and <code>count-pos</code>.
   1.158 +     * If <code>k</code> is positive, then bytes
   1.159 +     * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
   1.160 +     * are copied into <code>b[off]</code>  through
   1.161 +     * <code>b[off+k-1]</code> in the manner performed
   1.162 +     * by <code>System.arraycopy</code>. The
   1.163 +     * value <code>k</code> is added into <code>pos</code>
   1.164 +     * and <code>k</code> is returned.
   1.165 +     * <p>
   1.166 +     * This <code>read</code> method cannot block.
   1.167 +     *
   1.168 +     * @param   b     the buffer into which the data is read.
   1.169 +     * @param   off   the start offset in the destination array <code>b</code>
   1.170 +     * @param   len   the maximum number of bytes read.
   1.171 +     * @return  the total number of bytes read into the buffer, or
   1.172 +     *          <code>-1</code> if there is no more data because the end of
   1.173 +     *          the stream has been reached.
   1.174 +     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
   1.175 +     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
   1.176 +     * <code>len</code> is negative, or <code>len</code> is greater than
   1.177 +     * <code>b.length - off</code>
   1.178 +     */
   1.179 +    public synchronized int read(byte b[], int off, int len) {
   1.180 +        if (b == null) {
   1.181 +            throw new NullPointerException();
   1.182 +        } else if (off < 0 || len < 0 || len > b.length - off) {
   1.183 +            throw new IndexOutOfBoundsException();
   1.184 +        }
   1.185 +
   1.186 +        if (pos >= count) {
   1.187 +            return -1;
   1.188 +        }
   1.189 +
   1.190 +        int avail = count - pos;
   1.191 +        if (len > avail) {
   1.192 +            len = avail;
   1.193 +        }
   1.194 +        if (len <= 0) {
   1.195 +            return 0;
   1.196 +        }
   1.197 +        PushbackInputStream.arraycopy(buf, pos, b, off, len);
   1.198 +        pos += len;
   1.199 +        return len;
   1.200 +    }
   1.201 +
   1.202 +    /**
   1.203 +     * Skips <code>n</code> bytes of input from this input stream. Fewer
   1.204 +     * bytes might be skipped if the end of the input stream is reached.
   1.205 +     * The actual number <code>k</code>
   1.206 +     * of bytes to be skipped is equal to the smaller
   1.207 +     * of <code>n</code> and  <code>count-pos</code>.
   1.208 +     * The value <code>k</code> is added into <code>pos</code>
   1.209 +     * and <code>k</code> is returned.
   1.210 +     *
   1.211 +     * @param   n   the number of bytes to be skipped.
   1.212 +     * @return  the actual number of bytes skipped.
   1.213 +     */
   1.214 +    public synchronized long skip(long n) {
   1.215 +        long k = count - pos;
   1.216 +        if (n < k) {
   1.217 +            k = n < 0 ? 0 : n;
   1.218 +        }
   1.219 +
   1.220 +        pos += k;
   1.221 +        return k;
   1.222 +    }
   1.223 +
   1.224 +    /**
   1.225 +     * Returns the number of remaining bytes that can be read (or skipped over)
   1.226 +     * from this input stream.
   1.227 +     * <p>
   1.228 +     * The value returned is <code>count&nbsp;- pos</code>,
   1.229 +     * which is the number of bytes remaining to be read from the input buffer.
   1.230 +     *
   1.231 +     * @return  the number of remaining bytes that can be read (or skipped
   1.232 +     *          over) from this input stream without blocking.
   1.233 +     */
   1.234 +    public synchronized int available() {
   1.235 +        return count - pos;
   1.236 +    }
   1.237 +
   1.238 +    /**
   1.239 +     * Tests if this <code>InputStream</code> supports mark/reset. The
   1.240 +     * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
   1.241 +     * always returns <code>true</code>.
   1.242 +     *
   1.243 +     * @since   JDK1.1
   1.244 +     */
   1.245 +    public boolean markSupported() {
   1.246 +        return true;
   1.247 +    }
   1.248 +
   1.249 +    /**
   1.250 +     * Set the current marked position in the stream.
   1.251 +     * ByteArrayInputStream objects are marked at position zero by
   1.252 +     * default when constructed.  They may be marked at another
   1.253 +     * position within the buffer by this method.
   1.254 +     * <p>
   1.255 +     * If no mark has been set, then the value of the mark is the
   1.256 +     * offset passed to the constructor (or 0 if the offset was not
   1.257 +     * supplied).
   1.258 +     *
   1.259 +     * <p> Note: The <code>readAheadLimit</code> for this class
   1.260 +     *  has no meaning.
   1.261 +     *
   1.262 +     * @since   JDK1.1
   1.263 +     */
   1.264 +    public void mark(int readAheadLimit) {
   1.265 +        mark = pos;
   1.266 +    }
   1.267 +
   1.268 +    /**
   1.269 +     * Resets the buffer to the marked position.  The marked position
   1.270 +     * is 0 unless another position was marked or an offset was specified
   1.271 +     * in the constructor.
   1.272 +     */
   1.273 +    public synchronized void reset() {
   1.274 +        pos = mark;
   1.275 +    }
   1.276 +
   1.277 +    /**
   1.278 +     * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
   1.279 +     * this class can be called after the stream has been closed without
   1.280 +     * generating an <tt>IOException</tt>.
   1.281 +     * <p>
   1.282 +     */
   1.283 +    public void close() throws IOException {
   1.284 +    }
   1.285 +
   1.286 +}