ikrs.httpd.resource
Class InterruptableResource

java.lang.Object
  extended by ikrs.httpd.resource.AbstractResource
      extended by ikrs.httpd.resource.InterruptableResource
All Implemented Interfaces:
Resource

public class InterruptableResource
extends AbstractResource

The interruptable resource does not refer to Thread interruption, but to a Resource that allows to stop the read process at a random point, then resuming it (after the last read byte) and telling the observer the read process would be at byte position 0 (adjusting the old length value, off course). It can be used to read a portion of data from the beginning of the underlying input stream (such as header data), then stopping and - after processing the received data - passing the resource to a different instance telling the byte position would be still at the beginning of the stream. This should be used to avoid copying the rest of the stream into a new buffer resource, but continue reading at the current position. The method for resetting the stream index is the 'resetBytePosition()' method. Note that this class uses the ikrs.io.BytePositionInputStream, which does not support the 'mark()' and 'reset()' methods yet.


Constructor Summary
InterruptableResource(HTTPHandler handler, CustomLogger logger, Resource resource, boolean useFairLocks)
          Create a new InterruptableResource from the given Resource.
 
Method Summary
 boolean close()
          Closes this resource.
 long getBytePosition()
          Get the current read position (since initialization or last 'resetBytePosition()' call).
 BytePositionInputStream getInputStream()
          Get the input stream from this resource.
 long getLength()
          This method returns the *actual* length of the underlying resource.
 java.io.OutputStream getOutputStream()
          Get the output stream to this resource.
 boolean isOpen()
          This method determines if this resource was alerady opened or not.
 boolean isReadOnly()
          This method returns true if the underlying resource is read-only (in general).
 void open(boolean readOnly)
          This method opens the underlying resource.
 long resetBytePosition()
          Resets the current byte position to 0.
 
Methods inherited from class ikrs.httpd.resource.AbstractResource
getHTTPHandler, getHypertextAccessFile, getLogger, getMetaData, getReadLock, getWriteLock, setHypertextAccessFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterruptableResource

public InterruptableResource(HTTPHandler handler,
                             CustomLogger logger,
                             Resource resource,
                             boolean useFairLocks)
                      throws java.lang.NullPointerException
Create a new InterruptableResource from the given Resource.

Parameters:
logger - A custom logger to write log messages to (must not be null).
pb - The process builder to use (must not be null).
useFairLocks - If set to true the class will use fair read locks (writing isn't possible at all with this class).
Throws:
java.lang.NullPointerException - If logger or pb is null.
Method Detail

getBytePosition

public long getBytePosition()
Get the current read position (since initialization or last 'resetBytePosition()' call).

Returns:
The current read position (since initialization or last 'resetBytePosition()' call).

resetBytePosition

public long resetBytePosition()
Resets the current byte position to 0.


open

public void open(boolean readOnly)
          throws ReadOnlyException,
                 java.io.IOException
This method opens the underlying resource. Don't forget to close.

Specified by:
open in interface Resource
Specified by:
open in class AbstractResource
Parameters:
readOnly - if set to true, the resource will be opened in read-only mode.
Throws:
ReadOnlyException - If the underlying resource is read-only in general.
java.io.IOException - If any other IO error occurs.
See Also:
isReadOnly()

isOpen

public boolean isOpen()
               throws java.io.IOException
This method determines if this resource was alerady opened or not.

Specified by:
isOpen in interface Resource
Specified by:
isOpen in class AbstractResource
Throws:
java.io.IOException - If any IO error occurs.

isReadOnly

public boolean isReadOnly()
                   throws java.io.IOException
This method returns true if the underlying resource is read-only (in general).

Specified by:
isReadOnly in interface Resource
Specified by:
isReadOnly in class AbstractResource
Throws:
java.io.IOException - If any IO error occurs.

getLength

public long getLength()
               throws java.io.IOException
This method returns the *actual* length of the underlying resource. This length will be used in the HTTP header fields to specify the transaction length. During read-process (you used the locks, didn't you?) the length MUST NOT change.

Specified by:
getLength in interface Resource
Specified by:
getLength in class AbstractResource
Returns:
the length of the resource's data in bytes.
Throws:
java.io.IOException - If any IO error occurs.

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws ReadOnlyException,
                                            java.io.IOException
Get the output stream to this resource.

Specified by:
getOutputStream in interface Resource
Specified by:
getOutputStream in class AbstractResource
Throws:
ReadOnlyException - If this resource was opened with the read-only flag set.
java.io.IOException - If any other IO error occurs.

getInputStream

public BytePositionInputStream getInputStream()
                                       throws java.io.IOException
Get the input stream from this resource.

Specified by:
getInputStream in interface Resource
Specified by:
getInputStream in class AbstractResource
Throws:
java.io.IOException - If any IO error occurs.

close

public boolean close()
              throws java.io.IOException
Closes this resource.

Specified by:
close in interface Resource
Specified by:
close in class AbstractResource
Returns:
false if the resource was already closed, true otherwise.
Throws:
java.io.IOException