Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/ByteArrayInputStream.java
38829 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* A <code>ByteArrayInputStream</code> contains29* an internal buffer that contains bytes that30* may be read from the stream. An internal31* counter keeps track of the next byte to32* be supplied by the <code>read</code> method.33* <p>34* Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in35* this class can be called after the stream has been closed without36* generating an <tt>IOException</tt>.37*38* @author Arthur van Hoff39* @see java.io.StringBufferInputStream40* @since JDK1.041*/42public43class ByteArrayInputStream extends InputStream {4445/**46* An array of bytes that was provided47* by the creator of the stream. Elements <code>buf[0]</code>48* through <code>buf[count-1]</code> are the49* only bytes that can ever be read from the50* stream; element <code>buf[pos]</code> is51* the next byte to be read.52*/53protected byte buf[];5455/**56* The index of the next character to read from the input stream buffer.57* This value should always be nonnegative58* and not larger than the value of <code>count</code>.59* The next byte to be read from the input stream buffer60* will be <code>buf[pos]</code>.61*/62protected int pos;6364/**65* The currently marked position in the stream.66* ByteArrayInputStream objects are marked at position zero by67* default when constructed. They may be marked at another68* position within the buffer by the <code>mark()</code> method.69* The current buffer position is set to this point by the70* <code>reset()</code> method.71* <p>72* If no mark has been set, then the value of mark is the offset73* passed to the constructor (or 0 if the offset was not supplied).74*75* @since JDK1.176*/77protected int mark = 0;7879/**80* The index one greater than the last valid character in the input81* stream buffer.82* This value should always be nonnegative83* and not larger than the length of <code>buf</code>.84* It is one greater than the position of85* the last byte within <code>buf</code> that86* can ever be read from the input stream buffer.87*/88protected int count;8990/**91* Creates a <code>ByteArrayInputStream</code>92* so that it uses <code>buf</code> as its93* buffer array.94* The buffer array is not copied.95* The initial value of <code>pos</code>96* is <code>0</code> and the initial value97* of <code>count</code> is the length of98* <code>buf</code>.99*100* @param buf the input buffer.101*/102public ByteArrayInputStream(byte buf[]) {103this.buf = buf;104this.pos = 0;105this.count = buf.length;106}107108/**109* Creates <code>ByteArrayInputStream</code>110* that uses <code>buf</code> as its111* buffer array. The initial value of <code>pos</code>112* is <code>offset</code> and the initial value113* of <code>count</code> is the minimum of <code>offset+length</code>114* and <code>buf.length</code>.115* The buffer array is not copied. The buffer's mark is116* set to the specified offset.117*118* @param buf the input buffer.119* @param offset the offset in the buffer of the first byte to read.120* @param length the maximum number of bytes to read from the buffer.121*/122public ByteArrayInputStream(byte buf[], int offset, int length) {123this.buf = buf;124this.pos = offset;125this.count = Math.min(offset + length, buf.length);126this.mark = offset;127}128129/**130* Reads the next byte of data from this input stream. The value131* byte is returned as an <code>int</code> in the range132* <code>0</code> to <code>255</code>. If no byte is available133* because the end of the stream has been reached, the value134* <code>-1</code> is returned.135* <p>136* This <code>read</code> method137* cannot block.138*139* @return the next byte of data, or <code>-1</code> if the end of the140* stream has been reached.141*/142public synchronized int read() {143return (pos < count) ? (buf[pos++] & 0xff) : -1;144}145146/**147* Reads up to <code>len</code> bytes of data into an array of bytes148* from this input stream.149* If <code>pos</code> equals <code>count</code>,150* then <code>-1</code> is returned to indicate151* end of file. Otherwise, the number <code>k</code>152* of bytes read is equal to the smaller of153* <code>len</code> and <code>count-pos</code>.154* If <code>k</code> is positive, then bytes155* <code>buf[pos]</code> through <code>buf[pos+k-1]</code>156* are copied into <code>b[off]</code> through157* <code>b[off+k-1]</code> in the manner performed158* by <code>System.arraycopy</code>. The159* value <code>k</code> is added into <code>pos</code>160* and <code>k</code> is returned.161* <p>162* This <code>read</code> method cannot block.163*164* @param b the buffer into which the data is read.165* @param off the start offset in the destination array <code>b</code>166* @param len the maximum number of bytes read.167* @return the total number of bytes read into the buffer, or168* <code>-1</code> if there is no more data because the end of169* the stream has been reached.170* @exception NullPointerException If <code>b</code> is <code>null</code>.171* @exception IndexOutOfBoundsException If <code>off</code> is negative,172* <code>len</code> is negative, or <code>len</code> is greater than173* <code>b.length - off</code>174*/175public synchronized int read(byte b[], int off, int len) {176if (b == null) {177throw new NullPointerException();178} else if (off < 0 || len < 0 || len > b.length - off) {179throw new IndexOutOfBoundsException();180}181182if (pos >= count) {183return -1;184}185186int avail = count - pos;187if (len > avail) {188len = avail;189}190if (len <= 0) {191return 0;192}193System.arraycopy(buf, pos, b, off, len);194pos += len;195return len;196}197198/**199* Skips <code>n</code> bytes of input from this input stream. Fewer200* bytes might be skipped if the end of the input stream is reached.201* The actual number <code>k</code>202* of bytes to be skipped is equal to the smaller203* of <code>n</code> and <code>count-pos</code>.204* The value <code>k</code> is added into <code>pos</code>205* and <code>k</code> is returned.206*207* @param n the number of bytes to be skipped.208* @return the actual number of bytes skipped.209*/210public synchronized long skip(long n) {211long k = count - pos;212if (n < k) {213k = n < 0 ? 0 : n;214}215216pos += k;217return k;218}219220/**221* Returns the number of remaining bytes that can be read (or skipped over)222* from this input stream.223* <p>224* The value returned is <code>count - pos</code>,225* which is the number of bytes remaining to be read from the input buffer.226*227* @return the number of remaining bytes that can be read (or skipped228* over) from this input stream without blocking.229*/230public synchronized int available() {231return count - pos;232}233234/**235* Tests if this <code>InputStream</code> supports mark/reset. The236* <code>markSupported</code> method of <code>ByteArrayInputStream</code>237* always returns <code>true</code>.238*239* @since JDK1.1240*/241public boolean markSupported() {242return true;243}244245/**246* Set the current marked position in the stream.247* ByteArrayInputStream objects are marked at position zero by248* default when constructed. They may be marked at another249* position within the buffer by this method.250* <p>251* If no mark has been set, then the value of the mark is the252* offset passed to the constructor (or 0 if the offset was not253* supplied).254*255* <p> Note: The <code>readAheadLimit</code> for this class256* has no meaning.257*258* @since JDK1.1259*/260public void mark(int readAheadLimit) {261mark = pos;262}263264/**265* Resets the buffer to the marked position. The marked position266* is 0 unless another position was marked or an offset was specified267* in the constructor.268*/269public synchronized void reset() {270pos = mark;271}272273/**274* Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in275* this class can be called after the stream has been closed without276* generating an <tt>IOException</tt>.277*/278public void close() throws IOException {279}280281}282283284