My most favorite methods – part 2 

Okay, here’s the second one. And it took me a few months to write this one πŸ˜›

/**
 * Copies from an {@link InputStream} to an {@link OutputStream}
 *
 * @param from
 * @param to
 */
public static void copyStream(InputStream from, OutputStream to)
{
	byte[] contents = null;
	int count = -1;

	if ((null != from) && (null != to))
	{
		try
		{
			contents = new byte[1024];
			while (-1 != (count = from.read(contents)))
			{
				to.write(contents, 0, count);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			contents = null;
		}
	}
}

This method copies the contents from the input stream to the output stream. Do I need to explain more?

Advertisements