Class IOUtils
- java.lang.Object
-
- io.microsphere.io.IOUtils
-
-
Field Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
The buffer size for I/Ostatic int
DEFAULT_BUFFER_SIZE
The default buffer size for I/O
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
close(java.io.Closeable closeable)
Closes the specifiedCloseable
object quietly, suppressing anyIOException
.static int
copy(java.io.InputStream in, java.io.OutputStream out)
Copies all data from the givenInputStream
to the specifiedOutputStream
.static int
copy(java.io.Reader reader, java.io.Writer writer)
Copies all characters from the givenReader
to the specifiedWriter
.static java.lang.String
copyToString(java.io.InputStream in)
Converts the content of the providedInputStream
into a newString
using the default charset (CharsetUtils.DEFAULT_CHARSET
).static java.lang.String
copyToString(java.io.InputStream in, java.lang.String encoding)
Copies the contents of the givenInputStream
into a newString
using the specified character encoding.static java.lang.String
copyToString(java.io.InputStream in, java.nio.charset.Charset charset)
Converts the content of the providedInputStream
into a newString
using the specified character set.static java.lang.String
copyToString(java.io.Reader reader)
Converts the content of the providedReader
into a newString
.static byte[]
toByteArray(java.io.InputStream in)
Copies the content of the givenInputStream
into a new byte array.static java.lang.String
toString(java.io.InputStream in)
Converts the content of the providedInputStream
into a newString
using the default charset.static java.lang.String
toString(java.io.InputStream in, java.lang.String encoding)
Converts the content of the providedInputStream
into a newString
using the specified character encoding.static java.lang.String
toString(java.io.InputStream in, java.nio.charset.Charset charset)
Converts the content of the providedInputStream
into a newString
using the specified character set.static java.lang.String
toString(java.io.Reader reader)
Converts the content of the providedReader
into a newString
.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size for I/O- See Also:
- Constant Field Values
-
BUFFER_SIZE
public static final int BUFFER_SIZE
The buffer size for I/O
-
-
Method Detail
-
toByteArray
public static byte[] toByteArray(java.io.InputStream in) throws java.io.IOException
Copies the content of the givenInputStream
into a new byte array.This method leaves the input stream open after copying is done.
Example Usage
InputStream inputStream = new FileInputStream("example.txt"); try { byte[] byteArray = IOUtils.toByteArray(inputStream); } finally { IOUtils.closeQuietly(inputStream); }
- Parameters:
in
- the InputStream to copy from (may benull
or empty)- Returns:
- a newly created byte array containing the copied data (possibly empty)
- Throws:
java.io.IOException
- if an I/O error occurs during copying
-
toString
public static java.lang.String toString(java.io.InputStream in) throws java.io.IOException
Converts the content of the providedInputStream
into a newString
using the default charset.This method internally uses
copyToString(InputStream)
to perform the conversion. It leaves the input stream open after the operation is complete.Example Usage
InputStream inputStream = getClass().getResourceAsStream("/example.txt"); try { String result = IOUtils.toString(inputStream); System.out.println(result); } finally { IOUtils.closeQuietly(inputStream); }
- Parameters:
in
- the InputStream to convert (may benull
or empty)- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream- See Also:
copyToString(InputStream)
-
toString
public static java.lang.String toString(java.io.InputStream in, java.lang.String encoding) throws java.io.IOException
Converts the content of the providedInputStream
into a newString
using the specified character encoding.This method internally uses
copyToString(InputStream, String)
to perform the conversion. It leaves the input stream open after the operation is complete.Example Usage
InputStream inputStream = getClass().getResourceAsStream("/example.txt"); try { String result = IOUtils.toString(inputStream, "UTF-8"); System.out.println(result); } finally { IOUtils.closeQuietly(inputStream); }
- Parameters:
in
- the InputStream to convert (may benull
or empty)encoding
- the character encoding to use; defaults to system file encoding if null or blank- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream- See Also:
copyToString(InputStream, String)
-
toString
public static java.lang.String toString(java.io.InputStream in, java.nio.charset.Charset charset) throws java.io.IOException
Converts the content of the providedInputStream
into a newString
using the specified character set.This method internally uses
copyToString(InputStream, Charset)
to perform the conversion. It leaves the input stream open after the operation is complete.Example Usage
InputStream inputStream = getClass().getResourceAsStream("/example.txt"); try { Charset charset = StandardCharsets.UTF_8; String result = IOUtils.toString(inputStream, charset); System.out.println(result); } finally { IOUtils.closeQuietly(inputStream); }
- Parameters:
in
- the InputStream to convert (may benull
or empty)charset
- the character set to use; defaults toCharsetUtils.DEFAULT_CHARSET
if null- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream- See Also:
copyToString(InputStream, Charset)
-
toString
public static java.lang.String toString(java.io.Reader reader) throws java.io.IOException
Converts the content of the providedReader
into a newString
.This method internally uses
copyToString(Reader)
to perform the conversion. It leaves the reader open after the operation is complete.Example Usage
Reader reader = new FileReader("example.txt"); try { String result = IOUtils.toString(reader); System.out.println(result); } finally { IOUtils.closeQuietly(reader); }
- Parameters:
reader
- the Reader to convert (may benull
or empty)- Returns:
- the resulting String from the reader, or
null
if the reader is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the reader- See Also:
copyToString(Reader)
-
copyToString
public static java.lang.String copyToString(java.io.InputStream in, java.lang.String encoding) throws java.io.IOException
Copies the contents of the givenInputStream
into a newString
using the specified character encoding.If the provided encoding is blank (null, empty, or whitespace-only), the system's default file encoding (
SystemUtils.FILE_ENCODING
) is used.Example Usage
try (InputStream inputStream = getClass().getResourceAsStream("/example.txt")) { String content = IOUtils.copyToString(inputStream, "UTF-8"); System.out.println(content); }
- Parameters:
in
- the InputStream to copy from (may benull
or empty)encoding
- the name of the character encoding to use; defaults to system file encoding if blank- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream
-
copyToString
public static java.lang.String copyToString(java.io.InputStream in) throws java.io.IOException
Converts the content of the providedInputStream
into a newString
using the default charset (CharsetUtils.DEFAULT_CHARSET
).This method internally uses
copyToString(InputStream, Charset)
to perform the conversion. It leaves the input stream open after the operation is complete.Example Usage
try (InputStream inputStream = getClass().getResourceAsStream("/example.txt")) { String result = IOUtils.copyToString(inputStream); System.out.println(result); }
- Parameters:
in
- the InputStream to convert (may benull
or empty)- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream- See Also:
copyToString(InputStream, Charset)
-
copyToString
public static java.lang.String copyToString(java.io.InputStream in, java.nio.charset.Charset charset) throws java.io.IOException
Converts the content of the providedInputStream
into a newString
using the specified character set.This method reads all available bytes from the input stream and decodes them into a string using the given charset. If the input stream is empty or null, this method returns null.
Example Usage
try (InputStream inputStream = getClass().getResourceAsStream("/example.txt")) { Charset charset = StandardCharsets.UTF_8; String result = IOUtils.copyToString(inputStream, charset); System.out.println(result); }
- Parameters:
in
- the InputStream to convert (may benull
or empty)charset
- the character set to use; defaults toCharsetUtils.DEFAULT_CHARSET
if null- Returns:
- the resulting String from the input stream, or
null
if the stream is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the stream
-
copyToString
public static java.lang.String copyToString(java.io.Reader reader) throws java.io.IOException
Converts the content of the providedReader
into a newString
.This method reads all available characters from the reader and appends them to a
StringBuilderWriter
, which is then converted to a String. If the reader is empty or null, this method returnsnull
.Example Usage
try (Reader reader = new FileReader("example.txt")) { String result = IOUtils.copyToString(reader); System.out.println(result); }
- Parameters:
reader
- the Reader to convert (may benull
or empty)- Returns:
- the resulting String from the reader, or
null
if the reader is empty or null - Throws:
java.io.IOException
- if an I/O error occurs during reading from the reader
-
copy
public static int copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
Copies all data from the givenInputStream
to the specifiedOutputStream
.This method uses a buffer of size
BUFFER_SIZE
for efficient copying. Both streams remain open after this operation. The total number of bytes copied is returned.If either stream is
null
, anIllegalArgumentException
will be thrown.Example Usage
try (InputStream inputStream = new FileInputStream("source.txt"); OutputStream outputStream = new FileOutputStream("destination.txt")) { int byteCount = IOUtils.copy(inputStream, outputStream); System.out.println("Copied " + byteCount + " bytes."); }
- Parameters:
in
- the source InputStream to read from; must not benull
out
- the target OutputStream to write to; must not benull
- Returns:
- the number of bytes copied from the input stream to the output stream
- Throws:
java.io.IOException
- if an I/O error occurs while reading or writing
-
copy
public static int copy(java.io.Reader reader, java.io.Writer writer) throws java.io.IOException
Copies all characters from the givenReader
to the specifiedWriter
.This method uses a buffer of size
BUFFER_SIZE
for efficient copying. Both the reader and writer remain open after this operation. The total number of characters copied is returned.If either the reader or writer is
null
, anIllegalArgumentException
will be thrown.Example Usage
try (Reader reader = new FileReader("input.txt"); Writer writer = new FileWriter("output.txt")) { int charCount = IOUtils.copy(reader, writer); System.out.println("Copied " + charCount + " characters."); }
- Parameters:
reader
- the source Reader to read from; must not benull
writer
- the target Writer to write to; must not benull
- Returns:
- the number of characters copied from the reader to the writer
- Throws:
java.io.IOException
- if an I/O error occurs while reading or writing
-
close
public static void close(java.io.Closeable closeable)
Closes the specifiedCloseable
object quietly, suppressing anyIOException
.If the provided
closeable
isnull
, this method does nothing. Any I/O errors that occur during closing are logged at the TRACE level.Example Usage
InputStream inputStream = null; try { inputStream = new FileInputStream("example.txt"); // perform operations with inputStream } catch (IOException e) { // handle exception } finally { IOUtils.close(inputStream); }
- Parameters:
closeable
- the Closeable object to be closed, may benull
-
-