HADOOP-7429. Add another IOUtils#copyBytes method. Contributed by Eli Collins
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1140442 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f43e0ffa7e
commit
3ebc992e58
@ -237,6 +237,8 @@ Trunk (unreleased changes)
|
|||||||
HADOOP-310. Additional constructor requested in BytesWritable. (Brock
|
HADOOP-310. Additional constructor requested in BytesWritable. (Brock
|
||||||
Noland via atm)
|
Noland via atm)
|
||||||
|
|
||||||
|
HADOOP-7429. Add another IOUtils#copyBytes method. (eli)
|
||||||
|
|
||||||
OPTIMIZATIONS
|
OPTIMIZATIONS
|
||||||
|
|
||||||
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole
|
HADOOP-7333. Performance improvement in PureJavaCrc32. (Eric Caspole
|
||||||
|
@ -36,6 +36,7 @@ public class IOUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies from one stream to another.
|
* Copies from one stream to another.
|
||||||
|
*
|
||||||
* @param in InputStrem to read from
|
* @param in InputStrem to read from
|
||||||
* @param out OutputStream to write to
|
* @param out OutputStream to write to
|
||||||
* @param buffSize the size of the buffer
|
* @param buffSize the size of the buffer
|
||||||
@ -44,7 +45,6 @@ public class IOUtils {
|
|||||||
*/
|
*/
|
||||||
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
|
public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
copyBytes(in, out, buffSize);
|
copyBytes(in, out, buffSize);
|
||||||
if(close) {
|
if(close) {
|
||||||
@ -70,7 +70,6 @@ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boo
|
|||||||
*/
|
*/
|
||||||
public static void copyBytes(InputStream in, OutputStream out, int buffSize)
|
public static void copyBytes(InputStream in, OutputStream out, int buffSize)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null;
|
PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null;
|
||||||
byte buf[] = new byte[buffSize];
|
byte buf[] = new byte[buffSize];
|
||||||
int bytesRead = in.read(buf);
|
int bytesRead = in.read(buf);
|
||||||
@ -82,9 +81,11 @@ public static void copyBytes(InputStream in, OutputStream out, int buffSize)
|
|||||||
bytesRead = in.read(buf);
|
bytesRead = in.read(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies from one stream to another. <strong>closes the input and output streams
|
* Copies from one stream to another. <strong>closes the input and output streams
|
||||||
* at the end</strong>.
|
* at the end</strong>.
|
||||||
|
*
|
||||||
* @param in InputStrem to read from
|
* @param in InputStrem to read from
|
||||||
* @param out OutputStream to write to
|
* @param out OutputStream to write to
|
||||||
* @param conf the Configuration object
|
* @param conf the Configuration object
|
||||||
@ -96,7 +97,8 @@ public static void copyBytes(InputStream in, OutputStream out, Configuration con
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies from one stream to another.
|
* Copies from one stream to another.
|
||||||
* @param in InputStrem to read from
|
*
|
||||||
|
* @param in InputStream to read from
|
||||||
* @param out OutputStream to write to
|
* @param out OutputStream to write to
|
||||||
* @param conf the Configuration object
|
* @param conf the Configuration object
|
||||||
* @param close whether or not close the InputStream and
|
* @param close whether or not close the InputStream and
|
||||||
@ -107,20 +109,49 @@ public static void copyBytes(InputStream in, OutputStream out, Configuration con
|
|||||||
copyBytes(in, out, conf.getInt("io.file.buffer.size", 4096), close);
|
copyBytes(in, out, conf.getInt("io.file.buffer.size", 4096), close);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Reads len bytes in a loop.
|
/**
|
||||||
* @param in The InputStream to read from
|
* Copies count bytes from one stream to another.
|
||||||
|
*
|
||||||
|
* @param in InputStream to read from
|
||||||
|
* @param out OutputStream to write to
|
||||||
|
* @param count number of bytes to copy
|
||||||
|
* @throws IOException if bytes can not be read or written
|
||||||
|
*/
|
||||||
|
public static void copyBytes(InputStream in, OutputStream out, long count)
|
||||||
|
throws IOException {
|
||||||
|
byte buf[] = new byte[4096];
|
||||||
|
long bytesRemaining = count;
|
||||||
|
int bytesRead;
|
||||||
|
|
||||||
|
while (bytesRemaining > 0) {
|
||||||
|
int bytesToRead = (int)
|
||||||
|
(bytesRemaining < buf.length ? bytesRemaining : buf.length);
|
||||||
|
|
||||||
|
bytesRead = in.read(buf, 0, bytesToRead);
|
||||||
|
if (bytesRead == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
out.write(buf, 0, bytesRead);
|
||||||
|
bytesRemaining -= bytesRead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads len bytes in a loop.
|
||||||
|
*
|
||||||
|
* @param in InputStream to read from
|
||||||
* @param buf The buffer to fill
|
* @param buf The buffer to fill
|
||||||
* @param off offset from the buffer
|
* @param off offset from the buffer
|
||||||
* @param len the length of bytes to read
|
* @param len the length of bytes to read
|
||||||
* @throws IOException if it could not read requested number of bytes
|
* @throws IOException if it could not read requested number of bytes
|
||||||
* for any reason (including EOF)
|
* for any reason (including EOF)
|
||||||
*/
|
*/
|
||||||
public static void readFully( InputStream in, byte buf[],
|
public static void readFully(InputStream in, byte buf[],
|
||||||
int off, int len ) throws IOException {
|
int off, int len) throws IOException {
|
||||||
int toRead = len;
|
int toRead = len;
|
||||||
while ( toRead > 0 ) {
|
while (toRead > 0) {
|
||||||
int ret = in.read( buf, off, toRead );
|
int ret = in.read(buf, off, toRead);
|
||||||
if ( ret < 0 ) {
|
if (ret < 0) {
|
||||||
throw new IOException( "Premature EOF from inputStream");
|
throw new IOException( "Premature EOF from inputStream");
|
||||||
}
|
}
|
||||||
toRead -= ret;
|
toRead -= ret;
|
||||||
@ -128,16 +159,17 @@ public static void readFully( InputStream in, byte buf[],
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Similar to readFully(). Skips bytes in a loop.
|
/**
|
||||||
|
* Similar to readFully(). Skips bytes in a loop.
|
||||||
* @param in The InputStream to skip bytes from
|
* @param in The InputStream to skip bytes from
|
||||||
* @param len number of bytes to skip.
|
* @param len number of bytes to skip.
|
||||||
* @throws IOException if it could not skip requested number of bytes
|
* @throws IOException if it could not skip requested number of bytes
|
||||||
* for any reason (including EOF)
|
* for any reason (including EOF)
|
||||||
*/
|
*/
|
||||||
public static void skipFully( InputStream in, long len ) throws IOException {
|
public static void skipFully(InputStream in, long len) throws IOException {
|
||||||
while ( len > 0 ) {
|
while (len > 0) {
|
||||||
long ret = in.skip( len );
|
long ret = in.skip(len);
|
||||||
if ( ret < 0 ) {
|
if (ret < 0) {
|
||||||
throw new IOException( "Premature EOF from inputStream");
|
throw new IOException( "Premature EOF from inputStream");
|
||||||
}
|
}
|
||||||
len -= ret;
|
len -= ret;
|
||||||
@ -147,11 +179,12 @@ public static void skipFully( InputStream in, long len ) throws IOException {
|
|||||||
/**
|
/**
|
||||||
* Close the Closeable objects and <b>ignore</b> any {@link IOException} or
|
* Close the Closeable objects and <b>ignore</b> any {@link IOException} or
|
||||||
* null pointers. Must only be used for cleanup in exception handlers.
|
* null pointers. Must only be used for cleanup in exception handlers.
|
||||||
|
*
|
||||||
* @param log the log to record problems to at debug level. Can be null.
|
* @param log the log to record problems to at debug level. Can be null.
|
||||||
* @param closeables the objects to close
|
* @param closeables the objects to close
|
||||||
*/
|
*/
|
||||||
public static void cleanup(Log log, java.io.Closeable... closeables) {
|
public static void cleanup(Log log, java.io.Closeable... closeables) {
|
||||||
for(java.io.Closeable c : closeables) {
|
for (java.io.Closeable c : closeables) {
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
try {
|
try {
|
||||||
c.close();
|
c.close();
|
||||||
@ -167,27 +200,29 @@ public static void cleanup(Log log, java.io.Closeable... closeables) {
|
|||||||
/**
|
/**
|
||||||
* Closes the stream ignoring {@link IOException}.
|
* Closes the stream ignoring {@link IOException}.
|
||||||
* Must only be called in cleaning up from exception handlers.
|
* Must only be called in cleaning up from exception handlers.
|
||||||
|
*
|
||||||
* @param stream the Stream to close
|
* @param stream the Stream to close
|
||||||
*/
|
*/
|
||||||
public static void closeStream( java.io.Closeable stream ) {
|
public static void closeStream(java.io.Closeable stream) {
|
||||||
cleanup(null, stream);
|
cleanup(null, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the socket ignoring {@link IOException}
|
* Closes the socket ignoring {@link IOException}
|
||||||
|
*
|
||||||
* @param sock the Socket to close
|
* @param sock the Socket to close
|
||||||
*/
|
*/
|
||||||
public static void closeSocket( Socket sock ) {
|
public static void closeSocket(Socket sock) {
|
||||||
// avoids try { close() } dance
|
if (sock != null) {
|
||||||
if ( sock != null ) {
|
|
||||||
try {
|
try {
|
||||||
sock.close();
|
sock.close();
|
||||||
} catch ( IOException ignored ) {
|
} catch (IOException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** /dev/null of OutputStreams.
|
/**
|
||||||
|
* The /dev/null of OutputStreams.
|
||||||
*/
|
*/
|
||||||
public static class NullOutputStream extends OutputStream {
|
public static class NullOutputStream extends OutputStream {
|
||||||
public void write(byte[] b, int off, int len) throws IOException {
|
public void write(byte[] b, int off, int len) throws IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user