public class IOUtil extends Object
java.io
and java.nio
packages.Constructor and Description |
---|
IOUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(Closeable closable)
Closes any
Closeable object, swallowing any exception
that it might throw. |
static long |
copy(InputStream in,
OutputStream out)
Copies the input stream to the output stream, leaving both open.
|
static File |
createTempFile(InputStream in,
String prefix)
Creates a temporary file via
createTempFile(java.lang.String, long) , then copies the
contents of the passed stream to it. |
static File |
createTempFile(String prefix,
long size)
Creates a temporary file in the default temporary-file directory.
|
static InputStream |
openFile(File file)
Opens a file.
|
static InputStream |
openFile(String fileName)
Opens a file.
|
static int |
readFully(InputStream in,
byte[] dest)
Repeatedly reads the passed stream, until either the buffer is full or EOF
is reached.
|
static long |
skipFully(InputStream in,
long bytesToSkip)
Repeatedly calls
skip() on the underlying stream, until either the
desired number of bytes have been read or EOF is reached. |
public static void closeQuietly(Closeable closable)
Closeable
object, swallowing any exception
that it might throw. This is used in a finally
block,
where such exceptions would supercede any thrown from code in the
try
block.
This method also exists in Apache Commons IO. It exists here because
older versions of Commons IO took explicit stream types, rather than
Closeable
.
closable
- The object to close. May be null, in which
case this method does nothing.public static long copy(InputStream in, OutputStream out) throws IOException
This method also exists (with many variants) in Jakarta Commons IO. It's here so that this library can be self-contained.
IOException
public static InputStream openFile(File file) throws IOException
IOException
public static InputStream openFile(String fileName) throws IOException
. If any step fails, the
underlying file will be closed (preventing leakage of file descriptors).IOException
public static File createTempFile(String prefix, long size) throws IOException
File
, this method adds a shutdown hook
to delete the file, always uses the suffix ".tmp", and will create files of
arbitrary size. The content of the file will be undefined.prefix
- The prefix used to construct the file's name. Must be at least
three characters long (per File
API).size
- The size of the file, in bytes.IOException
public static File createTempFile(InputStream in, String prefix) throws IOException
createTempFile(java.lang.String, long)
, then copies the
contents of the passed stream to it. The input stream will be closed,
whether or not the method completed normally (including cases where the
file could not be created).IOException
public static int readFully(InputStream in, byte[] dest) throws IOException
The stream will not be closed by this method, even if EOF is reached.
IOException
public static long skipFully(InputStream in, long bytesToSkip) throws IOException
skip()
on the underlying stream, until either the
desired number of bytes have been read or EOF is reached. Returns the number
of bytes actually skipped.IOException