package org.springframework.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Utility methods for file and stream copying.
 * @author Juergen Hoeller
 * @since 06.10.2003
 */
public abstract class FileCopyUtils {

	/**
	 * Copy the contents of the given InputStream to the given OutputStream.
	 * @param in the stream to copy from
	 * @param out the stream to copy to
	 * @throws IOException in case of I/O errors
	 */
	public static void copy(InputStream in, OutputStream out) throws IOException {
		try {
			int b;
			while ((b = in.read()) != -1) {
				out.write(b);
			}
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
				// ignore
			}
			try {
				out.close();
			}
			catch (IOException ex) {
				// ignore
			}
		}
	}

	/**
	 * Copy the contents of the given input File to the given output File.
	 * @param in the file to copy from
	 * @param out the file to copy to
	 * @throws IOException in case of I/O errors
	 */
	public static void copy(File in, File out) throws IOException {
		copy(new BufferedInputStream(new FileInputStream(in)), new BufferedOutputStream(new FileOutputStream(out)));
	}

	/**
	 * Copy the contents of the given byte array to the given output File.
	 * @param in the byte array to copy from
	 * @param out the file to copy to
	 * @throws IOException in case of I/O errors
	 */
	public static void copy(byte[] in, File out) throws IOException {
		ByteArrayInputStream inStream = new ByteArrayInputStream(in);
		OutputStream outStream = new BufferedOutputStream(new FileOutputStream(out));
		copy(inStream, outStream);
	}

	/**
	 * Copy the contents of the given InputStream into a new byte array.
	 * @param in the stream to copy from
	 * @return the new byte array that has been copied to
	 * @throws IOException in case of I/O errors
	 */
	public static byte[] copyToByteArray(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		copy(in, out);
		return out.toByteArray();
	}

	/**
	 * Copy the contents of the given input File into a new byte array.
	 * @param in the file to copy from
	 * @return the new byte array that has been copied to
	 * @throws IOException in case of I/O errors
	 */
	public static byte[] copyToByteArray(File in) throws IOException {
		return copyToByteArray(new BufferedInputStream(new FileInputStream(in)));
	}

}

@@ -491,21 +491,66 @@
 tils %7B%0A%0A
+%09public static final int BLOCK_SIZE = 1024;%0A%0A
 %09/**%0A
-
 %09 * Copy
@@ -615,16 +615,51 @@
 Stream.%0A
+%09 * Closes both streams when done.%0A
 %09 * @par
@@ -871,13 +871,67 @@
 %0A%09%09%09
-int b
+byte%5B%5D buffer = new byte%5BBLOCK_SIZE%5D;%0A%09%09%09int nrOfBytes = -1
 ;%0A%09%09
@@ -939,17 +939,25 @@
 while ((
-b
+nrOfBytes
  = in.re
@@ -959,16 +959,22 @@
 in.read(
+buffer
 )) != -1
@@ -992,23 +992,58 @@
 .write(b
-);%0A%09%09%09%7D
+uffer, 0, nrOfBytes);%0A%09%09%09%7D%0A%09%09%09out.flush();
 %0A%09%09%7D%0A%09%09f

copy with block size 1024