Class IOUtils
- java.lang.Object
-
- io.microsphere.io.IOUtils
-
-
Field Summary
Fields Modifier and Type Field Description static intBUFFER_SIZEThe buffer size for I/Ostatic java.lang.StringBUFFER_SIZE_PROPERTY_NAMEThe property name used to configure the I/O buffer size("microsphere.io.buffer.size").static intDEFAULT_BUFFER_SIZEThe default buffer size for I/O
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclose(java.io.Closeable closeable)Closes the specifiedCloseableobject quietly, suppressing anyIOException.static intcopy(java.io.InputStream in, java.io.OutputStream out)Copies all data from the givenInputStreamto the specifiedOutputStream.static intcopy(java.io.Reader reader, java.io.Writer writer)Copies all characters from the givenReaderto the specifiedWriter.static java.lang.StringcopyToString(java.io.InputStream in)Converts the content of the providedInputStreaminto a newStringusing the default charset (CharsetUtils.DEFAULT_CHARSET).static java.lang.StringcopyToString(java.io.InputStream in, java.lang.String encoding)Copies the contents of the givenInputStreaminto a newStringusing the specified character encoding.static java.lang.StringcopyToString(java.io.InputStream in, java.nio.charset.Charset charset)Converts the content of the providedInputStreaminto a newStringusing the specified character set.static java.lang.StringcopyToString(java.io.Reader reader)Converts the content of the providedReaderinto a newString.static byte[]toByteArray(java.io.InputStream in)Copies the content of the givenInputStreaminto a new byte array.static java.lang.StringtoString(java.io.InputStream in)Converts the content of the providedInputStreaminto a newStringusing the default charset.static java.lang.StringtoString(java.io.InputStream in, java.lang.String encoding)Converts the content of the providedInputStreaminto a newStringusing the specified character encoding.static java.lang.StringtoString(java.io.InputStream in, java.nio.charset.Charset charset)Converts the content of the providedInputStreaminto a newStringusing the specified character set.static java.lang.StringtoString(java.io.Reader reader)Converts the content of the providedReaderinto a newString.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size for I/O
-
BUFFER_SIZE_PROPERTY_NAME
public static final java.lang.String BUFFER_SIZE_PROPERTY_NAME
The property name used to configure the I/O buffer size("microsphere.io.buffer.size").This property can be set in system properties or configuration files to customize the default buffer size used by I/O operations within the MicroSphere framework.
Example Usage
// Set the custom buffer size property System.setProperty(IOUtils.BUFFER_SIZE_PROPERTY_NAME, "4096");- See Also:
- Constant Field Values
-
BUFFER_SIZE
@ConfigurationProperty(name="microsphere.io.buffer.size", defaultValue="2048", description="The buffer size for I/O", source="system-properties") public static final int BUFFER_SIZEThe buffer size for I/O
-
-
Method Detail
-
toByteArray
public static byte[] toByteArray(java.io.InputStream in) throws java.io.IOExceptionCopies the content of the givenInputStreaminto 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 benullor 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.IOExceptionConverts the content of the providedInputStreaminto a newStringusing 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 benullor empty)- Returns:
- the resulting String from the input stream, or
nullif 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.IOExceptionConverts the content of the providedInputStreaminto a newStringusing 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 benullor 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
nullif 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.IOExceptionConverts the content of the providedInputStreaminto a newStringusing 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 benullor empty)charset- the character set to use; defaults toCharsetUtils.DEFAULT_CHARSETif null- Returns:
- the resulting String from the input stream, or
nullif 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.IOExceptionConverts the content of the providedReaderinto 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 benullor empty)- Returns:
- the resulting String from the reader, or
nullif 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.IOExceptionCopies the contents of the givenInputStreaminto a newStringusing 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 benullor 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
nullif 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.IOExceptionConverts the content of the providedInputStreaminto a newStringusing 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 benullor empty)- Returns:
- the resulting String from the input stream, or
nullif 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.IOExceptionConverts the content of the providedInputStreaminto a newStringusing 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 benullor empty)charset- the character set to use; defaults toCharsetUtils.DEFAULT_CHARSETif null- Returns:
- the resulting String from the input stream, or
nullif 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.IOExceptionConverts the content of the providedReaderinto 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 benullor empty)- Returns:
- the resulting String from the reader, or
nullif 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.IOExceptionCopies all data from the givenInputStreamto the specifiedOutputStream.This method uses a buffer of size
BUFFER_SIZEfor efficient copying. Both streams remain open after this operation. The total number of bytes copied is returned.If either stream is
null, anIllegalArgumentExceptionwill 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 benullout- 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.IOExceptionCopies all characters from the givenReaderto the specifiedWriter.This method uses a buffer of size
BUFFER_SIZEfor 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, anIllegalArgumentExceptionwill 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 benullwriter- 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 specifiedCloseableobject quietly, suppressing anyIOException.If the provided
closeableisnull, 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
-
-