Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
close(Object... o)
Close all specified input streams, output streams, readers, and writers.
|
static void |
closeQuietly(InputStream is)
Close input stream and ignore any exceptions.
|
static void |
closeQuietly(Object... o)
Quietly close all specified input streams, output streams, readers, and writers.
|
static void |
closeQuietly(OutputStream os)
Close output stream and ignore any exceptions.
|
static void |
closeQuietly(Reader r)
Close reader and ignore any exceptions.
|
static void |
closeQuietly(Writer w)
Close writer and ignore any exceptions.
|
static long |
count(InputStream is)
Counts the number of bytes in the input stream and then closes the stream.
|
static long |
count(Reader r)
Counts the number of characters in the reader and then closes the reader.
|
static void |
flush(Object... o)
Flushes multiple output streams and writers in a single call.
|
static Reader |
getBufferedReader(Reader r)
Wraps the specified reader in a buffered reader.
|
static int |
getBufferSize(String contentLength)
Given the specified
|
static void |
pipe(Object in,
OutputStream out)
Pipes the contents of the specified object into the output stream.
|
static void |
pipe(Object in,
Writer out)
Pipes the contents of the specified object into the writer.
|
static String |
read(File in)
Reads the contents of a file into a string.
|
static String |
read(InputStream in)
Reads the contents of an input stream into a string using the system default charset.
|
static String |
read(InputStream in,
Charset cs)
Reads the contents of an input stream into a string using the specified charset.
|
static String |
read(Object... in)
Same as
read(Object) but appends all the input into a single String. |
static String |
read(Object in)
Reads the specified object to a
String . |
static String |
read(Reader in)
Reads the contents of a reader into a string.
|
static String |
read(Reader in,
int length,
int bufferSize)
Reads the specified input into a
String until the end of the input is reached. |
static byte[] |
readBytes(File in)
Read the specified file into a byte array.
|
static byte[] |
readBytes(File in,
int buffSize)
Read the specified file into a byte array.
|
static byte[] |
readBytes(InputStream in)
Read the specified input stream into a byte array.
|
static byte[] |
readBytes(InputStream in,
int buffSize)
Read the specified input stream into a byte array.
|
static byte[] |
readBytes(Object... in)
Same as
readBytes(Object) but appends all the input into a single byte array. |
static byte[] |
readBytes(Object in)
Shortcut for calling
readBytes(in, 1024); |
static byte[] |
readBytes(Object in,
int buffSize)
Read the specified object into a byte array.
|
static String |
readFile(String path)
Reads the contents of a file into a string.
|
static InputStream |
toInputStream(Object o)
Converts an object to an
InputStream . |
static Reader |
toReader(Object o)
Converts an object to a
Reader . |
static int |
write(File out,
InputStream in)
Writes the contents of the specified
InputStream to the specified file. |
static int |
write(File out,
Reader in)
Writes the contents of the specified
Reader to the specified file. |
static void |
write(String path,
String contents)
Writes the specified string to the specified file.
|
public IOUtils()
public static String readFile(String path) throws IOException
path
- The path of the file to read using default character encoding.IOException
- If a problem occurred trying to read from the reader.public static String read(Object in) throws IOException
String
.
Can be any of the following object types:
CharSequence
File
Reader
InputStream
byte []
in
- The object to read.IOException
public static String read(Object... in) throws IOException
read(Object)
but appends all the input into a single String.in
- The objects to read.IOException
public static String read(File in) throws IOException
in
- The file to read using default character encoding.IOException
- If a problem occurred trying to read from the reader.public static String read(Reader in) throws IOException
in
- The input reader.IOException
- If a problem occurred trying to read from the reader.public static String read(InputStream in, Charset cs) throws IOException
in
- The input stream.cs
- The charset of the contents of the input stream.IOException
- If a problem occurred trying to read from the input stream.public static String read(InputStream in) throws IOException
in
- The input stream.IOException
- If a problem occurred trying to read from the input stream.public static String read(Reader in, int length, int bufferSize) throws IOException
String
until the end of the input is reached.
The Reader
is automatically closed.
If the Reader
is not an instance of a BufferedReader
, then it gets wrapped in a
BufferedReader
.
in
- The input reader.length
- Specify a positive number if the length of the input is known.bufferSize
- Specify the buffer size to use.IOException
- If a problem occurred trying to read from the reader.public static byte[] readBytes(Object in, int buffSize) throws IOException
in
- The object to read into a byte array.
byte []
InputStream
Reader
CharSequence
File
buffSize
- The buffer size to use.IOException
- Thrown by underlying stream or if object is not a supported type.public static byte[] readBytes(InputStream in) throws IOException
in
- The stream to read into a byte array.IOException
- Thrown by underlying stream.public static byte[] readBytes(InputStream in, int buffSize) throws IOException
in
- The stream to read into a byte array.buffSize
- The buffer size to use.IOException
- Thrown by underlying stream.public static byte[] readBytes(File in) throws IOException
in
- The file to read into a byte array.IOException
- Thrown by underlying stream.public static byte[] readBytes(File in, int buffSize) throws IOException
in
- The file to read into a byte array.buffSize
- The buffer size to use.IOException
- Thrown by underlying stream.public static byte[] readBytes(Object in) throws IOException
readBytes(in, 1024);
in
- The object to read into a byte array.
byte []
InputStream
Reader
CharSequence
File
IOException
- Thrown by underlying stream or if object is not a supported type.public static byte[] readBytes(Object... in) throws IOException
readBytes(Object)
but appends all the input into a single byte array.in
- The objects to read.IOException
public static int write(File out, Reader in) throws IOException
Reader
to the specified file.out
- The file to write the output to.in
- The reader to pipe from.IOException
public static int write(File out, InputStream in) throws IOException
InputStream
to the specified file.out
- The file to write the output to.in
- The input stream to pipe from.IOException
public static void pipe(Object in, Writer out) throws IOException
The reader is closed, the writer is not.
in
- The input to pipe from.
Can be any of the types defined by toReader(Object)
.out
- The writer to pipe to.IOException
public static void pipe(Object in, OutputStream out) throws IOException
The input stream is closed, the output stream is not.
in
- The input to pipe from.
Can be any of the types defined by toInputStream(Object)
.out
- The writer to pipe to.IOException
public static Reader getBufferedReader(Reader r)
r
- The reader being wrapped.BufferedReader
, or the original Reader
if it's already a buffered
reader.public static long count(InputStream is) throws IOException
is
- The input stream to read from.IOException
public static long count(Reader r) throws IOException
r
- The reader to read from.IOException
public static int getBufferSize(String contentLength)
The maximum buffer size is 1MB.
contentLength
- The value of the public static void closeQuietly(InputStream is)
No-op if input stream is
is
- The input stream to close.public static void closeQuietly(OutputStream os)
No-op if output stream is
os
- The output stream to close.public static void closeQuietly(Reader r)
No-op if reader is
r
- The reader to close.public static void closeQuietly(Writer w)
No-op if writer is
w
- The writer to close.public static void closeQuietly(Object... o)
o
- The list of all objects to quietly close.public static void flush(Object... o) throws IOException
o
- The objects to flush.
IOException
public static void close(Object... o) throws IOException
o
- The list of all objects to close.
IOException
public static Reader toReader(Object o) throws IOException
Reader
.o
- The object to convert to a reader.
Can be any of the following:
InputStream
Reader
File
CharSequence
byte []
null
- Returns IOException
- If file could not be read.IllegalArgumentException
- If invalid object passed in.public static InputStream toInputStream(Object o) throws IOException
InputStream
.o
- The object to convert to an input stream.
Can be any of the following:
InputStream
Reader
File
CharSequence
- Converted to UTF-8 stream.
byte []
null
- Returns IOException
- If file could not be read.IllegalArgumentException
- If invalid object passed in.public static void write(String path, String contents) throws IOException
path
- The file path.contents
- The new file contents.IOException
Copyright © 2018 Apache. All rights reserved.