org.backsource.utils.io
Interface CachedStream

All Known Implementing Classes:
ByteCachedStream

public interface CachedStream

Helper class to work with cached streams.

A CachedStream should normally/allways be obtained through the CachedStreamFactory.

A cached stream is a stream that is cached in memory or some other place like in a file.They should basically be used when the same stream will be use (emptied) a multitude of times and it is important to do this without a lot of work.

Use it when you need to get data from an external source and reuse it.

  CachedStreamFactory factory = CachedStreamFactory.newInstance();
  CachedStream cache = factory.getCachedStream();
  OutputStream os = cache.getOutputStream();
  // Let external write to it
  ftp.get("filename",os);
  // Get a reusable input stream
  CachedInputStream is = cache.getInputStream( os );
 

This is also possible when you get an InputStream.

   InputStream is = url.openStream();
   CachedInputStream cis = cache.getInputStream( is );
 

The getInputStream know if it gets a CachedInputStream and will return that, so in situations where stuff takes an InputStream which might already be a CachedInput stream, just use getInputStream. The helper method rewind will rewind the stream, if only if the stream is really rewindable: otherwise it will raise an exception.

CachedInputStreams are not possible to close through the normal close method because there are a lot of utilities out there that will close your stream for you under the hood. Use CachedInputStream.close(boolean) to really close the stream.

Version:
$Revision: 1.1.1.1 $
Author:
Peter Antman

Method Summary
 CachedInputStream getInputStream(CachedOutputStream cout)
          Get a rewindable input stream from the given CacheOutputStream.
 CachedInputStream getInputStream(java.io.InputStream is)
          Get a rewindable inputstream from the given input stream, if the stream is a Rewindable input stream, it is just returned.
 CachedOutputStream getOutputStream()
          Get a CachedOutputStream.
 java.lang.String getProperty(java.lang.String name)
          Chech if property is set.
 void rewind(java.io.InputStream is)
          Rewind this stream if it is rewindable, otherwise throw an IOException.
 void setProperty(java.lang.String name, java.lang.String value)
          Set a CacheStream specific property.
 

Method Detail

getOutputStream

public CachedOutputStream getOutputStream()
                                   throws java.io.IOException
Get a CachedOutputStream.

The typical usecase for this is to later use it to get a CachedInputStream from it.

Throws:
java.io.IOException

getInputStream

public CachedInputStream getInputStream(CachedOutputStream cout)
                                 throws java.io.IOException
Get a rewindable input stream from the given CacheOutputStream.

Throws:
java.io.IOException - if the CachedOutputStream is not of the correct type (not from the same cache) or if an underlying IOException happens.

getInputStream

public CachedInputStream getInputStream(java.io.InputStream is)
                                 throws java.io.IOException
Get a rewindable inputstream from the given input stream, if the stream is a Rewindable input stream, it is just returned.

Throws:
java.io.IOException

rewind

public void rewind(java.io.InputStream is)
            throws java.io.IOException
Rewind this stream if it is rewindable, otherwise throw an IOException.

Throws:
java.io.IOException

setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value)
Set a CacheStream specific property.


getProperty

public java.lang.String getProperty(java.lang.String name)
Chech if property is set.



Copyright © 2000-2004 Backsource. All Rights Reserved.