org.gcube.contentmanagement.baselayer.streamutils
Class FinishingActionInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.gcube.contentmanagement.baselayer.streamutils.FinishingActionInputStream
All Implemented Interfaces:
java.io.Closeable, FinishingActionStream
Direct Known Subclasses:
CountExceedsActionInputStream

public class FinishingActionInputStream
extends java.io.InputStream
implements FinishingActionStream

This class provites a generic wrapper for InputStreams, which are decorated with some action that should be performed whenever the end of the stream has been reached.


Constructor Summary
FinishingActionInputStream(java.io.InputStream wrappedStream, FinishingAction action)
          wrapps a stream with some action, that will be executed once, when the end of the stream has been reached for the first time.
 
Method Summary
 int available()
          
 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
 long getCount()
          Returns the number of bytes read from or skipped on 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.
 void mark(int readlimit)
          
 boolean markSupported()
          
 int read()
          
 int read(byte[] b)
          
 int read(byte[] b, int off, int len)
          
 void reset()
          
 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
 long skip(long n)
          
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FinishingActionInputStream

public FinishingActionInputStream(java.io.InputStream wrappedStream,
                                  FinishingAction action)
wrapps a stream with some action, that will be executed once, when the end of the stream has been reached for the first time.

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

read

public int read()
         throws java.io.IOException

Specified by:
read in class java.io.InputStream
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

close

public void close()
           throws java.io.IOException

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

read

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

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException

Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

mark

public void mark(int readlimit)

Overrides:
mark in class java.io.InputStream

markSupported

public boolean markSupported()

Overrides:
markSupported in class java.io.InputStream

reset

public void reset()
           throws java.io.IOException

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException

getCount

public long getCount()
Returns the number of bytes read from or skipped on the stream.

This is the actual number counted, if a stream supports marking - in other words, if mark(int) and reset has been used, some bytes might get be counted more than once!

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