Class IOUtils

java.lang.Object
io.github.amanzat.util.IOUtils

public final class IOUtils extends Object
Miscellaneous I/O streams related utilities.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default buffer size (8192) to use in copy methods.
    static final int
    Represents the end-of-file (or stream).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    copy(InputStream inputStream, OutputStream outputStream)
    Copies bytes a InputStream to an OutputStream.
    static long
    copy(InputStream inputStream, OutputStream outputStream, byte[] buffer)
    Copies bytes from a InputStream to an OutputStream.
    static long
    copy(InputStream inputStream, OutputStream outputStream, int bufferSize)
    Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.
    toInputStream(String input, Charset charset)
    Converts the specified string to an input stream, encoded as bytes using the specified character encoding.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      The default buffer size (8192) to use in copy methods.
      See Also:
    • EOF

      public static final int EOF
      Represents the end-of-file (or stream).
      See Also:
  • Constructor Details

    • IOUtils

      public IOUtils()
  • Method Details

    • copy

      public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException
      Copies bytes a InputStream to an OutputStream.

      This method buffers the input internally, using the default buffer size: DEFAULT_BUFFER_SIZE.

      Parameters:
      inputStream - The InputStream to read.
      outputStream - The OutputStream to write.
      Returns:
      The number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • copy

      public static long copy(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException
      Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.
      Parameters:
      inputStream - The InputStream to read.
      outputStream - The OutputStream to write to
      bufferSize - The bufferSize used to copy from the input to the output
      Returns:
      The number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • copy

      public static long copy(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException
      Copies bytes from a InputStream to an OutputStream.

      This method uses the provided buffer, so there is no need to use a BufferedInputStream.

      Parameters:
      inputStream - The InputStream to read.
      outputStream - The OutputStream to write.
      buffer - The buffer to use for the copy
      Returns:
      The number of bytes copied.
      Throws:
      IOException - if an I/O error occurs.
    • toInputStream

      public static InputStream toInputStream(String input, Charset charset)
      Converts the specified string to an input stream, encoded as bytes using the specified character encoding.
      Parameters:
      input - The string to convert
      charset - The charset to use, null allowed and transformed to default charset.
      Returns:
      An input stream.