Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; public class Main { /** *Copies source stream to target. * *@param source The source. *@param name target The target. **/ protected static void copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target) throws Exception { // See if this is a MemoryStream -- we can use WriteTo. ByteArrayOutputStream memContentStream = source; if (memContentStream != null) { memContentStream.writeTo(target); memContentStream.flush(); } else { // Otherwise, copy data through a buffer int c; ByteArrayInputStream inStream = new ByteArrayInputStream(source.toByteArray()); while ((c = inStream.read()) != -1) { target.write((char) c); } } } }