Top Description Fields Constructors Methods
uk.ac.ed.ph.snuggletex.internal.util

public final Class IOUtilities

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .Closeable, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, .InputStreamReader, .OutputStream, .Reader, java.nio.channels.FileChannel

A collection of vaguely useful utilities for doing common I/O and File-related tasks.

(This is a cut-down version of the Class of the same name in ph-commons-util.)

Author
David McKain
Version
$Revision$

Field Summary

Modifier and TypeField and Description
public static final int
BUFFER_SIZE

Buffer size when transferring streams

public static final int
MAX_TEXT_STREAM_SIZE

Maximum size of characters we'll read from a text stream before complaining

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
ensureClose(Closeable...
"streams" to close
streams
)

Ensures that as many of the given Closeables are closed.

public static File
ensureDirectoryCreated(File directory)

Simple method to ensure that a given directory exists.

public static File
ensureFileCreated(File file)

Simple method to ensure that a given File exists.

public static String

Returns:

String representing the data read
readCharacterStream
(Reader
source of string data
reader
)

Reads all character data from the given Reader, returning a String containing all of the data.

public static String

Returns:

String representing the data we read in
readUnicodeFile
(File
File to read from
file
)

Same as readUnicodeStream(InputStream) but accepts a plain File object for convenience

public static String

Returns:

String representing the data read in
readUnicodeStream
(InputStream
InputStream supplying character data
in
)

Same as readCharacterStream(Reader) but assumes the stream is encoded as UTF-8.

public static void
transfer(InputStream inStream, OutputStream outStream)

"Transfers" data from the given InputStream to the given OutputStream, closing both streams once the InputStream has been exhausted.

public static void
transfer(FileInputStream fileInStream, FileOutputStream fileOutStream)

Version of transfer(InputStream, OutputStream) for File streams that uses NIO to do a hopefully more efficient transfer.

public static void
transfer(InputStream inStream, OutputStream outStream, boolean closeOutputStream)

"Transfers" data from the given InputStream to the given OutputStream, closing the InputStream afterwards.

public static void
transfer(InputStream inStream, OutputStream outStream, boolean closeInputStream, boolean closeOutputStream)

"Transfers" data from the given InputStream to the given OutputStream, optionally closing the given input and output streams afterwards.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_SIZEback to summary
public static final int BUFFER_SIZE

Buffer size when transferring streams

MAX_TEXT_STREAM_SIZEback to summary
public static final int MAX_TEXT_STREAM_SIZE

Maximum size of characters we'll read from a text stream before complaining

Constructor Detail

IOUtilitiesback to summary
public IOUtilities()

Method Detail

ensureCloseback to summary
public static void ensureClose(Closeable... streams) throws IOException

Ensures that as many of the given Closeables are closed. If any fail to close, the first Exception is thrown after an attempt has been made to close the rest.

For convenience, any number of the stream may be null, in which case they will be ignored.

Parameters
streams:Closeable[]

"streams" to close

ensureDirectoryCreatedback to summary
public static File ensureDirectoryCreated(File directory) throws IOException

Simple method to ensure that a given directory exists. If the directory does not exist then it is created, along with all required parents.

Exceptions
IOException:
if creation could not succeed for some reason.
ensureFileCreatedback to summary
public static File ensureFileCreated(File file) throws IOException

Simple method to ensure that a given File exists. If the File does not exist then it is created, along with all required parent directories.

Exceptions
IOException:
if creation could not succeed for some reason.
readCharacterStreamback to summary
public static String readCharacterStream(Reader reader) throws IOException

Reads all character data from the given Reader, returning a String containing all of the data. The Reader will be buffered for efficiency and will be closed once finished with. Be careful reading in very large files - we will barf if MAX_FILE_SIZE is passed as a safety precaution.

Parameters
reader:Reader

source of string data

Returns:String

String representing the data read

readUnicodeFileback to summary
public static String readUnicodeFile(File file) throws IOException

Same as readUnicodeStream(InputStream) but accepts a plain File object for convenience

Parameters
file:File

File to read from

Returns:String

String representing the data we read in

readUnicodeStreamback to summary
public static String readUnicodeStream(InputStream in) throws IOException

Same as readCharacterStream(Reader) but assumes the stream is encoded as UTF-8.

Parameters
in:InputStream

InputStream supplying character data

Returns:String

String representing the data read in

transferback to summary
public static void transfer(InputStream inStream, OutputStream outStream) throws IOException

"Transfers" data from the given InputStream to the given OutputStream, closing both streams once the InputStream has been exhausted.

This will check to see if both streams are File streams and, if so, use the transfer(FileInputStream, FileOutputStream) version of this method instead.

transferback to summary
public static void transfer(FileInputStream fileInStream, FileOutputStream fileOutStream) throws IOException

Version of transfer(InputStream, OutputStream) for File streams that uses NIO to do a hopefully more efficient transfer.

transferback to summary
public static void transfer(InputStream inStream, OutputStream outStream, boolean closeOutputStream) throws IOException

"Transfers" data from the given InputStream to the given OutputStream, closing the InputStream afterwards. If the parameter closeOutputStream is true then the OutputStream is closed too. If not, it will be flushed.

transferback to summary
public static void transfer(InputStream inStream, OutputStream outStream, boolean closeInputStream, boolean closeOutputStream) throws IOException

"Transfers" data from the given InputStream to the given OutputStream, optionally closing the given input and output streams afterwards.

Even if not closing the output stream, it will still be flushed.