@InterfaceAudience.Public @InterfaceStability.Evolving public class IOUtils extends Object
Modifier and Type | Field and Description |
---|---|
static org.slf4j.Logger |
LOG |
Constructor and Description |
---|
IOUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
cleanup(org.apache.commons.logging.Log log,
Closeable... closeables)
Deprecated.
use
cleanupWithLogger(Logger, java.io.Closeable...)
instead |
static void |
cleanupWithLogger(org.slf4j.Logger logger,
Closeable... closeables)
Close the Closeable objects and ignore any
Throwable or
null pointers. |
static void |
closeSocket(Socket sock)
Closes the socket ignoring
IOException |
static void |
closeStream(Closeable stream)
Closes the stream ignoring
Throwable . |
static void |
closeStreams(Closeable... streams)
Closes the streams ignoring
Throwable . |
static void |
copyBytes(InputStream in,
OutputStream out,
Configuration conf)
Copies from one stream to another.
|
static void |
copyBytes(InputStream in,
OutputStream out,
Configuration conf,
boolean close)
Copies from one stream to another.
|
static void |
copyBytes(InputStream in,
OutputStream out,
int buffSize)
Copies from one stream to another.
|
static void |
copyBytes(InputStream in,
OutputStream out,
int buffSize,
boolean close)
Copies from one stream to another.
|
static void |
copyBytes(InputStream in,
OutputStream out,
long count,
boolean close)
Copies count bytes from one stream to another.
|
static void |
fsync(File fileToSync)
Ensure that any writes to the given file is written to the storage device
that contains it.
|
static void |
fsync(FileChannel channel,
boolean isDir)
Ensure that any writes to the given file is written to the storage device
that contains it.
|
static List<String> |
listDirectory(File dir,
FilenameFilter filter)
Return the complete list of files in a directory as strings.
This is better than File#listDir because it does not ignore IOExceptions.
|
static void |
readFully(InputStream in,
byte[] buf,
int off,
int len)
Reads len bytes in a loop.
|
static byte[] |
readFullyToByteArray(DataInput in)
Reads a DataInput until EOF and returns a byte array.
|
static void |
skipFully(InputStream in,
long len)
Similar to readFully().
|
static IOException |
wrapException(String path,
String methodName,
IOException exception)
Takes an IOException, file/directory path, and method name and returns an
IOException with the input exception as the cause and also include the
file,method details.
|
static int |
wrappedReadForCompressedData(InputStream is,
byte[] buf,
int off,
int len)
Utility wrapper for reading from
InputStream . |
static void |
writeFully(FileChannel fc,
ByteBuffer buf,
long offset)
Write a ByteBuffer to a FileChannel at a given offset,
handling short writes.
|
static void |
writeFully(WritableByteChannel bc,
ByteBuffer buf)
Write a ByteBuffer to a WritableByteChannel, handling short writes.
|
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException
in
- InputStrem to read fromout
- OutputStream to write tobuffSize
- the size of the bufferclose
- whether or not close the InputStream and
OutputStream at the end. The streams are closed in the finally clause.IOException
public static void copyBytes(InputStream in, OutputStream out, int buffSize) throws IOException
in
- InputStrem to read fromout
- OutputStream to write tobuffSize
- the size of the bufferIOException
public static void copyBytes(InputStream in, OutputStream out, Configuration conf) throws IOException
in
- InputStrem to read fromout
- OutputStream to write toconf
- the Configuration objectIOException
public static void copyBytes(InputStream in, OutputStream out, Configuration conf, boolean close) throws IOException
in
- InputStream to read fromout
- OutputStream to write toconf
- the Configuration objectclose
- whether or not close the InputStream and
OutputStream at the end. The streams are closed in the finally clause.IOException
public static void copyBytes(InputStream in, OutputStream out, long count, boolean close) throws IOException
in
- InputStream to read fromout
- OutputStream to write tocount
- number of bytes to copyclose
- whether to close the streamsIOException
- if bytes can not be read or writtenpublic static int wrappedReadForCompressedData(InputStream is, byte[] buf, int off, int len) throws IOException
InputStream
. It catches any errors
thrown by the underlying stream (either IO or decompression-related), and
re-throws as an IOException.is
- - InputStream to be read frombuf
- - buffer the data is read intooff
- - offset within buflen
- - amount of data to be readIOException
public static void readFully(InputStream in, byte[] buf, int off, int len) throws IOException
in
- InputStream to read frombuf
- The buffer to filloff
- offset from the bufferlen
- the length of bytes to readIOException
- if it could not read requested number of bytes
for any reason (including EOF)public static void skipFully(InputStream in, long len) throws IOException
in
- The InputStream to skip bytes fromlen
- number of bytes to skip.IOException
- if it could not skip requested number of bytes
for any reason (including EOF)@Deprecated public static void cleanup(org.apache.commons.logging.Log log, Closeable... closeables)
cleanupWithLogger(Logger, java.io.Closeable...)
insteadThrowable
or
null pointers. Must only be used for cleanup in exception handlers.log
- the log to record problems to at debug level. Can be null.closeables
- the objects to closepublic static void cleanupWithLogger(org.slf4j.Logger logger, Closeable... closeables)
Throwable
or
null pointers. Must only be used for cleanup in exception handlers.logger
- the log to record problems to at debug level. Can be null.closeables
- the objects to closepublic static void closeStream(Closeable stream)
Throwable
.
Must only be called in cleaning up from exception handlers.stream
- the Stream to closepublic static void closeStreams(Closeable... streams)
Throwable
.
Must only be called in cleaning up from exception handlers.streams
- the Streams to closepublic static void closeSocket(Socket sock)
IOException
sock
- the Socket to closepublic static void writeFully(WritableByteChannel bc, ByteBuffer buf) throws IOException
bc
- The WritableByteChannel to write tobuf
- The input bufferIOException
- On I/O errorpublic static void writeFully(FileChannel fc, ByteBuffer buf, long offset) throws IOException
fc
- The FileChannel to write tobuf
- The input bufferoffset
- The offset in the file to start writing atIOException
- On I/O errorpublic static List<String> listDirectory(File dir, FilenameFilter filter) throws IOException
dir
- The directory to list.filter
- If non-null, the filter to use when listing
this directory.IOException
- On I/O errorpublic static void fsync(File fileToSync) throws IOException
fileToSync
- the file to fsyncIOException
public static void fsync(FileChannel channel, boolean isDir) throws IOException
channel
- Channel to syncisDir
- if true, the given file is a directory (Channel should be
opened for read and ignore IOExceptions, because not all file
systems and operating systems allow to fsync on a directory)IOException
public static IOException wrapException(String path, String methodName, IOException exception)
path
- file/directory pathmethodName
- method nameexception
- the caught exception.public static byte[] readFullyToByteArray(DataInput in) throws IOException
in
- A DataInputIOException
- on I/O error, other than EOFCopyright © 2018 Apache Software Foundation. All rights reserved.