Read from InputStream and write to OutputStream : OutputStream « File Input Output « Java






Read from InputStream and write to OutputStream

   



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
  /**
   * Read input from input stream and write it to output stream 
   * until there is no more input from input stream.
   *
   * @param is input stream the input stream to read from.
   * @param os output stream the output stream to write to.
   * @param buf the byte array to use as a buffer
   */
  public static void flow( InputStream is, OutputStream os, byte[] buf ) 
      throws IOException {
      int numRead;
      while ( (numRead = is.read(buf) ) >= 0) {
          os.write(buf, 0, numRead);
      }
  } 
}

   
    
    
  








Related examples in the same category

1.String Buffer OutputStream
2.A null output stream. All data written to this stream is ignored.
3.Memory Byte Array OutputStream
4.Compare the contents of two Streams to determine if they are equal or not.
5.Byte Counting OutputStream
6.Counting OutputStream
7.A utility class that allows for easy simple obfuscation of streamed data