Class TeeOutputStream

java.lang.Object
java.io.OutputStream
com.unboundid.util.TeeOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

This class provides an OutputStream implementation that can cause everything provided to it to be written to multiple output streams (e.g., to both a file and to standard output, or to both a file and a network socket). Any number of destination streams (including zero, if desired) may be specified.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TeeOutputStream(OutputStream... targetStreams)
    Creates a new instance of this output stream that will write any data received to each of the provided target streams.
    TeeOutputStream(Collection<? extends OutputStream> targetStreams)
    Creates a new instance of this output stream that will write any data received to each of the provided target streams.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes each of the target output streams.
    void
    Flushes each of the target output streams to force any buffered content to be written out.
    void
    write(byte[] b)
    Writes the entire contents of the provided byte array to each of the target output streams.
    void
    write(byte[] b, int off, int len)
    Writes a portion of the contents of the provided byte array to each of the target output streams.
    void
    write(int b)
    Writes the provided byte of data to each of the target output streams.

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TeeOutputStream

      public TeeOutputStream(@Nullable OutputStream... targetStreams)
      Creates a new instance of this output stream that will write any data received to each of the provided target streams.
      Parameters:
      targetStreams - The set of output streams to which any data received will be written. If it is null or empty, then any data received will simply be discarded.
    • TeeOutputStream

      public TeeOutputStream(@Nullable Collection<? extends OutputStream> targetStreams)
      Creates a new instance of this output stream that will write any data received to each of the provided target streams.
      Parameters:
      targetStreams - The set of output streams to which any data received will be written. If it is null or empty, then any data received will simply be discarded.
  • Method Details

    • write

      public void write(int b) throws IOException
      Writes the provided byte of data to each of the target output streams.
      Specified by:
      write in class OutputStream
      Parameters:
      b - The byte of data to be written. Only the lower eight bits of the provided value will be written.
      Throws:
      IOException - If a problem occurs while writing the provided byte to any of the target output streams.
    • write

      public void write(@NotNull byte[] b) throws IOException
      Writes the entire contents of the provided byte array to each of the target output streams.
      Overrides:
      write in class OutputStream
      Parameters:
      b - The byte array containing the data to be written.
      Throws:
      IOException - If a problem occurs while writing the provided data to any of the target output streams.
    • write

      public void write(@NotNull byte[] b, int off, int len) throws IOException
      Writes a portion of the contents of the provided byte array to each of the target output streams.
      Overrides:
      write in class OutputStream
      Parameters:
      b - The byte array containing the data to be written.
      off - The offset within the array at which the data should start being written.
      len - The number of bytes from the array that should be written.
      Throws:
      IOException - If a problem occurs while writing the provided data to any of the target output streams.
    • flush

      public void flush() throws IOException
      Flushes each of the target output streams to force any buffered content to be written out.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - If a problem occurs while flushing any of the target output streams.
    • close

      public void close() throws IOException
      Closes each of the target output streams.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - If a problem occurs while closing any of the target output streams. Note that even if an exception is thrown, an attempt will be made to close all target streams. If multiple target streams throw an exception, then the first exception encountered will be thrown.