org.h2.util
Class IOUtils

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

public class IOUtils
extends java.lang.Object

This utility class contains input/output functions.


Method Summary
static void closeSilently(java.io.InputStream in)
          Close an input stream without throwing an exception.
static void closeSilently(java.io.OutputStream out)
          Close an output stream without throwing an exception.
static void closeSilently(java.io.Reader reader)
          Close a reader without throwing an exception.
static void closeSilently(java.io.Writer writer)
          Close a writer 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 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)
          Copy all data from the reader to the writer and close the reader.
static java.io.Reader getAsciiReader(java.io.InputStream in)
          Wrap an input stream in a reader.
static java.io.InputStream getInputStream(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.io.Reader getReader(java.lang.String s)
          Create a reader to read from a string.
static java.io.Writer getWriter(java.io.OutputStream out)
          Create a buffered writer to write to an output stream using the UTF-8 format.
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 off, 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

closeSilently

public static void closeSilently(java.io.OutputStream out)
Close an output stream without throwing an exception.

Parameters:
out - the output stream 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

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
Returns:
the number of bytes copied
Throws:
java.io.IOException

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
Returns:
the number of bytes copied
Throws:
java.io.IOException

copyAndCloseInput

public static long copyAndCloseInput(java.io.Reader in,
                                     java.io.Writer out)
                              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
Returns:
the number of characters copied
Throws:
java.io.IOException

closeSilently

public static void closeSilently(java.io.InputStream in)
Close an input stream without throwing an exception.

Parameters:
in - the input stream or null

closeSilently

public static void closeSilently(java.io.Reader reader)
Close a reader without throwing an exception.

Parameters:
reader - the reader or null

closeSilently

public static void closeSilently(java.io.Writer writer)
Close a writer without throwing an exception.

Parameters:
writer - the writer or null

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

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

readFully

public static int readFully(java.io.InputStream in,
                            byte[] buffer,
                            int off,
                            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
off - the offset in the buffer
max - the number of bytes to read at most
Returns:
the number of bytes read
Throws:
java.io.IOException

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
Throws:
java.io.IOException

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.

Parameters:
in - the input stream or null
Returns:
the reader

getWriter

public static java.io.Writer getWriter(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

getInputStream

public static java.io.InputStream getInputStream(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

getReader

public static java.io.Reader getReader(java.lang.String s)
Create a reader to read from a string. If the string is null, this method returns null.

Parameters:
s - the string or null
Returns:
the reader

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