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 boolean canWrite(java.lang.String fileName)
          Check if the file is writable.
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 copy(java.io.InputStream in, java.io.OutputStream out, long length)
          Copy all data from the input stream to the output stream.
static void copy(java.lang.String original, java.lang.String copy)
          Copy a file from one directory to another, or to another file.
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 createDirs(java.lang.String fileName)
          Create all required directories that are required for this file.
static boolean createNewFile(java.lang.String fileName)
          Create a new file.
static java.lang.String createTempFile(java.lang.String prefix, java.lang.String suffix, boolean deleteOnExit, boolean inTempDir)
          Create a new temporary file.
static void delete(java.lang.String fileName)
          Delete a file.
static void deleteRecursive(java.lang.String directory, boolean tryOnly)
          Delete a directory or file and all subdirectories and files.
static boolean exists(java.lang.String fileName)
          Checks if a file exists.
static boolean fileStartsWith(java.lang.String fileName, java.lang.String prefix)
          Check if a file starts with a given prefix.
static java.io.Reader getAsciiReader(java.io.InputStream in)
          Wrap an input stream in a reader.
static java.io.Reader getBufferedReader(java.io.InputStream in)
          Create a buffered reader to read from an input stream using the UTF-8 format.
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.lang.String getCanonicalPath(java.lang.String fileName)
          Normalize a file name.
static java.lang.String getFileName(java.lang.String name)
          Get the file name (without directory part).
static java.io.InputStream getInputStreamFromString(java.lang.String s)
          Create an input stream to read from a string.
static long getLastModified(java.lang.String fileName)
          Get the last modified date of a file.
static java.lang.String getParent(java.lang.String fileName)
          Get the parent directory of a file or directory.
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 getReaderFromString(java.lang.String s)
          Create a reader to read from a string.
static boolean isAbsolute(java.lang.String fileName)
          Check if the file name includes a path.
static boolean isDirectory(java.lang.String fileName)
          Check if it is a file or a directory.
static boolean isReadOnly(java.lang.String fileName)
          Check if a file is read-only.
static long length(java.lang.String fileName)
          Get the length of a file.
static java.lang.String[] listFiles(java.lang.String path)
          List the files in the given directory.
static void mkdirs(java.io.File directory)
          Create the directory and all parent directories if required.
static java.io.InputStream openFileInputStream(java.lang.String fileName)
          Create an input stream to read from the file.
static FileObject openFileObject(java.lang.String fileName, java.lang.String mode)
          Open a random access file object.
static java.io.OutputStream openFileOutputStream(java.lang.String fileName, boolean append)
          Create an output stream to write into the file.
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 rename(java.lang.String oldName, java.lang.String newName)
          Rename a file if this is allowed.
static void setLength(java.io.RandomAccessFile file, long newLength)
          Change the length of the file.
static boolean setReadOnly(java.lang.String fileName)
          Disable the ability to write.
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.
static boolean tryDelete(java.lang.String fileName)
          Try to delete a file.
static java.lang.String unwrap(java.lang.String fileName)
          Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).
 
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 (null if writing is not required)
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 (null if writing is not required)
Returns:
the number of bytes copied
Throws:
java.io.IOException

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

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

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, 0 meaning EOF
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

getBufferedReader

public static java.io.Reader getBufferedReader(java.io.InputStream in)
Create a buffered 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

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

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

getReaderFromString

