ikrs.httpd.resource
Class BufferedResource

java.lang.Object
  extended by ikrs.httpd.resource.AbstractResource
      extended by ikrs.httpd.resource.BufferedResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
ReplacingResource

public class BufferedResource
extends AbstractResource

There are cases when it is not predictable how many bytes a data source will produce until the input stream has reached EOF. But for HTTP resources it is not acceptable to return an unprecise or aproximate value for Resource.getLength() because this value will be stored in the HTTP resonse's header data for the 'Content-Length' field (as soon as the data output started the header field was sent and cannot be modified any more)! For those unpredictable cases we need buffered resources that load the _complete_ data into an internal buffer to gain the exact number of bytes. Handle with care! Some underlying resources might hold a large number of bytes which might cause out-of-memory errors or stack overflows when this BufferedResource tries to allocate enought memory to store the data.


Constructor Summary
BufferedResource(HTTPHandler handler, CustomLogger logger, java.io.InputStream in, boolean useFairLocks)
          Create a new BufferedResource.
 
Method Summary
 boolean close()
          Closes this resource.
 java.io.InputStream 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.
 
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

BufferedResource

public BufferedResource(HTTPHandler handler,
                        CustomLogger logger,
                        java.io.InputStream in,
                        boolean useFairLocks)
                 throws java.lang.NullPointerException
Create a new BufferedResource.

Parameters:
handler - The HTTPHandler (may be null if not available).
logger - A custom logger to write log messages to (must not be null).
in - The input stream to read the data for buffering from.
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 the input stream is null.
Method Detail

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 java.io.InputStream 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