com.groovemanager.thread
Class SaveFileThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.groovemanager.thread.ProgressThread
          extended by com.groovemanager.thread.SaveFileThread
All Implemented Interfaces:
java.lang.Runnable

public class SaveFileThread
extends ProgressThread

This ProgressThread implementation can be used for writing data from an InputStream to an OutputStream

Author:
Manu Robledo

Nested Class Summary
 
Nested classes inherited from class java.lang.Thread
java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  byte[] buffer
          Temporary buffer
protected  boolean closeAtEnd
          Indicates, whether the streams should be closed when finishing the read/write operation or not
static int DEFAULT_BUFFER_SIZE
          The default buffer size to use
protected  int frameLength
          The length in frames to be written
protected  int frameSize
          The size of one frame
protected  java.io.InputStream in
          The InputStream to read from
protected  int numBytesRead
          The number of bytes read in the last read operation.
protected  java.io.OutputStream out
          The OutputStream to write to
protected  int written
          The number of bytes written so far
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SaveFileThread(java.io.InputStream in, java.io.OutputStream out, int frameLength, int frameSize)
          Create a new SaveFileThread using the default buffer size.
SaveFileThread(java.io.InputStream in, java.io.OutputStream out, int frameLength, int frameSize, boolean closeAtEnd)
          Create a new SaveFileThread using the default buffer size.
SaveFileThread(java.io.InputStream in, java.io.OutputStream out, int frameLength, int frameSize, boolean closeAtEnd, int bufferSize)
          Create a new SaveFileThread
SaveFileThread(java.io.InputStream in, java.io.OutputStream out, int frameLength, int frameSize, int bufferSize)
          Create a new SaveFileThread.
 
Method Summary
protected  boolean breakCondition()
          The break condition to indicate that the operation has finished
protected  void cleanUp()
          Free any ressources and perform clean up operations.
protected  void init()
          Any kind of initialization in the moment right before starting the operation should be performed here
protected  void processNext()
          Perform the next step.
protected  java.lang.Object result()
          Specify the result of this operation, if any.
protected  int tellElapsed()
          Tell the length of the operation elapsed so for
protected  int tellTotal()
          Tell the total length of the operation
 
Methods inherited from class com.groovemanager.thread.ProgressThread
cancelOperation, getElapsed, getErrorMessage, getResult, getTotal, hasFinished, run, setMonitor
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffer

protected byte[] buffer
Temporary buffer


closeAtEnd

protected boolean closeAtEnd
Indicates, whether the streams should be closed when finishing the read/write operation or not


DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default buffer size to use

See Also:
Constant Field Values

frameLength

protected int frameLength
The length in frames to be written


frameSize

protected int frameSize
The size of one frame


in

protected java.io.InputStream in
The InputStream to read from


numBytesRead

protected int numBytesRead
The number of bytes read in the last read operation. If this value is -1, it indicates that the end of the InputStream has been reached


out

protected java.io.OutputStream out
The OutputStream to write to


written

protected int written
The number of bytes written so far

Constructor Detail

SaveFileThread

public SaveFileThread(java.io.InputStream in,
                      java.io.OutputStream out,
                      int frameLength,
                      int frameSize)
Create a new SaveFileThread using the default buffer size. This instance will not close the given streams when ready

Parameters:
in - The InputStream to read from
out - The OutputStream to write to
frameLength - The number of frames to write
frameSize - The size of one frame in bytes

SaveFileThread

public SaveFileThread(java.io.InputStream in,
                      java.io.OutputStream out,
                      int frameLength,
                      int frameSize,
                      boolean closeAtEnd)
Create a new SaveFileThread using the default buffer size.

Parameters:
in - The InputStream to read from
out - The OutputStream to write to
frameLength - The number of frames to write
frameSize - The size of one frame in bytes
closeAtEnd - true, if the given streams should be closed when ready, false otherwise

SaveFileThread

public SaveFileThread(java.io.InputStream in,
                      java.io.OutputStream out,
                      int frameLength,
                      int frameSize,
                      boolean closeAtEnd,
                      int bufferSize)
Create a new SaveFileThread

Parameters:
in - The InputStream to read from
out - The OutputStream to write to
frameLength - The number of frames to write
frameSize - The size of one frame in bytes
closeAtEnd - true, if the given streams should be closed when ready, false otherwise
bufferSize - The buffer size in bytes to be used

SaveFileThread

public SaveFileThread(java.io.InputStream in,
                      java.io.OutputStream out,
                      int frameLength,
                      int frameSize,
                      int bufferSize)
Create a new SaveFileThread. This instance will not close the given streams when ready

Parameters:
in - The InputStream to read from
out - The OutputStream to write to
frameLength - The number of frames to write
frameSize - The size of one frame in bytes
bufferSize - The buffer size in bytes to be used
Method Detail

breakCondition

protected boolean breakCondition()
Description copied from class: ProgressThread
The break condition to indicate that the operation has finished

Specified by:
breakCondition in class ProgressThread
Returns:
true, if the operation has finished and the thread should therefore come to its end, false otherwise

cleanUp

protected void cleanUp()
Description copied from class: ProgressThread
Free any ressources and perform clean up operations. This methos will be called, if the operation has been cancelled, if an Exception is thrown by processNext(), or if the operation has been finished as expected.

Specified by:
cleanUp in class ProgressThread

init

protected void init()
Description copied from class: ProgressThread
Any kind of initialization in the moment right before starting the operation should be performed here

Specified by:
init in class ProgressThread

processNext

protected void processNext()
Description copied from class: ProgressThread
Perform the next step. This method will be called repeatedly as long as
- tellElpased() < tellTotal() and
- breakCondition() != true and
- The thread has not been cancelled

Specified by:
processNext in class ProgressThread

result

protected java.lang.Object result()
Description copied from class: ProgressThread
Specify the result of this operation, if any.

Specified by:
result in class ProgressThread
Returns:
Any kind of object representing the result of the performed operation. null should onlx be returned, if the operation didnīt finish correctly. If it did finish correctly, but it has no result, a dummy object should be returned.

tellElapsed

protected int tellElapsed()
Description copied from class: ProgressThread
Tell the length of the operation elapsed so for

Specified by:
tellElapsed in class ProgressThread
Returns:
The elapsed length of the operation. The unit to use for this value is free defineable

tellTotal

protected int tellTotal()
Description copied from class: ProgressThread
Tell the total length of the operation

Specified by:
tellTotal in class ProgressThread
Returns:
The total length of the operation. The unit to use for this value is free defineable