org.deegree.coverage.raster.cache
Class CacheRasterReader

java.lang.Object
  extended by org.deegree.coverage.raster.io.grid.GridReader
      extended by org.deegree.coverage.raster.io.grid.GridFileReader
          extended by org.deegree.coverage.raster.cache.CacheRasterReader
All Implemented Interfaces:
RasterReader

public class CacheRasterReader
extends GridFileReader

The CacheRasterReader is a grid based caching mechanism for raster readers.

Version:
$Revision: 31644 $, $Date: 2011-08-24 22:27:43 +0200 (Wed, 24 Aug 2011) $
Author:
Rutger Bezema, last edited by: $Author: mschneider $

Field Summary
 
Fields inherited from class org.deegree.coverage.raster.io.grid.GridReader
infoFile, sampleSize
 
Constructor Summary
CacheRasterReader(ByteBuffer filledBuffer, int width, int height, File cacheFile, boolean shouldCreateCachefile, RasterDataInfo dataInfo, RasterGeoReference geoReference, RasterCache cache)
          Create a cached raster from the given bytebuffer.
CacheRasterReader(RasterReader cachedReader, File cacheFile, RasterCache cache)
           
 
Method Summary
 long cacheFileSize()
           
 boolean canCreateCacheFile()
           
 boolean canLoad(File filename)
          Check if the raster reader is able to read the given raster file.
 long clear(boolean deleteCacheFile)
          Clears all memory buffers and deletes the cache file if requested.
static CacheRasterReader createFromCache(RasterReader reader, File cacheFile, RasterCache cache)
          Creates a CachedRasterReader from the given cacheFile.
 long currentApproxMemory()
           
 boolean deleteCacheFile()
          Delete the file this cache reader is using.
 void dispose()
          Signals the reader that it should try to dispose all in memory data.
 long dispose(boolean memoryBuffersAsWell)
          Causes the cachefile to be written (if existing) and the memory buffers to be set to null.
 boolean equals(Object other)
           
 void flush()
          Writes all current in memory byte buffers to the cache file (if existing).
 String getDataLocationId()
           
 RasterGeoReference getGeoReference()
           
 int getHeight()
           
 Set<String> getSupportedFormats()
           
 int getWidth()
           
 int hashCode()
          Implementation as proposed by Joshua Block in Effective Java (Addison-Wesley 2001), which supplies an even distribution and is relatively fast.
 long lastReadAccess()
          Return the current time millis of the last read action.
 AbstractRaster load(File gridFile, RasterIOOptions options)
          Read the given raster file into an abstract raster.
 AbstractRaster load(InputStream stream, RasterIOOptions options)
          Read the given input stream into an abstract raster.
protected  void read(int columnId, int rowId, ByteBuffer buffer)
          Reads the data from the grid.
 BufferResult read(RasterRect rect, ByteBuffer resultBuffer)
           
 boolean shouldCreateCacheFile()
           
 
Methods inherited from class org.deegree.coverage.raster.io.grid.GridFileReader
file, instantiate, leaveStreamOpen, readValuesFromTile
 
Methods inherited from class org.deegree.coverage.raster.io.grid.GridReader
allocateTileBuffer, canReadTiles, getBytesPerTile, getEnvelope, getIntersectingTiles, getNumberOfTiles, getRasterDataInfo, getTile, getTileColumns, getTileData, getTileEnvelope, getTileId, getTileRasterHeight, getTileRasterWidth, getTileRows, getTilesPerBlob, instantiate, setTilesPerBlob, snapToGrid
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheRasterReader

public CacheRasterReader(ByteBuffer filledBuffer,
                         int width,
                         int height,
                         File cacheFile,
                         boolean shouldCreateCachefile,
                         RasterDataInfo dataInfo,
                         RasterGeoReference geoReference,
                         RasterCache cache)
Create a cached raster from the given bytebuffer.

Parameters:
filledBuffer - with values.
width - of the raster for which this buffer is valid.
height - of the raster for which this buffer is valid.
cacheFile - to use for storage
shouldCreateCachefile - if writing to disk should be enabled.
dataInfo - defining the data
geoReference - of the raster
cache - used.

CacheRasterReader

public CacheRasterReader(RasterReader cachedReader,
                         File cacheFile,
                         RasterCache cache)
Parameters:
cachedReader -
cacheFile -
cache -
Method Detail

createFromCache

public static CacheRasterReader createFromCache(RasterReader reader,
                                                File cacheFile,
                                                RasterCache cache)
Creates a CachedRasterReader from the given cacheFile.

Parameters:
reader - which backs the cache
cacheFile - to instantiate from
cache - manager to use.
Returns:
a CacheRasterReader or null if the cacheFile could not be read.

load

