Package org.h2.util

Class IOUtils


  • public class IOUtils
    extends java.lang.Object
    This utility class contains input/output functions.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IOUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeSilently​(java.lang.AutoCloseable out)
      Close an AutoCloseable without throwing an exception.
      static long copy​(java.io.InputStream in, java.io.OutputStream out)
      Copy all data from the input stream to the output stream.
      static long copy​(java.io.InputStream in, java.io.OutputStream out, long length)
      Copy all data from the input stream to the output stream.
      static long copyAndClose​(java.io.InputStream in, java.io.OutputStream out)
      Copy all data from the input stream to the output stream and close both streams.
      static long copyAndCloseInput​(java.io.InputStream in, java.io.OutputStream out)
      Copy all data from the input stream to the output stream and close the input stream.
      static long copyAndCloseInput​(java.io.Reader in, java.io.Writer out, long length)
      Copy all data from the reader to the writer and close the reader.
      static void copyFiles​(java.lang.String original, java.lang.String copy)
      Copy a file from one directory to another, or to another file.
      static java.io.Reader getAsciiReader​(java.io.InputStream in)
      Wrap an input stream in a reader.
      static java.io.Writer getBufferedWriter​(java.io.OutputStream out)
      Create a buffered writer to write to an output stream using the UTF-8 format.
      static java.io.InputStream getInputStreamFromString​(java.lang.String s)
      Create an input stream to read from a string.
      static java.io.Reader getReader​(java.io.InputStream in)
      Create a reader to read from an input stream using the UTF-8 format.
      static java.lang.String nameSeparatorsToNative​(java.lang.String path)
      Converts / and \ name separators in path to native separators.
      static byte[] readBytesAndClose​(java.io.InputStream in, int length)
      Read a number of bytes from an input stream and close the stream.
      static int readFully​(java.io.InputStream in, byte[] buffer, int max)
      Try to read the given number of bytes to the buffer.
      static int readFully​(java.io.Reader in, char[] buffer, int max)
      Try to read the given number of characters to the buffer.
      static java.lang.String readStringAndClose​(java.io.Reader in, int length)
      Read a number of characters from a reader and close it.
      static void skipFully​(java.io.InputStream in, long skip)
      Skip a number of bytes in an input stream.
      static void skipFully​(java.io.Reader reader, long skip)
      Skip a number of characters in a reader.
      static void trace​(java.lang.String method, java.lang.String fileName, java.lang.Object o)
      Trace input or output operations if enabled.
      • Methods inherited from class java.lang.Object

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

      • IOUtils

        private IOUtils()
    • Method Detail

      • closeSilently

        public static void closeSilently​(java.lang.AutoCloseable out)
        Close an AutoCloseable without throwing an exception.
        Parameters:
        out - the AutoCloseable or null
      • skipFully

        public static void skipFully​(java.io.InputStream in,
                                     long skip)
                              throws java.io.IOException
        Skip a number of bytes in an input stream.
        Parameters:
        in - the input stream
        skip - the number of bytes to skip
        Throws:
        java.io.EOFException - if the end of file has been reached before all bytes could be skipped
        java.io.IOException - if an IO exception occurred while skipping
      • skipFully

        public static void skipFully​(java.io.Reader reader,
                                     long skip)
                              throws java.io.IOException
        Skip a number of characters in a reader.
        Parameters:
        reader - the reader
        skip - the number of characters to skip
        Throws:
        java.io.EOFException - if the end of file has been reached before all characters could be skipped
        java.io.IOException - if an IO exception occurred while skipping
      • copyAndClose

        public static long copyAndClose​(java.io.InputStream in,
                                        java.io.OutputStream out)
                                 throws java.io.IOException
        Copy all data from the input stream to the output stream and close both streams. Exceptions while closing are ignored.
        Parameters:
        in - the input stream
        out - the output stream
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - on failure
      • copyAndCloseInput

        public static long copyAndCloseInput​(java.io.InputStream in,
                                             java.io.OutputStream out)
                                      throws java.io.IOException
        Copy all data from the input stream to the output stream and close the input stream. Exceptions while closing are ignored.
        Parameters:
        in - the input stream
        out - the output stream (null if writing is not required)
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - on failure
      • copy

        public static long copy​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy all data from the input stream to the output stream. Both streams are kept open.
        Parameters:
        in - the input stream
        out - the output stream (null if writing is not required)
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - on failure
      • copy

        public static long copy​(java.io.InputStream in,
                                java.io.OutputStream out,
                                long length)
                         throws java.io.IOException
        Copy all data from the input stream to the output stream. Both streams are kept open.
        Parameters:
        in - the input stream
        out - the output stream (null if writing is not required)
        length - the maximum number of bytes to copy
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - on failure
      • copyAndCloseInput

        public static long copyAndCloseInput​(java.io.Reader in,
                                             java.io.Writer out,
                                             long length)
                                      throws java.io.IOException
        Copy all data from the reader to the writer and close the reader. Exceptions while closing are ignored.
        Parameters:
        in - the reader
        out - the writer (null if writing is not required)
        length - the maximum number of bytes to copy
        Returns:
        the number of characters copied
        Throws:
        java.io.IOException - on failure
      • readBytesAndClose

        public static byte[] readBytesAndClose​(java.io.InputStream in,
                                               int length)
                                        throws java.io.IOException
        Read a number of bytes from an input stream and close the stream.
        Parameters:
        in - the input stream
        length - the maximum number of bytes to read, or -1 to read until the end of file
        Returns:
        the bytes read
        Throws:
        java.io.IOException - on failure
      • readStringAndClose

        public static java.lang.String readStringAndClose​(java.io.Reader in,
                                                          int length)
                                                   throws java.io.IOException
        Read a number of characters from a reader and close it.
        Parameters:
        in - the reader
        length - the maximum number of characters to read, or -1 to read until the end of file
        Returns:
        the string read
        Throws:
        java.io.IOException - on failure
      • readFully

        public static int readFully​(java.io.InputStream in,
                                    byte[] buffer,
                                    int max)
                             throws java.io.IOException
        Try to read the given number of bytes to the buffer. This method reads until the maximum number of bytes have been read or until the end of file.
        Parameters:
        in - the input stream
        buffer - the output buffer
        max - the number of bytes to read at most
        Returns:
        the number of bytes read, 0 meaning EOF
        Throws:
        java.io.IOException - on failure
      • readFully

        public static int readFully​(java.io.Reader in,
                                    char[] buffer,
                                    int max)
                             throws java.io.IOException
        Try to read the given number of characters to the buffer. This method reads until the maximum number of characters have been read or until the end of file.
        Parameters:
        in - the reader
        buffer - the output buffer
        max - the number of characters to read at most
        Returns:
        the number of characters read, 0 meaning EOF
        Throws:
        java.io.IOException - on failure
      • getReader

        public static java.io.Reader getReader​(java.io.InputStream in)
        Create a reader to read from an input stream using the UTF-8 format. If the input stream is null, this method returns null. The InputStreamReader that is used here is not exact, that means it may read some additional bytes when buffering.
        Parameters:
        in - the input stream or null
        Returns:
        the reader
      • getBufferedWriter

        public static java.io.Writer getBufferedWriter​(java.io.OutputStream out)
        Create a buffered writer to write to an output stream using the UTF-8 format. If the output stream is null, this method returns null.
        Parameters:
        out - the output stream or null
        Returns:
        the writer
      • getAsciiReader

        public static java.io.Reader getAsciiReader​(java.io.InputStream in)
        Wrap an input stream in a reader. The bytes are converted to characters using the US-ASCII character set.
        Parameters:
        in - the input stream
        Returns:
        the reader
      • trace

        public static void trace​(java.lang.String method,
                                 java.lang.String fileName,
                                 java.lang.Object o)
        Trace input or output operations if enabled.
        Parameters:
        method - the method from where this method was called
        fileName - the file name
        o - the object to append to the message
      • getInputStreamFromString

        public static java.io.InputStream getInputStreamFromString​(java.lang.String s)
        Create an input stream to read from a string. The string is converted to a byte array using UTF-8 encoding. If the string is null, this method returns null.
        Parameters:
        s - the string
        Returns:
        the input stream
      • copyFiles

        public static void copyFiles​(java.lang.String original,
                                     java.lang.String copy)
                              throws java.io.IOException
        Copy a file from one directory to another, or to another file.
        Parameters:
        original - the original file name
        copy - the file name of the copy
        Throws:
        java.io.IOException - on failure
      • nameSeparatorsToNative

        public static java.lang.String nameSeparatorsToNative​(java.lang.String path)
        Converts / and \ name separators in path to native separators.
        Parameters:
        path - path to convert
        Returns:
        path with converted separators