Apache Commons IO - TeeOutputStream Class



Overview

TeeOutputStream splits the OutputStream. It is named after the unix 'tee' command. It allows a stream to be branched to two streams.

Class Declaration

Following is the declaration for org.apache.commons.io.input.TeeOutputStream Class −

public class TeeOutputStream
   extends ProxyOutputStream

Steps of Using TeeOutputStream

Step 1: Create a TeeOutputStream for two outputstreams

// create two output streams
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
TeeOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);

Step 2: Create a TeeInputStream for TeeOutputStream

// create an input stream
ByteArrayInputStream inputStream = new ByteArrayInputStream("data".getBytes("US-ASCII"));
// create teeInputStream using inputStream, teeOutputStream and closeBranch flag as true
// if closeBranch is true, outputstream is automatically closed when teeInputstream is closed.
TeeInputStream teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true);

Step 3: Read Data using TeeInputStream

teeInputStream.read(new byte["data".length()]);

Step 4: Verify data in output streams

System.out.println("Output stream 1: " + outputStream1.toString());
System.out.println("Output stream 2: " + outputStream2.toString());

Step 5: Close the stream.

//teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2.
teeInputStream.close();

Example - Usage of TeeOutputStream.

CommonsIoTester.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.commons.io.input.TeeInputStream;
import org.apache.commons.io.output.TeeOutputStream;

public class CommonsIoTester {
   private static final String SAMPLE = "Welcome to TutorialsPoint. Simply Easy Learning.";
   public static void main(String[] args) {
      TeeInputStream teeInputStream = null;
      TeeOutputStream teeOutputStream = null;
      try {
         ByteArrayInputStream inputStream = new
         ByteArrayInputStream(SAMPLE.getBytes("US-ASCII"));
         ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
         ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
         teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
         teeInputStream = new TeeInputStream(inputStream, teeOutputStream, true);
         teeInputStream.read(new byte[SAMPLE.length()]);
         System.out.println("Output stream 1: " + outputStream1.toString());
         System.out.println("Output stream 2: " + outputStream2.toString());
      }catch (IOException e) {
         System.out.println(e.getMessage());
      } finally {
         //teeIn.close() closes teeIn and teeOut which in turn closes the out1 and out2.
         try {
            teeInputStream.close();
         } catch (IOException e) {
            System.out.println(e.getMessage());
         }
      }
   }
}

Output

It will print the following result −

Output stream 1: Welcome to TutorialsPoint. Simply Easy Learning.
Output stream 2: Welcome to TutorialsPoint. Simply Easy Learning.
Advertisements