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 * This abstract class is the superclass of all classes representing 33 * an input stream of bytes. 34 * 35 * <p> Applications that need to define a subclass of <code>InputStream</code> 36 * must always provide a method that returns the next byte of input. 37 * 38 * @author Arthur van Hoff 39 * @see java.io.BufferedInputStream 40 * @see java.io.ByteArrayInputStream 41 * @see java.io.DataInputStream 42 * @see java.io.FilterInputStream 43 * @see java.io.InputStream#read() 44 * @see java.io.OutputStream 45 * @see java.io.PushbackInputStream 46 * @since 1.0 47 */ 48 public abstract class InputStream implements Closeable { 212 * 213 * <p> The behavior for the case where the input stream is <i>asynchronously 214 * closed</i>, or the thread interrupted during the read, is highly input 215 * stream specific, and therefore not specified. 216 * 217 * <p> If an I/O error occurs reading from the input stream, then it may do 218 * so after some, but not all, bytes have been read. Consequently the input 219 * stream may not be at end of stream and may be in an inconsistent state. 220 * It is strongly recommended that the stream be promptly closed if an I/O 221 * error occurs. 222 * 223 * @return a byte array containing the bytes read from this input stream 224 * @throws IOException if an I/O error occurs 225 * @throws OutOfMemoryError if an array of the required size cannot be 226 * allocated. For example, if an array larger than {@code 2GB} would 227 * be required to store the bytes. 228 * 229 * @since 9 230 */ 231 public byte[] readAllBytes() throws IOException { 232 byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; 233 int capacity = buf.length; 234 int nread = 0; 235 int n; 236 for (;;) { 237 // read to EOF which may read more or less than initial buffer size 238 while ((n = read(buf, nread, capacity - nread)) > 0) 239 nread += n; 240 241 // if the last call to read returned -1, then we're done 242 if (n < 0) 243 break; 244 245 // need to allocate a larger buffer 246 if (capacity <= MAX_BUFFER_SIZE - capacity) { 247 capacity = capacity << 1; 248 } else { 249 if (capacity == MAX_BUFFER_SIZE) 250 throw new OutOfMemoryError("Required array size too large"); 251 capacity = MAX_BUFFER_SIZE; 252 } 253 buf = Arrays.copyOf(buf, capacity); 254 } 255 return (capacity == nread) ? buf : Arrays.copyOf(buf, nread); 256 } 257 258 /** 259 * Reads the requested number of bytes from the input stream into the given 260 * byte array. This method blocks until {@code len} bytes of input data have 261 * been read, end of stream is detected, or an exception is thrown. The 262 * number of bytes actually read, possibly zero, is returned. This method 263 * does not close the input stream. 264 * 265 * <p> In the case where end of stream is reached before {@code len} bytes 266 * have been read, then the actual number of bytes read will be returned. 267 * When this stream reaches end of stream, further invocations of this 268 * method will return zero. 269 * 270 * <p> If {@code len} is zero, then no bytes are read and {@code 0} is 271 * returned; otherwise, there is an attempt to read up to {@code len} bytes. 272 * 273 * <p> The first byte read is stored into element {@code b[off]}, the next 274 * one in to {@code b[off+1]}, and so on. The number of bytes read is, at 275 * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually | 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.ArrayList; 29 import java.util.Arrays; 30 import java.util.List; 31 import java.util.Objects; 32 33 /** 34 * This abstract class is the superclass of all classes representing 35 * an input stream of bytes. 36 * 37 * <p> Applications that need to define a subclass of <code>InputStream</code> 38 * must always provide a method that returns the next byte of input. 39 * 40 * @author Arthur van Hoff 41 * @see java.io.BufferedInputStream 42 * @see java.io.ByteArrayInputStream 43 * @see java.io.DataInputStream 44 * @see java.io.FilterInputStream 45 * @see java.io.InputStream#read() 46 * @see java.io.OutputStream 47 * @see java.io.PushbackInputStream 48 * @since 1.0 49 */ 50 public abstract class InputStream implements Closeable { 214 * 215 * <p> The behavior for the case where the input stream is <i>asynchronously 216 * closed</i>, or the thread interrupted during the read, is highly input 217 * stream specific, and therefore not specified. 218 * 219 * <p> If an I/O error occurs reading from the input stream, then it may do 220 * so after some, but not all, bytes have been read. Consequently the input 221 * stream may not be at end of stream and may be in an inconsistent state. 222 * It is strongly recommended that the stream be promptly closed if an I/O 223 * error occurs. 224 * 225 * @return a byte array containing the bytes read from this input stream 226 * @throws IOException if an I/O error occurs 227 * @throws OutOfMemoryError if an array of the required size cannot be 228 * allocated. For example, if an array larger than {@code 2GB} would 229 * be required to store the bytes. 230 * 231 * @since 9 232 */ 233 public byte[] readAllBytes() throws IOException { 234 List<byte[]> bufs = null; 235 byte[] result = null; 236 int total = 0; 237 int n; 238 do { 239 byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; 240 int nread = 0; 241 242 // read to EOF which may read more or less than buffer size 243 while ((n = read(buf, nread, buf.length - nread)) > 0) { 244 nread += n; 245 } 246 247 if (nread > 0) { 248 if (MAX_BUFFER_SIZE - total < nread) { 249 throw new OutOfMemoryError("Required array size too large"); 250 } 251 total += nread; 252 if (result == null) { 253 result = buf; 254 } else { 255 if (bufs == null) { 256 bufs = new ArrayList<>(); 257 bufs.add(result); 258 } 259 bufs.add(buf); 260 } 261 } 262 } while (n >= 0); // if the last call to read returned -1, then break 263 264 if (bufs == null) { 265 if (result == null) { 266 return new byte[0]; 267 } 268 return result.length == total ? 269 result : Arrays.copyOf(result, total); 270 } 271 272 result = new byte[total]; 273 int offset = 0; 274 int remaining = total; 275 for (byte[] b : bufs) { 276 int len = Math.min(b.length, remaining); 277 System.arraycopy(b, 0, result, offset, len); 278 offset += len; 279 remaining -= len; 280 } 281 282 return result; 283 } 284 285 /** 286 * Reads the requested number of bytes from the input stream into the given 287 * byte array. This method blocks until {@code len} bytes of input data have 288 * been read, end of stream is detected, or an exception is thrown. The 289 * number of bytes actually read, possibly zero, is returned. This method 290 * does not close the input stream. 291 * 292 * <p> In the case where end of stream is reached before {@code len} bytes 293 * have been read, then the actual number of bytes read will be returned. 294 * When this stream reaches end of stream, further invocations of this 295 * method will return zero. 296 * 297 * <p> If {@code len} is zero, then no bytes are read and {@code 0} is 298 * returned; otherwise, there is an attempt to read up to {@code len} bytes. 299 * 300 * <p> The first byte read is stored into element {@code b[off]}, the next 301 * one in to {@code b[off+1]}, and so on. The number of bytes read is, at 302 * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually |