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