emul/mini/src/main/java/java/io/ByteArrayInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 20:39:23 +0100
branchemul
changeset 554 05224402145d
parent 164 emul/src/main/java/java/io/ByteArrayInputStream.java@eb7c3802b3b7
child 560 53fafe384803
permissions -rw-r--r--
First attempt to separate 'mini' profile from the rest of JDK APIs
jaroslav@163
     1
/*
jaroslav@163
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@163
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@163
     4
 *
jaroslav@163
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@163
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@163
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@163
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@163
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@163
    10
 *
jaroslav@163
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@163
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@163
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@163
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@163
    15
 * accompanied this code).
jaroslav@163
    16
 *
jaroslav@163
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@163
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@163
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@163
    20
 *
jaroslav@163
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@163
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@163
    23
 * questions.
jaroslav@163
    24
 */
jaroslav@163
    25
jaroslav@163
    26
package java.io;
jaroslav@163
    27
jaroslav@163
    28
/**
jaroslav@163
    29
 * A <code>ByteArrayInputStream</code> contains
jaroslav@163
    30
 * an internal buffer that contains bytes that
jaroslav@163
    31
 * may be read from the stream. An internal
jaroslav@163
    32
 * counter keeps track of the next byte to
jaroslav@163
    33
 * be supplied by the <code>read</code> method.
jaroslav@163
    34
 * <p>
jaroslav@163
    35
 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
jaroslav@163
    36
 * this class can be called after the stream has been closed without
jaroslav@163
    37
 * generating an <tt>IOException</tt>.
jaroslav@163
    38
 *
jaroslav@163
    39
 * @author  Arthur van Hoff
jaroslav@163
    40
 * @see     java.io.StringBufferInputStream
jaroslav@163
    41
 * @since   JDK1.0
jaroslav@163
    42
 */
jaroslav@163
    43
public
jaroslav@163
    44
