Top Description Constructors Methods
org.h2.util

public Class IOUtils

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .BufferedWriter, .ByteArrayInputStream, .ByteArrayOutputStream, .EOFException, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, .StringWriter, .Writer, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.nio.charset.StandardCharsets, org.h2.engine.Constants, .SysProperties, org.h2.mvstore.DataUtils, org.h2.store.fs.FileUtils

This utility class contains input/output functions.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
closeSilently(AutoCloseable
the AutoCloseable or null
out
)

Close an AutoCloseable without throwing an exception.

public static long

Returns:

the number of bytes copied
copy
(InputStream
the input stream
in
,
OutputStream
the output stream (null if writing is not required)
out
)

Copy all data from the input stream to the output stream.

public static long

Returns:

the number of bytes copied
copy
(InputStream
the input stream
in
,
OutputStream
the output stream (null if writing is not required)
out
,
long
the maximum number of bytes to copy
length
)

Copy all data from the input stream to the output stream.

public static long

Returns:

the number of bytes copied
copy
(FileChannel
the input FileChannel
in
,
OutputStream
the output stream (null if writing is not required)
out
)

Copy all data from the input FileChannel to the output stream.

public static long

Returns:

the number of bytes copied
copy
(FileChannel
the input FileChannel
in
,
OutputStream
the output stream (null if writing is not required)
out
,
long
the maximum number of bytes to copy
length
)

Copy all data from the input FileChannel to the output stream.

public static long

Returns:

the number of bytes copied
copyAndClose
(InputStream
the input stream
in
,
OutputStream
the output stream
out
)

Copy all data from the input stream to the output stream and close both streams.

public static long

Returns:

the number of bytes copied
copyAndCloseInput
(InputStream
the input stream
in
,
OutputStream
the output stream (null if writing is not required)
out
)

Copy all data from the input stream to the output stream and close the input stream.

public static long

Returns:

the number of characters copied
copyAndCloseInput
(Reader
the reader
in
,
Writer
the writer (null if writing is not required)
out
,
long
the maximum number of bytes to copy
length
)

Copy all data from the reader to the writer and close the reader.

public static void
copyFiles(String
the original file name
original
,
String
the file name of the copy
copy
)

Copy a file from one directory to another, or to another file.

public static Reader

Returns:

the reader
getAsciiReader
(InputStream
the input stream
in
)

Wrap an input stream in a reader.

public static Writer

Returns:

the writer
getBufferedWriter
(OutputStream
the output stream or null
out
)

Create a buffered writer to write to an output stream using the UTF-8 format.

public static InputStream

Returns:

the input stream
getInputStreamFromString
(String
the string
s
)

Create an input stream to read from a string.

public static Reader

Returns:

the reader
getReader
(InputStream
the input stream or null
in
)

Create a reader to read from an input stream using the UTF-8 format.

public static String

Returns:

path with converted separators
nameSeparatorsToNative
(String
path to convert
path
)

Converts / and \ name separators in path to native separators.

public static byte[]

Returns:

the bytes read
readBytesAndClose
(InputStream
the input stream
in
,
int
the maximum number of bytes to read, or -1 to read until the end of file
length
)

Read a number of bytes from an input stream and close the stream.

public static int

Returns:

the number of bytes read, 0 meaning EOF
readFully
(InputStream
the input stream
in
,
byte[]
the output buffer
buffer
,
int
the number of bytes to read at most
max
)

Try to read the given number of bytes to the buffer.

public static int

Returns:

the number of characters read, 0 meaning EOF
readFully
(Reader
the reader
in
,
char[]
the output buffer
buffer
,
int
the number of characters to read at most
max
)

Try to read the given number of characters to the buffer.

public static String

Returns:

the string read
readStringAndClose
(Reader
the reader
in
,
int
the maximum number of characters to read, or -1 to read until the end of file
length
)

Read a number of characters from a reader and close it.

public static void
skipFully(InputStream
the input stream
in
,
long
the number of bytes to skip
skip
)

Skip a number of bytes in an input stream.

public static void
skipFully(Reader
the reader
reader
,
long
the number of characters to skip
skip
)

Skip a number of characters in a reader.

public static void
trace(String
the method from where this method was called
method
,
String
the file name
fileName
,
Object
the object to append to the message
o
)

