Package yakworks.util

Class StreamUtils


  • public abstract class StreamUtils
    extends java.lang.Object
    Simple utility methods for dealing with streams. The copy methods of this class are similar to those defined in FileCopyUtils except that all affected streams are left open when done. All copy methods use a block size of 4096 bytes.

    Mainly for use within the framework, but also useful for application code.

    Since:
    3.2.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE
      The default buffer size used when copying bytes.
    • Constructor Summary

      Constructors 
      Constructor Description
      StreamUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copy​(byte[] in, java.io.OutputStream out)
      Copy the contents of the given byte array to the given OutputStream.
      static int copy​(java.io.InputStream in, java.io.OutputStream out)
      Copy the contents of the given InputStream to the given OutputStream.
      static void copy​(java.lang.String in, java.nio.charset.Charset charset, java.io.OutputStream out)
      Copy the contents of the given String to the given OutputStream.
      static long copyRange​(java.io.InputStream in, java.io.OutputStream out, long start, long end)
      Copy a range of content of the given InputStream to the given OutputStream.
      static byte[] copyToByteArray​(java.io.InputStream in)
      Copy the contents of the given InputStream into a new byte array.
      static java.lang.String copyToString​(java.io.ByteArrayOutputStream baos, java.nio.charset.Charset charset)
      Copy the contents of the given ByteArrayOutputStream into a String.
      static java.lang.String copyToString​(java.io.InputStream in, java.nio.charset.Charset charset)
      Copy the contents of the given InputStream into a String.
      static int drain​(java.io.InputStream in)
      Drain the remaining content of the given InputStream.
      static java.io.InputStream emptyInput()
      Return an efficient empty InputStream.
      static java.io.InputStream nonClosing​(java.io.InputStream in)
      Return a variant of the given InputStream where calling close() has no effect.
      static java.io.OutputStream nonClosing​(java.io.OutputStream out)
      Return a variant of the given OutputStream where calling close() has no effect.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BUFFER_SIZE

        public static final int BUFFER_SIZE
        The default buffer size used when copying bytes.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StreamUtils

        public StreamUtils()
    • Method Detail

      • copyToByteArray

        public static byte[] copyToByteArray​(@Nullable
                                             java.io.InputStream in)
                                      throws java.io.IOException
        Copy the contents of the given InputStream into a new byte array.

        Leaves the stream open when done.

        Parameters:
        in - the stream to copy from (may be null or empty)
        Returns:
        the new byte array that has been copied to (possibly empty)
        Throws:
        java.io.IOException - in case of I/O errors
      • copyToString

        public static java.lang.String copyToString​(@Nullable
                                                    java.io.InputStream in,
                                                    java.nio.charset.Charset charset)
                                             throws java.io.IOException
        Copy the contents of the given InputStream into a String.

        Leaves the stream open when done.

        Parameters:
        in - the InputStream to copy from (may be null or empty)
        charset - the Charset to use to decode the bytes
        Returns:
        the String that has been copied to (possibly empty)
        Throws:
        java.io.IOException - in case of I/O errors
      • copyToString

        public static java.lang.String copyToString​(java.io.ByteArrayOutputStream baos,
                                                    java.nio.charset.Charset charset)
        Copy the contents of the given ByteArrayOutputStream into a String.

        This is a more effective equivalent of new String(baos.toByteArray(), charset).

        Parameters:
        baos - the ByteArrayOutputStream to be copied into a String
        charset - the Charset to use to decode the bytes
        Returns:
        the String that has been copied to (possibly empty)
        Since:
        5.2.6
      • copy

        public static void copy​(byte[] in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy the contents of the given byte array to the given OutputStream.

        Leaves the stream open when done.

        Parameters:
        in - the byte array to copy from
        out - the OutputStream to copy to
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static void copy​(java.lang.String in,
                                java.nio.charset.Charset charset,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy the contents of the given String to the given OutputStream.

        Leaves the stream open when done.

        Parameters:
        in - the String to copy from
        charset - the Charset
        out - the OutputStream to copy to
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static int copy​(java.io.InputStream in,
                               java.io.OutputStream out)
                        throws java.io.IOException
        Copy the contents of the given InputStream to the given OutputStream.

        Leaves both streams open when done.

        Parameters:
        in - the InputStream to copy from
        out - the OutputStream to copy to
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - in case of I/O errors
      • copyRange

        public static long copyRange​(java.io.InputStream in,
                                     java.io.OutputStream out,
                                     long start,
                                     long end)
                              throws java.io.IOException
        Copy a range of content of the given InputStream to the given OutputStream.

        If the specified range exceeds the length of the InputStream, this copies up to the end of the stream and returns the actual number of copied bytes.

        Leaves both streams open when done.

        Parameters:
        in - the InputStream to copy from
        out - the OutputStream to copy to
        start - the position to start copying from
        end - the position to end copying
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - in case of I/O errors
        Since:
        4.3
      • drain

        public static int drain​(java.io.InputStream in)
                         throws java.io.IOException
        Drain the remaining content of the given InputStream.

        Leaves the InputStream open when done.

        Parameters:
        in - the InputStream to drain
        Returns:
        the number of bytes read
        Throws:
        java.io.IOException - in case of I/O errors
        Since:
        4.3
      • emptyInput

        public static java.io.InputStream emptyInput()
        Return an efficient empty InputStream.
        Returns:
        a ByteArrayInputStream based on an empty byte array
        Since:
        4.2.2
      • nonClosing

        public static java.io.InputStream nonClosing​(java.io.InputStream in)
        Return a variant of the given InputStream where calling close() has no effect.
        Parameters:
        in - the InputStream to decorate
        Returns:
        a version of the InputStream that ignores calls to close
      • nonClosing

        public static java.io.OutputStream nonClosing​(java.io.OutputStream out)
        Return a variant of the given OutputStream where calling close() has no effect.
        Parameters:
        out - the OutputStream to decorate
        Returns:
        a version of the OutputStream that ignores calls to close