Class Streams
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddrain(InputStream inStr) Read stream till EOF is encountered.static voidpipeAll(InputStream inStr, OutputStream outStr) Write the full contents of inStr to the destination stream outStr.static voidpipeAll(InputStream inStr, OutputStream outStr, int bufferSize) Write the full contents of inStr to the destination stream outStr.static longpipeAllLimited(InputStream inStr, long limit, OutputStream outStr) Write up to limit bytes of data from inStr to the destination stream outStr.static byte[]readAll(InputStream inStr) Read stream fully, returning contents in a byte array.static byte[]readAllLimited(InputStream inStr, int limit) Read from inStr up to a maximum number of bytes, throwing an exception if more the maximum amount of requested data is available.static intreadFully(InputStream inStr, byte[] buf) Fully read in buf's length in data, or up to EOF, whichever occurs first,static intreadFully(InputStream inStr, byte[] buf, int off, int len) Fully read in len's bytes of data into buf, or up to EOF, whichever occurs first,static byte[]readLenBytesFully(InputStream inStr, int len) Read exactlylenbytes frominStrand return them as a newly allocated array.static voidvalidateBufferArguments(byte[] buf, int off, int len) static voidwriteBufTo(ByteArrayOutputStream buf, OutputStream output)
-
Constructor Details
-
Streams
public Streams()
-
-
Method Details
-
drain
Read stream till EOF is encountered.- Parameters:
inStr- stream to be emptied.- Throws:
IOException- in case of underlying IOException.
-
pipeAll
Write the full contents of inStr to the destination stream outStr.- Parameters:
inStr- source input stream.outStr- destination output stream.- Throws:
IOException- in case of underlying IOException.
-
pipeAll
public static void pipeAll(InputStream inStr, OutputStream outStr, int bufferSize) throws IOException Write the full contents of inStr to the destination stream outStr.- Parameters:
inStr- source input stream.outStr- destination output stream.bufferSize- the size of temporary buffer to use.- Throws:
IOException- in case of underlying IOException.
-
pipeAllLimited
public static long pipeAllLimited(InputStream inStr, long limit, OutputStream outStr) throws IOException Write up to limit bytes of data from inStr to the destination stream outStr.- Parameters:
inStr- source input stream.limit- the maximum number of bytes allowed to be read.outStr- destination output stream.- Throws:
IOException- in case of underlying IOException, or if limit is reached on inStr still has data in it.
-
readAll
Read stream fully, returning contents in a byte array.- Parameters:
inStr- stream to be read.- Returns:
- a byte array representing the contents of inStr.
- Throws:
IOException- in case of underlying IOException.
-
readAllLimited
Read from inStr up to a maximum number of bytes, throwing an exception if more the maximum amount of requested data is available.- Parameters:
inStr- stream to be read.limit- maximum number of bytes that can be read.- Returns:
- a byte array representing the contents of inStr.
- Throws:
IOException- in case of underlying IOException, or if limit is reached on inStr still has data in it.
-
readFully
Fully read in buf's length in data, or up to EOF, whichever occurs first,- Parameters:
inStr- the stream to be read.buf- the buffer to be read into.- Returns:
- the number of bytes read into the buffer.
- Throws:
IOException- in case of underlying IOException.
-
readFully
Fully read in len's bytes of data into buf, or up to EOF, whichever occurs first,- Parameters:
inStr- the stream to be read.buf- the buffer to be read into.off- offset into buf to start putting bytes into.len- the number of bytes to be read.- Returns:
- the number of bytes read into the buffer.
- Throws:
IOException- in case of underlying IOException.
-
readLenBytesFully
Read exactlylenbytes frominStrand return them as a newly allocated array.Unlike
new byte[len]followed byreadFully(InputStream, byte[]), the full-length array is not allocated until the stream has actually delivered a quarter of the declared length, so a caller passing an untrusted (possibly hostile) length cannot drive a large allocation from a short input: a stream that ends beforelenbytes have been read fails with anEOFException, having caused at most five times the delivered bytes plus 128 KiB to be allocated.Lengths up to 128 KiB are allocated up front and read directly. Above that, the first quarter of the data is read into a rope of 64 KiB chunks, each allocated just ahead of its own fill; the result is then allocated once, the chunks copied into it and the remainder read directly into it. The only intermediate arrays are the chunks, so a read leaves no large-object garbage behind - a geometrically grown single buffer would instead leave one humongous array per doubling on G1 and the other region based collectors. The chunks are fresh zeroed arrays that are never reused: the stream is handed a reference to each of them, so pooling would not be hygienic.
- Parameters:
inStr- the stream to read from.len- the exact number of bytes to read.- Returns:
- a
byte[len]containing the bytes read. - Throws:
EOFException- if the stream ends beforelenbytes are available.IOException- on an underlying read error.IllegalArgumentException- iflenis negative.
-
validateBufferArguments
public static void validateBufferArguments(byte[] buf, int off, int len) -
writeBufTo
- Throws:
IOException
-