Package org.apache.hadoop.io
Class IOUtils
java.lang.Object
org.apache.hadoop.io.IOUtils
An utility class for I/O related functionality.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classorg.apache.hadoop.io.IOUtils.NullOutputStreamThe /dev/null of OutputStreams. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanupWithLogger(org.slf4j.Logger logger, Closeable... closeables) Close the Closeable objects and ignore anyThrowableor null pointers.static voidcloseSocket(Socket sock) Closes the socket ignoringIOExceptionstatic voidcloseStream(Closeable stream) Closes the stream ignoringThrowable.static voidcloseStreams(Closeable... streams) Closes the streams ignoringThrowable.static voidcopyBytes(InputStream in, OutputStream out, int buffSize) Copies from one stream to another.static voidcopyBytes(InputStream in, OutputStream out, int buffSize, boolean close) Copies from one stream to another.static voidcopyBytes(InputStream in, OutputStream out, long count, boolean close) Copies count bytes from one stream to another.static voidcopyBytes(InputStream in, OutputStream out, Configuration conf) Copies from one stream to another.static voidcopyBytes(InputStream in, OutputStream out, Configuration conf, boolean close) Copies from one stream to another.static voidEnsure that any writes to the given file is written to the storage device that contains it.static voidfsync(FileChannel channel, boolean isDir) Ensure that any writes to the given file is written to the storage device that contains it.listDirectory(File dir, FilenameFilter filter) Return the complete list of files in a directory as strings.static voidreadFully(InputStream in, byte[] buf, int off, int len) Reads len bytes in a loop.static byte[]Reads a DataInput until EOF and returns a byte array.static voidskipFully(InputStream in, long len) Similar to readFully().static IOExceptionwrapException(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 intwrappedReadForCompressedData(InputStream is, byte[] buf, int off, int len) Utility wrapper for reading fromInputStream.static voidwriteFully(FileChannel fc, ByteBuffer buf, long offset) Write a ByteBuffer to a FileChannel at a given offset, handling short writes.static voidwriteFully(WritableByteChannel bc, ByteBuffer buf) Write a ByteBuffer to a WritableByteChannel, handling short writes.
-
Field Details
-
LOG
public static final org.slf4j.Logger LOG
-
-
Constructor Details
-
IOUtils
public IOUtils()
-
-
Method Details
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException Copies from one stream to another.- Parameters:
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.- Throws:
IOException- raised on errors performing I/O.
-
copyBytes
Copies from one stream to another.- Parameters:
in- InputStrem to read fromout- OutputStream to write tobuffSize- the size of the buffer.- Throws:
IOException- raised on errors performing I/O.
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, Configuration conf) throws IOException Copies from one stream to another. closes the input and output streams at the end.- Parameters:
in- InputStrem to read fromout- OutputStream to write toconf- the Configuration object.- Throws:
IOException- raised on errors performing I/O.
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, Configuration conf, boolean close) throws IOException Copies from one stream to another.- Parameters:
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.- Throws:
IOException- raised on errors performing I/O.
-
copyBytes
public static void copyBytes(InputStream in, OutputStream out, long count, boolean close) throws IOException Copies count bytes from one stream to another.- Parameters:
in- InputStream to read fromout- OutputStream to write tocount- number of bytes to copyclose- whether to close the streams- Throws:
IOException- if bytes can not be read or written
-
wrappedReadForCompressedData
public static int wrappedReadForCompressedData(InputStream is, byte[] buf, int off, int len) throws IOException Utility wrapper for reading fromInputStream. It catches any errors thrown by the underlying stream (either IO or decompression-related), and re-throws as an IOException.- Parameters:
is- - InputStream to be read frombuf- - buffer the data is read intooff- - offset within buflen- - amount of data to be read- Returns:
- number of bytes read
- Throws:
IOException- raised on errors performing I/O.
-
readFully
Reads len bytes in a loop.- Parameters:
in- InputStream to read frombuf- The buffer to filloff- offset from the bufferlen- the length of bytes to read- Throws:
IOException- if it could not read requested number of bytes for any reason (including EOF)
-
skipFully
Similar to readFully(). Skips bytes in a loop.- Parameters:
in- The InputStream to skip bytes fromlen- number of bytes to skip.- Throws:
IOException- if it could not skip requested number of bytes for any reason (including EOF)
-
cleanupWithLogger
Close the Closeable objects and ignore anyThrowableor null pointers. Must only be used for cleanup in exception handlers.- Parameters:
logger- the log to record problems to at debug level. Can be null.closeables- the objects to close
-
closeStream
Closes the stream ignoringThrowable. Must only be called in cleaning up from exception handlers.- Parameters:
stream- the Stream to close
-
closeStreams
Closes the streams ignoringThrowable. Must only be called in cleaning up from exception handlers.- Parameters:
streams- the Streams to close
-
closeSocket
Closes the socket ignoringIOException- Parameters:
sock- the Socket to close
-
writeFully
Write a ByteBuffer to a WritableByteChannel, handling short writes.- Parameters:
bc- The WritableByteChannel to write tobuf- The input buffer- Throws:
IOException- On I/O error
-
writeFully
Write a ByteBuffer to a FileChannel at a given offset, handling short writes.- Parameters:
fc- The FileChannel to write tobuf- The input bufferoffset- The offset in the file to start writing at- Throws:
IOException- On I/O error
-
listDirectory
Return the complete list of files in a directory as strings.This is better than File#listDir because it does not ignore IOExceptions.
- Parameters:
dir- The directory to list.filter- If non-null, the filter to use when listing this directory.- Returns:
- The list of files in the directory.
- Throws:
IOException- On I/O error
-
fsync
Ensure that any writes to the given file is written to the storage device that contains it. This method opens channel on given File and closes it once the sync is done.
Borrowed from Uwe Schindler in LUCENE-5588- Parameters:
fileToSync- the file to fsync- Throws:
IOException- raised on errors performing I/O.
-
fsync
Ensure that any writes to the given file is written to the storage device that contains it. This method opens channel on given File and closes it once the sync is done. Borrowed from Uwe Schindler in LUCENE-5588- Parameters:
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)- Throws:
IOException- raised on errors performing I/O.
-
wrapException
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. The new exception provides the stack trace of the place where the exception is thrown and some extra diagnostics information. Return instance of same exception if exception class has a public string constructor; Otherwise return an PathIOException. InterruptedIOException and PathIOException are returned unwrapped.- Parameters:
path- file/directory pathmethodName- method nameexception- the caught exception.- Returns:
- an exception to throw
-
readFullyToByteArray
Reads a DataInput until EOF and returns a byte array. Make sure not to pass in an infinite DataInput or this will never return.- Parameters:
in- A DataInput- Returns:
- a byte array containing the data from the DataInput
- Throws:
IOException- on I/O error, other than EOF
-