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