org.gcube.contentmanagement.baselayer.streamutils
Class FinishingActionOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.gcube.contentmanagement.baselayer.streamutils.FinishingActionOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, FinishingActionStream
Direct Known Subclasses:
CountExceedsActionOutputStream

public class FinishingActionOutputStream
extends java.io.OutputStream
implements FinishingActionStream

This class provites a generic wrapper for OutputStreams, which are decorated with some action that should be performed when the end of the stream has been reached, in other words, the stream is closed. For convenience, also the functionality to count the number of written bytes has been implemented here, so through this, the attached action can decide on the number of bytes that have been written, what to do next.
There is an implementation for this available in the Jakarta Commons IO classes, but since this was not enough work to implement here, it was preferred not to include yet another dependency here - just use a similar interface.

See Also:
OutputStream, org.apache.commons.io.output.CountingOutputStream

Constructor Summary
FinishingActionOutputStream(java.io.OutputStream wrappedStream, FinishingAction action)
          wrapps a stream with some action, that will be executed once, when the end of the stream has been reached, that is, the stream is closed.
 
Method Summary
 void close()
          
 java.io.IOException exceptionBeforeFinishing()
          Captures any error/exception/throwable or exception that occured right before executing the finishing action, in particular any IOException that occured when closing the stream.
By this, the action can decide how to deal with this exception.
 void finish()
          closes the wrapped stream and then executes the action, that has been defined to take place at the end of the stream
 void flush()
          
 long getCount()
          Returns the number of bytes written to the stream
 FinishingAction getFinishingAction()
          Retruns the assigned finishing action
 java.lang.Object getWrappedStream()
          Returns the wrapped stream on which the stream operates
 void incrementCount(long valueToAdd)
          Increments the value of count by the given value.
 long resetCount()
          Resets the number of bytes written to zero, to start again counting.
 void resetExceptionBeforeFinishing()
          Resets any exception that has been captured before executing the finishing action, such that it will not be thrown after finishing action was performed.
 void setFinishingAction(FinishingAction finishingAction)
          Sets the action that should be performed for finishing processing of the stream
 void setWrappedStream(java.lang.Object wrappedStream)
          Sets the wrapped stream on which the stream operates
 void write(byte[] b)
          
 void write(byte[] b, int off, int len)
          
 void write(int b)
          
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FinishingActionOutputStream

public FinishingActionOutputStream(java.io.OutputStream wrappedStream,
                                   FinishingAction action)
wrapps a stream with some action, that will be executed once, when the end of the stream has been reached, that is, the stream is closed.

Parameters:
wrappedStream - the stream from which the data is taken
action - the action that should be executed
Method Detail

close

public void close()
           throws java.io.IOException

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

finish

public void finish()
            throws java.io.IOException
closes the wrapped stream and then executes the action, that has been defined to take place at the end of the stream

Specified by:
finish in interface FinishingActionStream
Throws:
java.io.IOException - if something goes wrong

flush

public void flush()
           throws java.io.IOException

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

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException

Overrides:
write in class java.io.OutputStream
Throws:
java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException

Overrides:
write in class java.io.OutputStream
Throws:
java.io.IOException

write

public void write(int b)
           throws java.io.IOException

Specified by:
write in class java.io.OutputStream
Throws:
java.io.IOException

getCount

public long getCount()
Returns the number of bytes written to the stream

Specified by:
getCount in interface FinishingActionStream
Returns:
number of bytes written since beginning or last reset

resetCount

public long resetCount()
Resets the number of bytes written to zero, to start again counting.

Specified by:
resetCount in interface FinishingActionStream
Returns:
the previous content of the counter, that has now been set to 0

exceptionBeforeFinishing

public java.io.IOException exceptionBeforeFinishing()
Captures any error/exception/throwable or exception that occured right before executing the finishing action, in particular any IOException that occured when closing the stream.
By this, the action can decide how to deal with this exception. If this error is not reset, it will be thrown after the action has been performed.

Specified by:
exceptionBeforeFinishing in interface FinishingActionStream
Returns:
the exception, that has been captured

resetExceptionBeforeFinishing

public void resetExceptionBeforeFinishing()
Resets any exception that has been captured before executing the finishing action, such that it will not be thrown after finishing action was performed.

Specified by:
resetExceptionBeforeFinishing in interface FinishingActionStream

incrementCount

public void incrementCount(long valueToAdd)
                    throws java.io.IOException
Increments the value of count by the given value.

Specified by:
incrementCount in interface FinishingActionStream
Parameters:
valueToAdd - the value to add to the counter
Throws:
java.io.IOException - if something goes wrong

getFinishingAction

public FinishingAction getFinishingAction()
Retruns the assigned finishing action

Specified by:
getFinishingAction in interface FinishingActionStream
Returns:
the action

setFinishingAction

public void setFinishingAction(FinishingAction finishingAction)
Sets the action that should be performed for finishing processing of the stream

Specified by:
setFinishingAction in interface FinishingActionStream
Parameters:
finishingAction - the action

getWrappedStream

public java.lang.Object getWrappedStream()
Returns the wrapped stream on which the stream operates

Specified by:
getWrappedStream in interface FinishingActionStream
Returns:
the wrapped stream

setWrappedStream

public void setWrappedStream(java.lang.Object wrappedStream)
Sets the wrapped stream on which the stream operates

Specified by:
setWrappedStream in interface FinishingActionStream
Parameters:
wrappedStream - the wrapped stream