Class StreamUtil


  • public final class StreamUtil
    extends java.lang.Object
    Stream util class
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long channelCopy​(java.io.InputStream src, java.io.OutputStream dest)
      This method copies data from the src and writes it to the dest until EOF on src.
      long channelCopy​(java.nio.channels.ReadableByteChannel src, java.nio.channels.WritableByteChannel dest)
      This method copies data from the src channel and writes it to the dest channel until EOF on src.
      void close​(java.io.Closeable closeable)
      Close silent a stream
      java.io.ByteArrayOutputStream convertStreamTo​(java.io.InputStream is)
      Convert the InputStream to String we use the Reader.read(char[] buffer) method.
      java.io.InputStream convertStreamToNewInputStream​(java.io.InputStream is)
      Convert the InputStream to String we use the Reader.read(char[] buffer) method.
      java.lang.String convertStreamToStr​(java.io.InputStream is)
      Convert the InputStream to String we use the Reader.read(char[] buffer) method.
      java.lang.String convertStreamToStr​(java.io.InputStream is, java.lang.String charsetName)
      Convert the InputStream to String we use the Reader.read(char[] buffer) method.
      static StreamUtil getInstance()
      Get the instance
      • Methods inherited from class java.lang.Object

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

      • getInstance

        public static StreamUtil getInstance()
        Get the instance
        Returns:
        the instance
      • convertStreamTo

        public java.io.ByteArrayOutputStream convertStreamTo​(java.io.InputStream is)
                                                      throws java.io.IOException
        Convert the InputStream to String we use the Reader.read(char[] buffer) method. We iterate until the Reader return -1 which means there's no more data to read.
        Parameters:
        is - the input stream
        Returns:
        the result
        Throws:
        java.io.IOException - in case of error
      • convertStreamToStr

        public java.lang.String convertStreamToStr​(java.io.InputStream is)
                                            throws java.io.IOException
        Convert the InputStream to String we use the Reader.read(char[] buffer) method. We iterate until the Reader return -1 which means there's no more data to read.
        Parameters:
        is - the input stream
        Returns:
        the result
        Throws:
        java.io.IOException - in case of error
      • convertStreamToStr

        public java.lang.String convertStreamToStr​(java.io.InputStream is,
                                                   java.lang.String charsetName)
                                            throws java.io.IOException
        Convert the InputStream to String we use the Reader.read(char[] buffer) method. We iterate until the Reader return -1 which means there's no more data to read.
        Parameters:
        is - the input stream
        charsetName - the charset name
        Returns:
        the result
        Throws:
        java.io.IOException - in case of error
      • convertStreamToNewInputStream

        public java.io.InputStream convertStreamToNewInputStream​(java.io.InputStream is)
                                                          throws java.io.IOException
        Convert the InputStream to String we use the Reader.read(char[] buffer) method. We iterate until the Reader return -1 which means there's no more data to read.
        Parameters:
        is - the input stream
        Returns:
        the result
        Throws:
        java.io.IOException - in case of error
      • channelCopy

        public long channelCopy​(java.io.InputStream src,
                                java.io.OutputStream dest)
                         throws java.io.IOException
        This method copies data from the src and writes it to the dest until EOF on src.
        Parameters:
        src - the source stream
        dest - the destination stream
        Returns:
        the copied bytes
        Throws:
        java.io.IOException - in case of error
      • channelCopy

        public long channelCopy​(java.nio.channels.ReadableByteChannel src,
                                java.nio.channels.WritableByteChannel dest)
                         throws java.io.IOException
        This method copies data from the src channel and writes it to the dest channel until EOF on src. This implementation makes use of compact() on the temp buffer to pack down the data if the buffer wasn't fully drained. This may result in data copying, but minimizes system calls. It also requires a cleanup loop to make sure all the data gets sent.
        Parameters:
        src - the source channel
        dest - the destination channel
        Returns:
        the copied bytes
        Throws:
        java.io.IOException - in case of error
      • close

        public void close​(java.io.Closeable closeable)
        Close silent a stream
        Parameters:
        closeable - close