Module de.carne
Package de.carne.io

Class IOUtil


  • public final class IOUtil
    extends java.lang.Object
    Utility class providing I/O related functions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int copyBuffer​(java.io.OutputStream dst, java.nio.ByteBuffer buffer)
      Copies all available bytes from a ByteBuffer to a OutputStream.
      static long copyChannel​(java.nio.channels.WritableByteChannel dst, java.nio.channels.ReadableByteChannel src)
      Copies all bytes from a ReadableByteChannel to a WritableByteChannel.
      static long copyFile​(java.io.File dst, java.io.File src)
      Copies all bytes from a File to a File.
      static long copyFile​(java.io.OutputStream dst, java.io.File src)
      Copies all bytes from a File to an OutputStream.
      static long copyStream​(java.io.File dst, java.io.InputStream src)
      Copies all bytes from an InputStream to a File.
      static long copyStream​(java.io.OutputStream dst, java.io.InputStream src)
      Copies all bytes from an InputStream to an OutputStream.
      static long copyUrl​(java.io.File dst, java.net.URL src)
      Copies all bytes from a URL to a File.
      static long copyUrl​(java.io.OutputStream dst, java.net.URL src)
      Copies all bytes from a URL to an OutputStream.
      static byte[] readAllBytes​(java.io.File src)
      Read all bytes from a File.
      static byte[] readAllBytes​(java.io.File src, int limit)
      Read up to limit bytes from a File.
      static byte[] readAllBytes​(java.io.InputStream src)
      Read all bytes from an InputStream.
      static byte[] readAllBytes​(java.io.InputStream src, int limit)
      Read up to limit bytes from an InputStream.
      static byte[] readAllBytes​(java.net.URL src)
      Read all bytes from a URL.
      static byte[] readAllBytes​(java.net.URL src, int limit)
      Read up to limit bytes from a URL.
      static int readBlocking​(java.io.InputStream in, byte[] b)
      Reads up to the requested number of bytes and blocks until they are all read or EOF is reached.
      static int readBlocking​(java.io.InputStream in, byte[] b, int off, int len)
      Reads up to the requested number of bytes and blocks until they are all read or EOF is reached.
      static int readEager​(java.io.InputStream in, byte[] b)
      Reads eagerly the requested number of bytes and fails if they are not available.
      static int readEager​(java.io.InputStream in, byte[] b, int off, int len)
      Reads eagerly the requested number of bytes and fails if they are not available.
      • Methods inherited from class java.lang.Object

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

      • copyStream

        public static long copyStream​(java.io.OutputStream dst,
                                      java.io.InputStream src)
                               throws java.io.IOException
        Copies all bytes from an InputStream to an OutputStream.
        Parameters:
        dst - the OutputStream to copy to.
        src - the InputStream to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyStream

        public static long copyStream​(java.io.File dst,
                                      java.io.InputStream src)
                               throws java.io.IOException
        Copies all bytes from an InputStream to a File.
        Parameters:
        dst - the File to copy to.
        src - the InputStream to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyFile

        public static long copyFile​(java.io.OutputStream dst,
                                    java.io.File src)
                             throws java.io.IOException
        Copies all bytes from a File to an OutputStream.
        Parameters:
        dst - the OutputStream to copy to.
        src - the File to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyFile

        public static long copyFile​(java.io.File dst,
                                    java.io.File src)
                             throws java.io.IOException
        Copies all bytes from a File to a File.
        Parameters:
        dst - the File to copy to.
        src - the File to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyUrl

        public static long copyUrl​(java.io.OutputStream dst,
                                   java.net.URL src)
                            throws java.io.IOException
        Copies all bytes from a URL to an OutputStream.
        Parameters:
        dst - the OutputStream to copy to.
        src - the URL to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyUrl

        public static long copyUrl​(java.io.File dst,
                                   java.net.URL src)
                            throws java.io.IOException
        Copies all bytes from a URL to a File.
        Parameters:
        dst - the File to copy to.
        src - the URL to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyChannel

        public static long copyChannel​(java.nio.channels.WritableByteChannel dst,
                                       java.nio.channels.ReadableByteChannel src)
                                throws java.io.IOException
        Copies all bytes from a ReadableByteChannel to a WritableByteChannel.
        Parameters:
        dst - the WritableByteChannel to copy to.
        src - the ReadableByteChannel to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • copyBuffer

        public static int copyBuffer​(java.io.OutputStream dst,
                                     java.nio.ByteBuffer buffer)
                              throws java.io.IOException
        Copies all available bytes from a ByteBuffer to a OutputStream.
        Parameters:
        dst - the OutputStream to copy to.
        buffer - the ByteBuffer to copy from.
        Returns:
        the number of copied bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readAllBytes

        public static byte[] readAllBytes​(java.io.InputStream src)
                                   throws java.io.IOException
        Read all bytes from an InputStream.
        Parameters:
        src - the InputStream to read from.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readAllBytes

        public static byte[] readAllBytes​(java.io.InputStream src,
                                          int limit)
                                   throws java.io.IOException
        Read up to limit bytes from an InputStream.
        Parameters:
        src - the InputStream to read from.
        limit - the maximum number of bytes to read.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs or limit is reached.
      • readAllBytes

        public static byte[] readAllBytes​(java.io.File src)
                                   throws java.io.IOException
        Read all bytes from a File.
        Parameters:
        src - the File to read from.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readAllBytes

        public static byte[] readAllBytes​(java.io.File src,
                                          int limit)
                                   throws java.io.IOException
        Read up to limit bytes from a File.
        Parameters:
        src - the File to read from.
        limit - the maximum number of bytes to read.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs or limit is reached.
      • readAllBytes

        public static byte[] readAllBytes​(java.net.URL src)
                                   throws java.io.IOException
        Read all bytes from a URL.
        Parameters:
        src - the URL to read from.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readAllBytes

        public static byte[] readAllBytes​(java.net.URL src,
                                          int limit)
                                   throws java.io.IOException
        Read up to limit bytes from a URL.
        Parameters:
        src - the URL to read from.
        limit - the maximum number of bytes to read.
        Returns:
        the read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs or limit is reached.
      • readBlocking

        public static int readBlocking​(java.io.InputStream in,
                                       byte[] b)
                                throws java.io.IOException
        Reads up to the requested number of bytes and blocks until they are all read or EOF is reached.
        Parameters:
        in - the InputStream to read from.
        b - the buffer to read into.
        Returns:
        the number of read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readBlocking

        public static int readBlocking​(java.io.InputStream in,
                                       byte[] b,
                                       int off,
                                       int len)
                                throws java.io.IOException
        Reads up to the requested number of bytes and blocks until they are all read or EOF is reached.
        Parameters:
        in - the InputStream to read from.
        b - the buffer to read into.
        off - the buffer offset to use.
        len - the maximum number of bytes to read.
        Returns:
        the number of read bytes.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • readEager

        public static int readEager​(java.io.InputStream in,
                                    byte[] b)
                             throws java.io.IOException
        Reads eagerly the requested number of bytes and fails if they are not available.
        Parameters:
        in - the InputStream to read from.
        b - the buffer to read into.
        Returns:
        the number of read bytes (always the size of the buffer).
        Throws:
        java.io.IOException - if an I/O error occurs or a premature EOF has been reached.
      • readEager

        public static int readEager​(java.io.InputStream in,
                                    byte[] b,
                                    int off,
                                    int len)
                             throws java.io.IOException
        Reads eagerly the requested number of bytes and fails if they are not available.
        Parameters:
        in - the InputStream to read from.
        b - the buffer to read into.
        off - the buffer offset to use.
        len - the number of bytes to read.
        Returns:
        the number of read bytes (always the size of the buffer).
        Throws:
        java.io.IOException - if an I/O error occurs or a premature EOF has been reached.