Java InputStream to OutputStream copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target)

Here you can find the source of copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target)

Description

Copies source stream to target.

License

Open Source License

Parameter

Parameter Description
source The source.
target The target.

Declaration

protected static void copyStream(ByteArrayOutputStream source,
        ByteArrayOutputStream target) throws Exception 

Method Source Code

 //package com.java2s;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;

 public class Main {
     /** /*from w w w  .j a v  a2 s  .  c  om*/
      *Copies source stream to target.
      * 
      *@param source The source. 
      *@param target The target.
      **/
     protected static void copyStream(ByteArrayOutputStream source,
             ByteArrayOutputStream target) throws Exception {
         // See if this is a MemoryStream -- we can use WriteTo.

         /*    InputStream inputStream = new FileInputStream ("D:\\EWS ManagedAPI sp2\\Rp\\xml\\useravailrequest.xml");
       
 byte buf[]=new byte[1024];
 int len;
 while((len=inputStream.read(buf))>0)
 {
  target.write(buf,0, len);
 }
*/

         /*PrintWriter pw = new PrintWriter(source,true);
         PrintWriter pw1 = new PrintWriter(target,true);
         pw1.println(pw.toString());*/

         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);

             }
         }
     }
 }

Related

  1. copyStream(@Nonnull InputStream in, @Nonnull OutputStream out)
  2. copyStream(final InputStream in, final OutputStream out)
  3. copyStream(final InputStream in, final OutputStream out)
  4. copyStream(final InputStream in, final OutputStream out, final int bufferSize)
  5. copyStream(final InputStream in, final OutputStream out, int iMax)