Minor update to HADOOP-7429.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1141415 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Eli Collins 2011-06-30 07:04:58 +00:00
parent 61fa4153dc
commit ad95cb1f0d

View File

@ -115,24 +115,32 @@ public static void copyBytes(InputStream in, OutputStream out, Configuration con
* @param in InputStream to read from * @param in InputStream to read from
* @param out OutputStream to write to * @param out OutputStream to write to
* @param count number of bytes to copy * @param count number of bytes to copy
* @param close whether to close the streams
* @throws IOException if bytes can not be read or written * @throws IOException if bytes can not be read or written
*/ */
public static void copyBytes(InputStream in, OutputStream out, long count) public static void copyBytes(InputStream in, OutputStream out, long count,
throws IOException { boolean close) throws IOException {
byte buf[] = new byte[4096]; byte buf[] = new byte[4096];
long bytesRemaining = count; long bytesRemaining = count;
int bytesRead; int bytesRead;
while (bytesRemaining > 0) { try {
int bytesToRead = (int) while (bytesRemaining > 0) {
(bytesRemaining < buf.length ? bytesRemaining : buf.length); int bytesToRead = (int)
(bytesRemaining < buf.length ? bytesRemaining : buf.length);
bytesRead = in.read(buf, 0, bytesToRead); bytesRead = in.read(buf, 0, bytesToRead);
if (bytesRead == -1) if (bytesRead == -1)
break; break;
out.write(buf, 0, bytesRead); out.write(buf, 0, bytesRead);
bytesRemaining -= bytesRead; bytesRemaining -= bytesRead;
}
} finally {
if (close) {
closeStream(out);
closeStream(in);
}
} }
} }