Class IOUtils

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE
      The buffer size for I/O
      static java.lang.String BUFFER_SIZE_PROPERTY_NAME
      The property name used to configure the I/O buffer size("microsphere.io.buffer.size").
      static 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 specified Closeable object quietly, suppressing any IOException.
      static int copy​(java.io.InputStream in, java.io.OutputStream out)
      Copies all data from the given InputStream to the specified OutputStream.
      static int copy​(java.io.Reader reader, java.io.Writer writer)
      Copies all characters from the given Reader to the specified Writer.
      static java.lang.String copyToString​(java.io.InputStream in)
      Converts the content of the provided InputStream into a new String 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 given InputStream into a new String using the specified character encoding.
      static java.lang.String copyToString​(java.io.InputStream in, java.nio.charset.Charset charset)
      Converts the content of the provided InputStream into a new String using the specified character set.
      static java.lang.String copyToString​(java.io.Reader reader)
      Converts the content of the provided Reader into a new String.
      static byte[] toByteArray​(java.io.InputStream in)
      Copies the content of the given InputStream into a new byte array.
      static java.lang.String toString​(java.io.InputStream in)
      Converts the content of the provided InputStream into a new String using the default charset.
      static java.lang.String toString​(java.io.InputStream in, java.lang.String encoding)
      Converts the content of the provided InputStream into a new String using the specified character encoding.
      static java.lang.String toString​(java.io.InputStream in, java.nio.charset.Charset charset)
      Converts the content of the provided InputStream into a new String using the specified character set.
      static java.lang.String toString​(java.io.Reader reader)
      Converts the content of the provided Reader into a new String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Method Detail

      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream in)
                                  throws java.io.IOException
        Copies the content of the given InputStream 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 be null 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 provided InputStream into a new String 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 be null 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 provided InputStream into a new String 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 be null 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 provided InputStream into a new String 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 be null or empty)
        charset - the character set to use; defaults to CharsetUtils.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 provided Reader into a new String.

        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 be null 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 given InputStream into a new String 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 be null 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 provided InputStream into a new String 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 be null 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 provided InputStream into a new String 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 be null or empty)
        charset - the character set to use; defaults to CharsetUtils.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 provided Reader into a new String.

        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 returns null.

        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 be null 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 given InputStream to the specified OutputStream.

        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, an IllegalArgumentException 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 be null
        out - the target OutputStream to write to; must not be null
        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 given Reader to the specified Writer.

        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, an IllegalArgumentException 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 be null
        writer - the target Writer to write to; must not be null
        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 specified Closeable object quietly, suppressing any IOException.

        If the provided closeable is null, 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 be null