Class IOUtil


  • public class IOUtil
    extends java.lang.Object
    Most of the functions herein are re-implementations of the ones in apache io IOUtils. The reason for re-implementing this is that the functions are fairly simple and using my own implementation saves the inclusion of a 200Kb jar file.
    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static int calcNewNrReadSize​(int nrRead, int totalNrNread)
      if totalNrRead < 0 then totalNrRead is returned, if (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead is returned, -1 otherwise.
      static int copy​(java.io.InputStream in, java.io.OutputStream out)
      Copies the contents of the InputStream to the OutputStream.
      static int copy​(java.io.Reader in, java.io.Writer out)
      Copies the contents of the Reader to the Writer.
      static byte[] toByteArray​(java.io.InputStream in)
      Returns the contents of the InputStream as a byte[]
      static byte[] toByteArray​(java.io.InputStream in, int size)
      Reads data from the InputStream, using the specified buffer size.
      static byte[] toByteArray​(java.io.Reader in, java.lang.String encoding)
      Gets the contents of the Reader as a byte[], with the given character encoding.
      • Methods inherited from class java.lang.Object

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

      • IOUtil

        public IOUtil()
    • Method Detail

      • toByteArray

        public static byte[] toByteArray​(java.io.Reader in,
                                         java.lang.String encoding)
                                  throws java.io.IOException
        Gets the contents of the Reader as a byte[], with the given character encoding.
        Parameters:
        in -
        encoding -
        Returns:
        the contents of the Reader as a byte[], with the given character encoding.
        Throws:
        java.io.IOException
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream in)
                                  throws java.io.IOException
        Returns the contents of the InputStream as a byte[]
        Parameters:
        in -
        Returns:
        the contents of the InputStream as a byte[]
        Throws:
        java.io.IOException
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream in,
                                         int size)
                                  throws java.io.IOException
        Reads data from the InputStream, using the specified buffer size. This is meant for situations where memory is tight, since it prevents buffer expansion.
        Parameters:
        in - the stream to read data from
        size - the size of the array to create
        Returns:
        the array, or null
        Throws:
        java.io.IOException
      • calcNewNrReadSize

        protected static int calcNewNrReadSize​(int nrRead,
                                               int totalNrNread)
        if totalNrRead < 0 then totalNrRead is returned, if (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead is returned, -1 otherwise.
        Parameters:
        nrRead -
        totalNrNread -
        Returns:
        if totalNrRead < 0 then totalNrRead is returned, if (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead is returned, -1 otherwise.
      • copy

        public static int copy​(java.io.InputStream in,
                               java.io.OutputStream out)
                        throws java.io.IOException
        Copies the contents of the InputStream to the OutputStream.
        Parameters:
        in -
        out -
        Returns:
        the nr of bytes read, or -1 if the amount > Integer.MAX_VALUE
        Throws:
        java.io.IOException
      • copy

        public static int copy​(java.io.Reader in,
                               java.io.Writer out)
                        throws java.io.IOException
        Copies the contents of the Reader to the Writer.
        Parameters:
        in -
        out -
        Returns:
        the nr of characters read, or -1 if the amount > Integer.MAX_VALUE
        Throws:
        java.io.IOException