public static java.io.Reader getReaderFromString(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

mkdirs

public static void mkdirs(java.io.File directory)
                   throws java.io.IOException
Create the directory and all parent directories if required.

Parameters:
directory - the directory
Throws:
java.io.IOException

setLength

public static void setLength(java.io.RandomAccessFile file,
                             long newLength)
                      throws java.io.IOException
Change the length of the file.

Parameters:
file - the random access file
newLength - the new length
Throws:
java.io.IOException

getFileName

public static java.lang.String getFileName(java.lang.String name)
Get the file name (without directory part).

Parameters:
name - the directory and file name
Returns:
just the file name

canWrite

public static boolean canWrite(java.lang.String fileName)
Check if the file is writable.

Parameters:
fileName - the file name
Returns:
if the file is writable

setReadOnly

public static boolean setReadOnly(java.lang.String fileName)
Disable the ability to write.

Parameters:
fileName - the file name
Returns:
true if the call was successful

copy

public static void copy(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

createNewFile

public static boolean createNewFile(java.lang.String fileName)
Create a new file.

Parameters:
fileName - the file name
Returns:
true if creating was successful

openFileObject

public static FileObject openFileObject(java.lang.String fileName,
                                        java.lang.String mode)
                                 throws java.io.IOException
Open a random access file object.

Parameters:
fileName - the file name
mode - the access mode. Supported are r, rw, rws, rwd
Returns:
the file object
Throws:
java.io.IOException

getCanonicalPath

public static java.lang.String getCanonicalPath(java.lang.String fileName)
Normalize a file name.

Parameters:
fileName - the file name
Returns:
the normalized file name

tryDelete

public static boolean tryDelete(java.lang.String fileName)
Try to delete a file.

Parameters:
fileName - the file name
Returns:
true if it worked

isReadOnly

public static boolean isReadOnly(java.lang.String fileName)
Check if a file is read-only.

Parameters:
fileName - the file name
Returns:
if it is read only

exists

public static boolean exists(java.lang.String fileName)
Checks if a file exists.

Parameters:
fileName - the file name
Returns:
true if it exists

length

public static long length(java.lang.String fileName)
Get the length of a file.

Parameters:
fileName - the file name
Returns:
the length in bytes

createTempFile

public static java.lang.String createTempFile(java.lang.String prefix,
                                              java.lang.String suffix,
                                              boolean deleteOnExit,
                                              boolean inTempDir)
                                       throws java.io.IOException
Create a new temporary file.

Parameters:
prefix - the prefix of the file name (including directory name if required)
suffix - the suffix
deleteOnExit - if the file should be deleted when the virtual machine exists
inTempDir - if the file should be stored in the temporary directory
Returns:
the name of the created file
Throws:
java.io.IOException

getParent

public static java.lang.String getParent(java.lang.String fileName)
Get the parent directory of a file or directory.

Parameters:
fileName - the file or directory name
Returns:
the parent directory name

listFiles

public static java.lang.String[] listFiles(java.lang.String path)
List the files in the given directory.

Parameters:
path - the directory
Returns:
the list of fully qualified file names

isDirectory

public static boolean isDirectory(java.lang.String fileName)
Check if it is a file or a directory.

Parameters:
fileName - the file or directory name
Returns:
true if it is a directory

isAbsolute

public static boolean isAbsolute(java.lang.String fileName)
Check if the file name includes a path.

Parameters:
fileName - the file name
Returns:
if the file name is absolute

fileStartsWith

public static boolean fileStartsWith(java.lang.String fileName,
                                     java.lang.String prefix)
Check if a file starts with a given prefix.

Parameters:
fileName - the complete file name
prefix - the prefix
Returns:
true if it starts with the prefix

openFileInputStream

public static java.io.InputStream openFileInputStream(java.lang.String fileName)
                                               throws java.io.IOException
Create an input stream to read from the file.

Parameters:
fileName - the file name
Returns:
the input stream
Throws:
java.io.IOException

openFileOutputStream

public static java.io.OutputStream openFileOutputStream(java.lang.String fileName,
                                                        boolean append)
Create an output stream to write into the file.

Parameters:
fileName - the file name
append - if true, the file will grow, if false, the file will be truncated first
Returns:
the output stream

rename

public static void rename(java.lang.String oldName,
                          java.lang.String newName)
Rename a file if this is allowed.

Parameters:
oldName - the old fully qualified file name
newName - the new fully qualified file name

createDirs

public static void createDirs(java.lang.String fileName)
Create all required directories that are required for this file.

Parameters:
fileName - the file name (not directory name)

delete

public static void delete(java.lang.String fileName)
Delete a file.

Parameters:
fileName - the file name

deleteRecursive

public static void deleteRecursive(java.lang.String directory,
                                   boolean tryOnly)
Delete a directory or file and all subdirectories and files.

Parameters:
directory - the directory
tryOnly - whether errors should be ignored

getLastModified

public static long getLastModified(java.lang.String fileName)
Get the last modified date of a file.

Parameters:
fileName - the file name
Returns:
the last modified date

unwrap

public static java.lang.String unwrap(java.lang.String fileName)
Get the unwrapped file name (without wrapper prefixes if wrapping / delegating file systems are used).

Parameters:
fileName - the file name
Returns:
the unwrapped

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