public class IOHelper extends Object
Modifier and Type | Method and Description |
---|---|
static void |
copyStream(InputStream in,
OutputStream out)
Copy contents of
in to out . |
static int |
readFully(InputStream is,
byte[] bytes,
int off,
int len)
Reads a specified number of bytes from a stream.
|
static byte[] |
readInputStreamToByteArray(InputStream instream)
Read the contents of
is into a byte array. |
static String |
readInputStreamToString(InputStream instream,
Charset charset)
Form a string from the contents of
instream with charset
charset . |
static File |
writeToTempFile(InputStream in)
Write contents of
in to a temporary file. |
static File |
writeToTempFile(String string,
Charset charset)
Write contents of
string to a temporary file, encoded using charset . |
public static void copyStream(InputStream in, OutputStream out) throws IOException
in
to out
.in
- is source of bytesout
- is destination for bytesIOException
- if reading or writing failspublic static byte[] readInputStreamToByteArray(InputStream instream) throws IOException
is
into a byte array.instream
- to be readIOException
- when reading failspublic static String readInputStreamToString(InputStream instream, Charset charset) throws IOException
instream
with charset
charset
.instream
- to be readcharset
- is encoding of input stream's bytesIOException
- when reading failspublic static File writeToTempFile(InputStream in) throws IOException
in
to a temporary file. Caller is responsible for
deleting the temporary file after use.in
- written into fileIOException
- when writing file failspublic static File writeToTempFile(String string, Charset charset) throws IOException
string
to a temporary file, encoded using charset
. Caller is responsible for deleting the temporary file after use.string
- written into filecharset
- is encoding of written fileIOException
- when writing file failspublic static int readFully(InputStream is, byte[] bytes, int off, int len) throws IOException
is
- the stream from which the bytes will be read.bytes
- the buffer into which the data is read.off
- the start offset in array b
at which the data is written.len
- the number of bytes to read unless end of
stream is encountered-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 reaching end-of-stream, 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