de.schlichtherle.io.util
Class SynchronizedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by de.schlichtherle.io.util.SynchronizedOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class SynchronizedOutputStream
extends OutputStream

A decorator which synchronizes all access to an OutputStream via an object provided to its constructor.

Since:
TrueZIP 6.4
Version:
TrueZIP 6.7
Author:
Christian Schlichtherle

Field Summary
protected  Object lock
          The object to synchronize on - never null.
protected  OutputStream out
          The decorated output stream.
 
Constructor Summary
SynchronizedOutputStream(OutputStream out)
          Constructs a new synchronized output stream.
SynchronizedOutputStream(OutputStream out, Object lock)
          Constructs a new synchronized output stream.
 
Method Summary
 void close()
          Synchronizes on the lock and calls doClose().
protected  void doClose()
          Calls doFlush() and finally closes the underlying stream.
protected  void doFlush()
          Flushes the underlying stream.
 void flush()
          Synchronizes on the lock and calls doFlush().
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock

protected final Object lock
The object to synchronize on - never null.


out

protected OutputStream out
The decorated output stream.

Constructor Detail

SynchronizedOutputStream

public SynchronizedOutputStream(OutputStream out)
Constructs a new synchronized output stream. This object will synchronize on itself.

Parameters:
out - The output stream to wrap in this decorator.

SynchronizedOutputStream

public SynchronizedOutputStream(OutputStream out,
                                Object lock)
Constructs a new synchronized output stream.

Parameters:
out - The output stream to wrap in this decorator.
lock - The object to synchronize on. If null, then this object is used, not the stream.
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Synchronizes on the lock and calls doFlush().

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

doFlush

protected void doFlush()
                throws IOException
Flushes the underlying stream. This method is not synchronized!

Throws:
IOException

close

public void close()
           throws IOException
Synchronizes on the lock and calls doClose().

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

doClose

protected void doClose()
                throws IOException
Calls doFlush() and finally closes the underlying stream. This method is not synchronized!

Throws:
IOException