FileCopyUtils

  • Post author:
  • Post category:其他


/**

* Copy the contents of the given InputStream to the given OutputStream.

* Closes both streams when done.

* @param in the stream to copy from

* @param out the stream to copy to

* @return the number of bytes copied

* @throws IOException in case of I/O errors

*/

public static int copy(InputStream in, OutputStream out) throws IOException {

Assert.notNull(in, “No InputStream specified”);

Assert.notNull(out, “No OutputStream specified”);

try {

int byteCount = 0;

byte[] buffer = new byte[BUFFER_SIZE];

int bytesRead = -1;

while ((bytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, bytesRead);

byteCount += bytesRead;

}

out.flush();

return byteCount;

}

finally {

try {

in.close();

}

catch (IOException ex) {

}

try {

out.close();

}

catch (IOException ex) {

}

}

}