public AbstractRaster load(File gridFile,
                           RasterIOOptions options)
                    throws IOException
Description copied from interface: RasterReader
Read the given raster file into an abstract raster.

Specified by:
load in interface RasterReader
Overrides:
load in class GridFileReader
Returns:
the loaded raster
Throws:
IOException - may be thrown when there is a problem with reading the raster.

read

protected void read(int columnId,
                    int rowId,
                    ByteBuffer buffer)
             throws IOException
Description copied from class: GridReader
Reads the data from the grid.

Overrides:
read in class GridFileReader
Throws:
IOException

read

public BufferResult read(RasterRect rect,
                         ByteBuffer resultBuffer)
                  throws IOException
Specified by:
read in interface RasterReader
Overrides:
read in class GridFileReader
Parameters:
rect - to read.
resultBuffer - to store the result in
Returns:
the read buffer and its domain
Throws:
IOException

clear

public long clear(boolean deleteCacheFile)
Clears all memory buffers and deletes the cache file if requested. Note this method will not write any data to the cachefile.

Parameters:
deleteCacheFile - true if the cache file should be deleted as well.
Returns:
the number of memory bytes freed up after cleaning.

deleteCacheFile

public boolean deleteCacheFile()
Delete the file this cache reader is using.

Returns:
true if this cached reader no longer references an existing file.

canLoad

public boolean canLoad(File filename)
Description copied from interface: RasterReader
Check if the raster reader is able to read the given raster file.

Specified by:
canLoad in interface RasterReader
Overrides:
canLoad in class GridFileReader
Returns:
true if the class can read the raster

getSupportedFormats

public Set<String> getSupportedFormats()
Specified by:
getSupportedFormats in interface RasterReader
Overrides:
getSupportedFormats in class GridFileReader
Returns:
a Set of (image) formats mime/types the implementation is able to read.

load

public AbstractRaster load(InputStream stream,
                           RasterIOOptions options)
                    throws IOException
Description copied from interface: RasterReader
Read the given input stream into an abstract raster.

Specified by:
load in interface RasterReader
Overrides:
load in class GridReader
Returns:
the loaded raster
Throws:
IOException - may be thrown when there is a problem with reading the raster.

shouldCreateCacheFile

public boolean shouldCreateCacheFile()
Specified by:
shouldCreateCacheFile in interface RasterReader
Overrides:
shouldCreateCacheFile in class GridReader
Returns:
true if a cache file should be created for the read raster.

getGeoReference

public RasterGeoReference getGeoReference()
Specified by:
getGeoReference in interface RasterReader
Overrides:
getGeoReference in class GridReader
Returns:
the geo reference of the read raster

getHeight

public int getHeight()
Specified by:
getHeight in interface RasterReader
Overrides:
getHeight in class GridReader
Returns:
the height (in pixels) of the read raster

getWidth

public int getWidth()
Specified by:
getWidth in interface RasterReader
Overrides:
getWidth in class GridReader
Returns:
the width (in pixels) of the read raster

getDataLocationId

public String getDataLocationId()
Specified by:
getDataLocationId in interface RasterReader
Overrides:
getDataLocationId in class GridFileReader
Returns:
a String which identifies this reader for used the data source, for example the file name, or an url.

lastReadAccess

public long lastReadAccess()
Return the current time millis of the last read action. This method is needed for caching.

Returns:
the System.currentTimeMillis() of the last read operation.

currentApproxMemory

public long currentApproxMemory()
Returns:
the current amount of memory this cached reader has on byte buffers.

cacheFileSize

public long cacheFileSize()
Returns:
the size of the cache file on disk.

dispose

public void dispose()
Description copied from interface: RasterReader
Signals the reader that it should try to dispose all in memory data.

Specified by:
dispose in interface RasterReader
Overrides:
dispose in class GridFileReader

dispose

public long dispose(boolean memoryBuffersAsWell)
Causes the cachefile to be written (if existing) and the memory buffers to be set to null.

Parameters:
memoryBuffersAsWell - if true the memorybuffers (the ones which don't have rasterfiles to back them up) will be deleted as well.
Returns:
the amount of freed memory.

flush

public void flush()
Writes all current in memory byte buffers to the cache file (if existing).


canCreateCacheFile

public boolean canCreateCacheFile()
Returns:
true if this reader can create a cachefile for it's in memory buffers.

equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Implementation as proposed by Joshua Block in Effective Java (Addison-Wesley 2001), which supplies an even distribution and is relatively fast. It is created from field f as follows:

Combining the hash code(s) computed above: result = 37 * result + code;

Overrides:
hashCode in class Object
Returns:
(int) ( result >>> 32 ) ^ (int) result;
See Also:
Object.hashCode()


Copyright © 2011. All Rights Reserved.