tribble.io
Class OutputStreamMerger

java.lang.Object
  extended by tribble.io.OutputStreamMerger

public class OutputStreamMerger
extends java.lang.Object

Output stream funnel. Merges the output of one or more output streams into the same underlying output stream.

Data written to any of the source streams for this object is merged together and written to the single output (sink) stream of this object. This acts like a funnel, combining the output data from several source streams into a single output stream. This can be useful when several threads are writing to a single text file simultaneously.

Source code:
http://david.tribble.com/src/java/tribble/io/OutputStreamMerger.java
Documentation:
http://david.tribble.com/docs/tribble/io/OutputStreamMerger.html

Since:
2008-09-25
Version:
$Revision: 1.2 $ $Date: 2008/09/26 14:29:36 $
Author:
David R. Tribble (david@tribble.com)

Copyright 2008 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.


Field Summary
static int EOLN_NATIVE
          Indicates that the native end-of-line character is used.
static int EOLN_NONE
          Indicates that no end-of-line character is used.
(package private) static java.lang.String REV
           
 
Constructor Summary
OutputStreamMerger(java.io.OutputStream out)
          Constructs a multiple output stream funnel with no source streams.
 
Method Summary
 void close()
          Close the underlying output stream.
 void flush()
          Flush any pending output to the underlying output stream.
 java.io.OutputStream getSource()
          Create a source output stream that writes merged data to the underlying output stream of this object.
 java.io.OutputStream getSource(int eoln, boolean autoFlush, int bufSize)
          Create a source output stream that writes merged data to the underlying output stream of this object.
(package private)  void writeBytes(byte[] buf, int off, int len)
          Write a set of bytes to the underlying output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REV

static final java.lang.String REV
See Also:
Constant Field Values

EOLN_NONE

public static final int EOLN_NONE
Indicates that no end-of-line character is used.

See Also:
Constant Field Values

EOLN_NATIVE

public static final int EOLN_NATIVE
Indicates that the native end-of-line character is used.

Constructor Detail

OutputStreamMerger

public OutputStreamMerger(java.io.OutputStream out)
Constructs a multiple output stream funnel with no source streams.

Method getSource() is called to create one or more source streams for this object's output stream. All data written to the source streams is merged and written to the single underlying output stream.

Parameters:
out - The underlying output stream, to which all the data written to the source streams associated with this object is written.
Since:
1.1, 2008-09-25
Method Detail

getSource

public java.io.OutputStream getSource(int eoln,
                                      boolean autoFlush,
                                      int bufSize)
Create a source output stream that writes merged data to the underlying output stream of this object.

Note that each call to this method creates a new independent data source for the underlying output stream.

Parameters:
eoln - End-of-line character code for the buffered output stream. All output written to the stream is buffered by lines, and is only written when the internal line buffer becomes full or when an end-of-line character code is written. If this is equal to EOLN_NATIVE, the native end-of-line character for the underlying operating system is used. Is this is equal to EOLN_NONE, no end-of-line character is used, and output is written only when the internal line buffer becomes full.

autoFlush - If true, the underlying output stream is flushed (see flush()) whenever the flush() method of the source stream is called; otherwise, only the internal line buffer of the source stream is flushed to the underlying output stream.

bufSize - Size (in bytes) of the internal line buffer. If this is zero or negative, the default size of 1024 bytes is used.
Returns:
A source output stream. Data written to the output stream is merged with data written to the same underlying output stream shared by other source streams.
Since:
1.2, 2008-09-26

getSource

public java.io.OutputStream getSource()
Create a source output stream that writes merged data to the underlying output stream of this object. This is equivalent to the call:
    getSource(EOLN_NATIVE, false, 0)

Returns:
A source output stream. Data written to the output stream is merged with data written to the same underlying output stream shared by other source streams.
Since:
1.2, 2008-09-26

flush

public void flush()
           throws java.io.IOException
Flush any pending output to the underlying output stream.

Throws:
java.io.IOException - Thrown if an I/O (write) error occurs on the underlying output stream.
Since:
1.1, 2008-09-25

close

public void close()
           throws java.io.IOException
Close the underlying output stream. Subsequent attempts to write data to any of the source streams associated with this object will cause exceptions.

Note that this method can be called multiple times without any ill effects.

Throws:
java.io.IOException - Thrown if an I/O error occurs on the underlying output stream.
Since:
1.1, 2008-09-25

writeBytes

void writeBytes(byte[] buf,
                int off,
                int len)
          throws java.io.IOException
Write a set of bytes to the underlying output stream. This method is called only by the methods of an helper inner class.

Parameters:
buf - An array of bytes to be written.

off - Index of the first byte in buf to write.

len - Number of bytes in buf to write.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs on the underlying output stream.
Since:
1.1, 2008-09-25