class ByteArrayInputStream extends InputStream {
jaroslav@163
    45
jaroslav@163
    46
    /**
jaroslav@163
    47
     * An array of bytes that was provided
jaroslav@163
    48
     * by the creator of the stream. Elements <code>buf[0]</code>
jaroslav@163
    49
     * through <code>buf[count-1]</code> are the
jaroslav@163
    50
     * only bytes that can ever be read from the
jaroslav@163
    51
     * stream;  element <code>buf[pos]</code> is
jaroslav@163
    52
     * the next byte to be read.
jaroslav@163
    53
     */
jaroslav@163
    54
    protected byte buf[];
jaroslav@163
    55
jaroslav@163
    56
    /**
jaroslav@163
    57
     * The index of the next character to read from the input stream buffer.
jaroslav@163
    58
     * This value should always be nonnegative
jaroslav@163
    59
     * and not larger than the value of <code>count</code>.
jaroslav@163
    60
     * The next byte to be read from the input stream buffer
jaroslav@163
    61
     * will be <code>buf[pos]</code>.
jaroslav@163
    62
     */
jaroslav@163
    63
    protected int pos;
jaroslav@163
    64
jaroslav@163
    65
    /**
jaroslav@163
    66
     * The currently marked position in the stream.
jaroslav@163
    67
     * ByteArrayInputStream objects are marked at position zero by
jaroslav@163
    68
     * default when constructed.  They may be marked at another
jaroslav@163
    69
     * position within the buffer by the <code>mark()</code> method.
jaroslav@163
    70
     * The current buffer position is set to this point by the
jaroslav@163
    71
     * <code>reset()</code> method.
jaroslav@163
    72
     * <p>
jaroslav@163
    73
     * If no mark has been set, then the value of mark is the offset
jaroslav@163
    74
     * passed to the constructor (or 0 if the offset was not supplied).
jaroslav@163
    75
     *
jaroslav@163
    76
     * @since   JDK1.1
jaroslav@163
    77
     */
jaroslav@163
    78
    protected int mark = 0;
jaroslav@163
    79
jaroslav@163
    80
    /**
jaroslav@163
    81
     * The index one greater than the last valid character in the input
jaroslav@163
    82
     * stream buffer.
jaroslav@163
    83
     * This value should always be nonnegative
jaroslav@163
    84
     * and not larger than the length of <code>buf</code>.
jaroslav@163
    85
     * It  is one greater than the position of
jaroslav@163
    86
     * the last byte within <code>buf</code> that
jaroslav@163
    87
     * can ever be read  from the input stream buffer.
jaroslav@163
    88
     */
jaroslav@163
    89
    protected int count;
jaroslav@163
    90
jaroslav@163
    91
    /**
jaroslav@163
    92
     * Creates a <code>ByteArrayInputStream</code>
jaroslav@163
    93
     * so that it  uses <code>buf</code> as its
jaroslav@163
    94
     * buffer array.
jaroslav@163
    95
     * The buffer array is not copied.
jaroslav@163
    96
     * The initial value of <code>pos</code>
jaroslav@163
    97
     * is <code>0</code> and the initial value
jaroslav@163
    98
     * of  <code>count</code> is the length of
jaroslav@163
    99
     * <code>buf</code>.
jaroslav@163
   100
     *
jaroslav@163
   101
     * @param   buf   the input buffer.
jaroslav@163
   102
     */
jaroslav@163
   103
    public ByteArrayInputStream(byte buf[]) {
jaroslav@163
   104
        this.buf = buf;
jaroslav@163
   105
        this.pos = 0;
jaroslav@163
   106
        this.count = buf.length;
jaroslav@163
   107
    }
jaroslav@163
   108
jaroslav@163
   109
    /**
jaroslav@163
   110
     * Creates <code>ByteArrayInputStream</code>
jaroslav@163
   111
     * that uses <code>buf</code> as its
jaroslav@163
   112
     * buffer array. The initial value of <code>pos</code>
jaroslav@163
   113
     * is <code>offset</code> and the initial value
jaroslav@163
   114
     * of <code>count</code> is the minimum of <code>offset+length</code>
jaroslav@163
   115
     * and <code>buf.length</code>.
jaroslav@163
   116
     * The buffer array is not copied. The buffer's mark is
jaroslav@163
   117
     * set to the specified offset.
jaroslav@163
   118
     *
jaroslav@163
   119
     * @param   buf      the input buffer.
jaroslav@163
   120
     * @param   offset   the offset in the buffer of the first byte to read.
jaroslav@163
   121
     * @param   length   the maximum number of bytes to read from the buffer.
jaroslav@163
   122
     */
jaroslav@163
   123
    public ByteArrayInputStream(byte buf[], int offset, int length) {
jaroslav@163
   124
        this.buf = buf;
jaroslav@163
   125
        this.pos = offset;
jaroslav@163
   126
        this.count = Math.min(offset + length, buf.length);
jaroslav@163
   127
        this.mark = offset;
jaroslav@163
   128
    }
jaroslav@163
   129
jaroslav@163
   130
    /**
jaroslav@163
   131
     * Reads the next byte of data from this input stream. The value
jaroslav@163
   132
     * byte is returned as an <code>int</code> in the range
jaroslav@163
   133
     * <code>0</code> to <code>255</code>. If no byte is available
jaroslav@163
   134
     * because the end of the stream has been reached, the value
jaroslav@163
   135
     * <code>-1</code> is returned.
jaroslav@163
   136
     * <p>
jaroslav@163
   137
     * This <code>read</code> method
jaroslav@163
   138
     * cannot block.
jaroslav@163
   139
     *
jaroslav@163
   140
     * @return  the next byte of data, or <code>-1</code> if the end of the
jaroslav@163
   141
     *          stream has been reached.
jaroslav@163
   142
     */
jaroslav@163
   143
    public synchronized int read() {
jaroslav@163
   144
        return (pos < count) ? (buf[pos++] & 0xff) : -1;
jaroslav@163
   145
    }
jaroslav@163
   146
jaroslav@163
   147
    /**
jaroslav@163
   148
     * Reads up to <code>len</code> bytes of data into an array of bytes
jaroslav@163
   149
     * from this input stream.
jaroslav@163
   150
     * If <code>pos</code> equals <code>count</code>,
jaroslav@163
   151
     * then <code>-1</code> is returned to indicate
jaroslav@163
   152
     * end of file. Otherwise, the  number <code>k</code>
jaroslav@163
   153
     * of bytes read is equal to the smaller of
jaroslav@163
   154
     * <code>len</code> and <code>count-pos</code>.
jaroslav@163
   155
     * If <code>k</code> is positive, then bytes
jaroslav@163
   156
     * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
jaroslav@163
   157
     * are copied into <code>b[off]</code>  through
jaroslav@163
   158
     * <code>b[off+k-1]</code> in the manner performed
jaroslav@163
   159
     * by <code>System.arraycopy</code>. The
jaroslav@163
   160
     * value <code>k</code> is added into <code>pos</code>
jaroslav@163
   161
     * and <code>k</code> is returned.
jaroslav@163
   162
     * <p>
jaroslav@163
   163
     * This <code>read</code> method cannot block.
jaroslav@163
   164
     *
jaroslav@163
   165
     * @param   b     the buffer into which the data is read.
jaroslav@163
   166
     * @param   off   the start offset in the destination array <code>b</code>
jaroslav@163
   167
     * @param   len   the maximum number of bytes read.
jaroslav@163
   168
     * @return  the total number of bytes read into the buffer, or
jaroslav@163
   169
     *          <code>-1</code> if there is no more data because the end of
jaroslav@163
   170
     *          the stream has been reached.
jaroslav@163
   171
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
jaroslav@163
   172
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
jaroslav@163
   173
     * <code>len</code> is negative, or <code>len</code> is greater than
jaroslav@163
   174
     * <code>b.length - off</code>
jaroslav@163
   175
     */
jaroslav@163
   176
    public synchronized int read(byte b[], int off, int len) {
jaroslav@163
   177
        if (b == null) {
jaroslav@163
   178
            throw new NullPointerException();
jaroslav@163
   179
        } else if (off < 0 || len < 0 || len > b.length - off) {
jaroslav@163
   180
            throw new IndexOutOfBoundsException();
jaroslav@163
   181
        }
jaroslav@163
   182
jaroslav@163
   183
        if (pos >= count) {
jaroslav@163
   184
            return -1;
jaroslav@163
   185
        }
jaroslav@163
   186
jaroslav@163
   187
        int avail = count - pos;
jaroslav@163
   188
        if (len > avail) {
jaroslav@163
   189
            len = avail;
jaroslav@163
   190
        }
jaroslav@163
   191
        if (len <= 0) {
jaroslav@163
   192
            return 0;
jaroslav@163
   193
        }
jtulach@164
   194
        PushbackInputStream.arraycopy(buf, pos, b, off, len);
jaroslav@163
   195
        pos += len;
jaroslav@163
   196
        return len;
jaroslav@163
   197
    }
jaroslav@163
   198
jaroslav@163
   199
    /**
jaroslav@163
   200
     * Skips <code>n</code> bytes of input from this input stream. Fewer
jaroslav@163
   201
     * bytes might be skipped if the end of the input stream is reached.
jaroslav@163
   202
     * The actual number <code>k</code>
jaroslav@163
   203
     * of bytes to be skipped is equal to the smaller
jaroslav@163
   204
     * of <code>n</code> and  <code>count-pos</code>.
jaroslav@163
   205
     * The value <code>k</code> is added into <code>pos</code>
jaroslav@163
   206
     * and <code>k</code> is returned.
jaroslav@163
   207
     *
jaroslav@163
   208
     * @param   n   the number of bytes to be skipped.
jaroslav@163
   209
     * @return  the actual number of bytes skipped.
jaroslav@163
   210
     */
jaroslav@163
   211
    public synchronized long skip(long n) {
jaroslav@163
   212
        long k = count - pos;
jaroslav@163
   213
        if (n < k) {
jaroslav@163
   214
            k = n < 0 ? 0 : n;
jaroslav@163
   215
        }
jaroslav@163
   216
jaroslav@163
   217
        pos += k;
jaroslav@163
   218
        return k;
jaroslav@163
   219
    }
jaroslav@163
   220
jaroslav@163
   221
    /**
jaroslav@163
   222
     * Returns the number of remaining bytes that can be read (or skipped over)
jaroslav@163
   223
     * from this input stream.
jaroslav@163
   224
     * <p>
jaroslav@163
   225
     * The value returned is <code>count&nbsp;- pos</code>,
jaroslav@163
   226
     * which is the number of bytes remaining to be read from the input buffer.
jaroslav@163
   227
     *
jaroslav@163
   228
     * @return  the number of remaining bytes that can be read (or skipped
jaroslav@163
   229
     *          over) from this input stream without blocking.
jaroslav@163
   230
     */
jaroslav@163
   231
    public synchronized int available() {
jaroslav@163
   232
        return count - pos;
jaroslav@163
   233
    }
jaroslav@163
   234
jaroslav@163
   235
    /**
jaroslav@163
   236
     * Tests if this <code>InputStream</code> supports mark/reset. The
jaroslav@163
   237
     * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
jaroslav@163
   238
     * always returns <code>true</code>.
jaroslav@163
   239
     *
jaroslav@163
   240
     * @since   JDK1.1
jaroslav@163
   241
     */
jaroslav@163
   242
    public boolean markSupported() {
jaroslav@163
   243
        return true;
jaroslav@163
   244
    }
jaroslav@163
   245
jaroslav@163
   246
    /**
jaroslav@163
   247
     * Set the current marked position in the stream.
jaroslav@163
   248
     * ByteArrayInputStream objects are marked at position zero by
jaroslav@163
   249
     * default when constructed.  They may be marked at another
jaroslav@163
   250
     * position within the buffer by this method.
jaroslav@163
   251
     * <p>
jaroslav@163
   252
     * If no mark has been set, then the value of the mark is the
jaroslav@163
   253
     * offset passed to the constructor (or 0 if the offset was not
jaroslav@163
   254
     * supplied).
jaroslav@163
   255
     *
jaroslav@163
   256
     * <p> Note: The <code>readAheadLimit</code> for this class
jaroslav@163
   257
     *  has no meaning.
jaroslav@163
   258
     *
jaroslav@163
   259
     * @since   JDK1.1
jaroslav@163
   260
     */
jaroslav@163
   261
    public void mark(int readAheadLimit) {
jaroslav@163
   262
        mark = pos;
jaroslav@163
   263
    }
jaroslav@163
   264
jaroslav@163
   265
    /**
jaroslav@163
   266
     * Resets the buffer to the marked position.  The marked position
jaroslav@163
   267
     * is 0 unless another position was marked or an offset was specified
jaroslav@163
   268
     * in the constructor.
jaroslav@163
   269
     */
jaroslav@163
   270
    public synchronized void reset() {
jaroslav@163
   271
        pos = mark;
jaroslav@163
   272
    }
jaroslav@163
   273
jaroslav@163
   274
    /**
jaroslav@163
   275
     * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
jaroslav@163
   276
     * this class can be called after the stream has been closed without
jaroslav@163
   277
     * generating an <tt>IOException</tt>.
jaroslav@163
   278
     * <p>
jaroslav@163
   279
     */
jaroslav@163
   280
    public void close() throws IOException {
jaroslav@163
   281
    }
jaroslav@163
   282
jaroslav@163
   283
}