Package org.h2.util

Class IOUtils

java.lang.Object
org.h2.util.IOUtils

public class IOUtils extends Object
This utility class contains input/output functions.
  • Constructor Details

    • IOUtils

      private IOUtils()
  • Method Details

    • closeSilently

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

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

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

      public static long copyAndClose(InputStream in, OutputStream out) throws 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:
      IOException - on failure
    • copyAndCloseInput

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

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

      public static long copy(InputStream in, OutputStream out, long length) throws 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:
      IOException - on failure
    • copyAndCloseInput

      public static long copyAndCloseInput(Reader in, Writer out, long length) throws 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:
      IOException - on failure
    • readBytesAndClose

      public static byte[] readBytesAndClose(InputStream in, int length) throws 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:
      IOException - on failure
    • readStringAndClose

      public static String readStringAndClose(Reader in, int length) throws 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:
      IOException - on failure
    • readFully

      public static int readFully(InputStream in, byte[] buffer, int max) throws 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:
      IOException - on failure
    • readFully

      public static int readFully(Reader in, char[] buffer, int max) throws 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:
      IOException - on failure
    • getReader

      public static Reader getReader(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 Writer getBufferedWriter(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 Reader getAsciiReader(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(String method, String fileName, 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 InputStream getInputStreamFromString(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(String original, String copy) throws 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:
      IOException - on failure
    • nameSeparatorsToNative

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