Class IOUtil

java.lang.Object
nl.siegmann.epublib.util.IOUtil

public class IOUtil extends 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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static int
    calcNewNrReadSize(int nrRead, int totalNrNread)
    if totalNrRead invalid input: '<' 0 then totalNrRead is returned, if (nrRead + totalNrRead) invalid input: '<' Integer.MAX_VALUE then nrRead + totalNrRead is returned, -1 otherwise.
    static int
    Copies the contents of the InputStream to the OutputStream.
    static int
    copy(Reader in, Writer out)
    Copies the contents of the Reader to the Writer.
    static byte[]
    Returns the contents of the InputStream as a byte[]
    static byte[]
    toByteArray(InputStream in, int size)
    Reads data from the InputStream, using the specified buffer size.
    static byte[]
    toByteArray(Reader in, 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
  • Field Details

  • Constructor Details

    • IOUtil

      public IOUtil()
  • Method Details

    • toByteArray

      public static byte[] toByteArray(Reader in, String encoding) throws 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:
      IOException
    • toByteArray

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

      public static byte[] toByteArray(InputStream in, int size) throws 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:
      IOException
    • calcNewNrReadSize

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

      public static int copy(InputStream in, OutputStream out) throws 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:
      IOException
    • copy

      public static int copy(Reader in, Writer out) throws 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:
      IOException