Trace input or output operations if enabled.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

IOUtilsback to summary
private IOUtils()

Method Detail

closeSilentlyback to summary
public static void closeSilently(AutoCloseable out)

Close an AutoCloseable without throwing an exception.

Parameters
out:AutoCloseable

the AutoCloseable or null

copyback to summary
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:InputStream

the input stream

out:OutputStream

the output stream (null if writing is not required)

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyback to summary
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:InputStream

the input stream

out:OutputStream

the output stream (null if writing is not required)

length:long

the maximum number of bytes to copy

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyback to summary
public static long copy(FileChannel in, OutputStream out) throws IOException

Copy all data from the input FileChannel to the output stream. Both source and destination are kept open.

Parameters
in:FileChannel

the input FileChannel

out:OutputStream

the output stream (null if writing is not required)

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyback to summary
public static long copy(FileChannel in, OutputStream out, long length) throws IOException

Copy all data from the input FileChannel to the output stream. Both source and destination are kept open.

Parameters
in:FileChannel

the input FileChannel

out:OutputStream

the output stream (null if writing is not required)

length:long

the maximum number of bytes to copy

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyAndCloseback to summary
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:InputStream

the input stream

out:OutputStream

the output stream

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyAndCloseInputback to summary
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:InputStream

the input stream

out:OutputStream

the output stream (null if writing is not required)

Returns:long

the number of bytes copied

Exceptions
IOException:
on failure
copyAndCloseInputback to summary
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:Reader

the reader

out:Writer

the writer (null if writing is not required)

length:long

the maximum number of bytes to copy

Returns:long

the number of characters copied

Exceptions
IOException:
on failure
copyFilesback to summary
public static void copyFiles(String original, String copy) throws IOException

Copy a file from one directory to another, or to another file.

Parameters
original:String

the original file name

copy:String

the file name of the copy

Exceptions
IOException:
on failure
getAsciiReaderback to summary
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:InputStream

the input stream

Returns:Reader

the reader

getBufferedWriterback to summary
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:OutputStream

the output stream or null

Returns:Writer

the writer

getInputStreamFromStringback to summary
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:String

the string

Returns:InputStream

the input stream

getReaderback to summary
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:InputStream

the input stream or null

Returns:Reader

the reader

nameSeparatorsToNativeback to summary
public static String nameSeparatorsToNative(String path)

Converts / and \ name separators in path to native separators.

Parameters
path:String

path to convert

Returns:String

path with converted separators

readBytesAndCloseback to summary
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:InputStream

the input stream

length:int

the maximum number of bytes to read, or -1 to read until the end of file

Returns:byte[]

the bytes read

Exceptions
IOException:
on failure
readFullyback to summary
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:InputStream

the input stream

buffer:byte[]

the output buffer

max:int

the number of bytes to read at most

Returns:int

the number of bytes read, 0 meaning EOF

Exceptions
IOException:
on failure
readFullyback to summary
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:Reader

the reader

buffer:char[]

the output buffer

max:int

the number of characters to read at most

Returns:int

the number of characters read, 0 meaning EOF

Exceptions
IOException:
on failure
readStringAndCloseback to summary
public static String readStringAndClose(Reader in, int length) throws IOException

Read a number of characters from a reader and close it.

Parameters
in:Reader

the reader

length:int

the maximum number of characters to read, or -1 to read until the end of file

Returns:String

the string read

Exceptions
IOException:
on failure
skipFullyback to summary
public static void skipFully(InputStream in, long skip) throws IOException

Skip a number of bytes in an input stream.

Parameters
in:InputStream

the input stream

skip:long

the number of bytes to skip

Exceptions
IOException:
if an IO exception occurred while skipping
EOFException:
if the end of file has been reached before all bytes could be skipped
skipFullyback to summary
public static void skipFully(Reader reader, long skip) throws IOException

Skip a number of characters in a reader.

Parameters
reader:Reader

the reader

skip:long

the number of characters to skip

Exceptions
IOException:
if an IO exception occurred while skipping
EOFException:
if the end of file has been reached before all characters could be skipped
traceback to summary
public static void trace(String method, String fileName, Object o)

Trace input or output operations if enabled.

Parameters
method:String

the method from where this method was called

fileName:String

the file name

o:Object

the object to append to the message