org.apache.hadoop.io
Class IOUtils

java.lang.Object
  extended by org.apache.hadoop.io.IOUtils

public class IOUtils
extends Object

An utility class for I/O related functionality.


Nested Class Summary
static class IOUtils.NullOutputStream
          /dev/null of OutputStreams.
 
Constructor Summary
IOUtils()
           
 
Method Summary
static void cleanup(org.apache.commons.logging.Log log, Closeable... closeables)
          Close the Closeable objects and ignore any IOException or null pointers.
static void closeSocket(Socket sock)
          Closes the socket ignoring IOException
static void closeStream(Closeable stream)
          Closes the stream ignoring IOException.
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 length, int bufferSize, boolean close)
          Copies the specified length of bytes from in to out.
static void readFileChannelFully(FileChannel fileChannel, byte[] buf, int off, int len)
          Reads len bytes in a loop using the channel of the stream
static void readFully(InputStream in, byte[] buf, int off, int len)
          Reads len bytes in a loop.
static void skipFully(InputStream in, long len)
          Similar to readFully().
static int wrappedReadForCompressedData(InputStream is, byte[] buf, int off, int len)
          Utility wrapper for reading from InputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IOUtils

public IOUtils()
Method Detail

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 from
out - OutputStream to write to
buffSize - the size of the buffer
close - whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause.
Throws:
IOException

copyBytes

public static void copyBytes(InputStream in,
                             OutputStream out,
                             int buffSize)
                      throws IOException
Copies from one stream to another.

Parameters:
in - InputStrem to read from
out - OutputStream to write to
buffSize - the size of the buffer
Throws:
IOException

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 from
out - OutputStream to write to
conf - the Configuration object
Throws:
IOException

copyBytes

public static void copyBytes(InputStream in,
                             OutputStream out,
                             Configuration conf,
                             boolean close)
                      throws IOException
Copies from one stream to another.

Parameters:
in - InputStrem to read from
out - OutputStream to write to
conf - the Configuration object
close - whether or not close the InputStream and OutputStream at the end. The streams are closed in the finally clause.
Throws:
IOException

copyBytes

public static void copyBytes(InputStream in,
                             OutputStream out,
                             long length,
                             int bufferSize,
                             boolean close)
                      throws IOException
Copies the specified length of bytes from in to out.

Parameters:
in - InputStream to read from
out - OutputStream to write to
length - number of bytes to copy
bufferSize - the size of the buffer
close - 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 from InputStream. 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 from
buf - - buffer the data is read into
off - - offset within buf
len - - amount of data to be read
Returns:
number of bytes read
Throws:
IOException

readFully

public static void readFully(InputStream in,
                             byte[] buf,
                             int off,
                             int len)
                      throws IOException
Reads len bytes in a loop.

Parameters:
in - The InputStream to read from
buf - The buffer to fill
off - offset from the buffer
len - the length of bytes to read
Throws:
IOException - if it could not read requested number of bytes for any reason (including EOF)

readFileChannelFully

public static void readFileChannelFully(FileChannel fileChannel,
                                        byte[] buf,
                                        int off,
                                        int len)
                                 throws IOException
Reads len bytes in a loop using the channel of the stream

Parameters:
fileChannel - a FileChannel to read len bytes into buf
buf - The buffer to fill
off - offset from the buffer
len - the length of bytes to read
Throws:
IOException - if it could not read requested number of bytes for any reason (including EOF)

skipFully

public static void skipFully(InputStream in,
                             long len)
                      throws IOException
Similar to readFully(). Skips bytes in a loop.

Parameters:
in - The InputStream to skip bytes from
len - number of bytes to skip.
Throws:
IOException - if it could not skip requested number of bytes for any reason (including EOF)

cleanup

public static void cleanup(org.apache.commons.logging.Log log,
                           Closeable... closeables)
Close the Closeable objects and ignore any IOException or null pointers. Must only be used for cleanup in exception handlers.

Parameters:
log - the log to record problems to at debug level. Can be null.
closeables - the objects to close

closeStream

public static void closeStream(Closeable stream)
Closes the stream ignoring IOException. Must only be called in cleaning up from exception handlers.

Parameters:
stream - the Stream to close

closeSocket

public static void closeSocket(Socket sock)
Closes the socket ignoring IOException

Parameters:
sock - the Socket to close


Copyright © 2009 The Apache Software Foundation