Class InputStreamHelper
InputStream
s.-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
Reads all remaining bytes from the input stream.static byte[]
readNBytes
(InputStream is, int len) Reads up to a specified number of bytes from the input stream.
-
Method Details
-
readAllBytes
Reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and end of stream is detected, or an exception is thrown. This method does not close the input stream.When this stream reaches end of stream, further invocations of this method will return an empty byte array.
Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading input streams with large amounts of data.
The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.
If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.
- Returns:
- a byte array containing the bytes read from this input stream
- Throws:
IOException
- if an I/O error occursOutOfMemoryError
- if an array of the required size cannot be allocated.- Since:
- 9
-
readNBytes
Reads up to a specified number of bytes from the input stream. This method blocks until the requested number of bytes has been read, end of stream is detected, or an exception is thrown. This method does not close the input stream.The length of the returned array equals the number of bytes read from the stream. If
len
is zero, then no bytes are read and an empty byte array is returned. Otherwise, up tolen
bytes are read from the stream. Fewer thanlen
bytes may be read if end of stream is encountered.When this stream reaches end of stream, further invocations of this method will return an empty byte array.
Note that this method is intended for simple cases where it is convenient to read the specified number of bytes into a byte array. The total amount of memory allocated by this method is proportional to the number of bytes read from the stream which is bounded by
len
. Therefore, the method may be safely called with very large values oflen
provided sufficient memory is available.The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.
If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.
- Parameters:
len
- the maximum number of bytes to read- Returns:
- a byte array containing the bytes read from this input stream
- Throws:
IllegalArgumentException
- iflength
is negativeIOException
- if an I/O error occursOutOfMemoryError
- if an array of the required size cannot be allocated.Source:
InputStream.readNBytes(int)
-