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 void createDirs(java.lang.String fileName)
          Create all required directories that are required for this 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 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.lang.String getAbsolutePath(java.lang.String fileName)
          Get the absolute file name.
static java.io.Reader getAsciiReader(java.io.InputStream in)
          Wrap an input stream in a reader.
static java.lang.String getFileInUserHome(java.lang.String fileName)
          Get the absolute file path of a file in the user home directory.
static java.lang.String getFileName(java.lang.String name)
          Get the file name (without directory part).
static java.io.InputStream getInputStream(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 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 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 isInDir(java.io.File file, java.io.File dir)
          Checks if a file is below a given 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.lang.String normalize(java.lang.String fileName)
          Normalize a file name.
static java.io.InputStream openFileInputStream(java.lang.String fileName)
          Create an input stream to read from the file.
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 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 boolean tryDelete(java.lang.String fileName)
          Try to delete a file.
 
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, 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

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

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

getFileInUserHome

public static java.lang.String getFileInUserHome(java.lang.String fileName)
Get the absolute file path of a file in the user home directory.

Parameters:
fileName - the file name
Returns:
the absolute path

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

normalize

public static java.lang.String normalize(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

getAbsolutePath

public static java.lang.String getAbsolutePath(java.lang.String fileName)
Get the absolute file name.

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

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

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

isInDir

public static boolean isInDir(java.io.File file,
                              java.io.File dir)
Checks if a file is below a given directory

Parameters:
file - the file to check
dir - the directory the file must be in
Returns:
true if the file is within the directory