public final class StreamUtils extends Object
Modifier and Type | Method and Description |
---|---|
static String |
getRateString(long bytes,
long milliseconds)
Returns a string representing the transfer rate.
|
static String |
getRateString(long bytes,
long milliseconds,
Locale locale)
Returns a string representing the transfer rate.
|
static byte[] |
read(InputStream source,
int size)
Read a specified amount of bytes from the source InputStream and
place them into the returned byte array.
|
static int |
tryReadFully(InputStream source,
byte[] buffer,
int offset,
int len)
Read bytes from the source InputStream into the
buffer . |
public static String getRateString(long bytes, long milliseconds)
bytes
- bytes transferredmilliseconds
- time in millisecondspublic static String getRateString(long bytes, long milliseconds, Locale locale)
bytes
- bytes transferredmilliseconds
- time in millisecondslocale
- the current locale to get the right decimal separators. If
null default locale will be used.public static byte[] read(InputStream source, int size) throws IOException
Read a specified amount of bytes from the source InputStream and place them into the returned byte array.
This utility ensures that either size
bytes are read or
the end of the stream has been reached. If the end of stream has been
reached the returned array length is the length of the data read.
source
- Stream from which the data is read.size
- The maximum length of the data read.null
if the stream was 0 length, or a byte[] whose
length is the length of the stream if the EOS has been reached.IOException
- If the first byte cannot be read for any reason other
than end of file, or if the input stream has been
closed, or if some other I/O error occurs.IOException
- if any.public static int tryReadFully(InputStream source, byte[] buffer, int offset, int len) throws IOException
Read bytes from the source InputStream into the
buffer
.
This utility ensures that either len
bytes are read or the
end of the stream has been reached.
source
- Stream from which the data is read.buffer
- the buffer into which the data is read.offset
- the start offset in array buffer
at which the
data is written.len
- maximum length of the bytes read.-1
if there is no more data because the end of the
stream has been reached.IOException
- If the first byte cannot be read for any reason other than
end of file, or if the input stream has been closed, or if
some other I/O error occurs.NullPointerException
- If b
is null
.IndexOutOfBoundsException
- If off
is negative, len
is
negative, or len
is greater than
b.length - off
InputStream.read(byte[] buf,int off, int len)
Copyright © 2008–2016. All rights reserved.