/** * Retrieves an array of all tiles in the cache which are owned by the * specified image. * * @param owner The <code>RenderedImage</code> to which the tiles belong. * @return An array of all tiles owned by the specified image or * <code>null</code> if there are none currently in the cache. * @since JAI 1.1 */ public Raster[] getTiles(RenderedImage owner) { return getTiles(owner, getTileIndices(owner)); }
/** * Adds a tile to the cache. * * @param owner The <code>RenderedImage</code> that the tile belongs to. * @param tileX The X index of the tile in the owner's tile grid. * @param tileY The Y index of the tile in the owner's tile grid. * @param tile A <code>Raster</code> containing the tile data. */ public void add(RenderedImage owner, int tileX, int tileY, Raster tile) { add(owner, tileX, tileY, tile, null); }
/** * Advises the cache that a tile is no longer needed. It is legal * to implement this method as a no-op. * * @param owner The <code>RenderedImage</code> that the tile belongs to. * @param tileX The X index of the tile in the owner's tile grid. * @param tileY The Y index of the tile in the owner's tile grid. */ public void remove(RenderedImage owner, int tileX, int tileY) { removeTile(createTileId(owner, tileX, tileY)); }
/** * Advises the cache that all tiles associated with a given image * are no longer needed. It is legal to implement this method as * a no-op. * * @param owner The <code>RenderedImage</code> owner of the tiles * to be removed. */ public void removeTiles(RenderedImage owner) { Point[] tileIndices = getTileIndices(owner); for (Point tileIndex : tileIndices) { remove(owner, tileIndex.x, tileIndex.y); } }
TileId tileId = createTileId(owner, tileX, tileY); synchronized (this) { CachedTileImpl cachedTile = tileMap.get(tileId); try { if (cachedTile != null) { writeTile(cachedTile.file, tile); cachedTile.tileTimeStamp = System.currentTimeMillis(); } else { cachedTile = new CachedTileImpl(tileId, tile, tileCacheMetric); if (memoryInUse + cachedTile.tileSize > memoryThreshold) { memoryControl(); writeTile(cachedTile.file, tile); tileMap.put(tileId, cachedTile); memoryInUse += cachedTile.tileSize;
public void testGetImageId() { FileTileCache cache = new FileTileCache(new File(".")); TiledImage image1 = createImage(); TiledImage image2 = createImage(); TiledImage image3 = createImage(); String id1 = cache.getImageId(image1); String id2 = cache.getImageId(image2); String id3 = cache.getImageId(image3); assertNotNull(id1); assertNotNull(id2); assertNotNull(id3); assertTrue(!id1.equals(id2)); assertTrue(!id1.equals(id3)); assertTrue(!id2.equals(id3)); assertSame(id1, cache.getImageId(image1)); assertSame(id2, cache.getImageId(image2)); assertSame(id3, cache.getImageId(image3)); }
/** * Advises the cache that some of its tiles may be discarded. It * is legal to implement this method as a no-op. * * @since JAI 1.1 */ public synchronized void memoryControl() { CachedTileImpl[] tiles = tileMap.values().toArray(new CachedTileImpl[tileMap.size()]); Arrays.sort(tiles, createTileComparator()); for (CachedTileImpl tile : tiles) { if (memoryInUse <= memoryThreshold * memoryCapacity) { break; } removeTile(tile.tileId); } }
/** * Retrieves a tile. Returns <code>null</code> if the tile is not * present in the cache. * * @param owner The <code>RenderedImage</code> that the tile belongs to. * @param tileX The X index of the tile in the owner's tile grid. * @param tileY The Y index of the tile in the owner's tile grid. */ public Raster getTile(RenderedImage owner, int tileX, int tileY) { TileId tileId = createTileId(owner, tileX, tileY); Raster tile = null; synchronized (this) { CachedTileImpl cachedTile = tileMap.get(tileId); if (cachedTile != null) { try { DataBuffer dataBuffer = readTileData(cachedTile.file, cachedTile.sampleModel); if (cachedTile.writable) { tile = Raster.createWritableRaster(cachedTile.sampleModel, dataBuffer, cachedTile.location); } else { tile = Raster.createRaster(cachedTile.sampleModel, dataBuffer, cachedTile.location); } cachedTile.tileTimeStamp = System.currentTimeMillis(); } catch (IOException e) { // todo - log warning } } } return tile; }
CachedTileImpl(TileId tileId, Raster tile, Object tileCacheMetric) { this.tileId = tileId; this.file = new File(cacheDir, getImageId(tileId.owner.get()) + "-" + tileId.tileX + "-" + tileId.tileY); this.tileCacheMetric = tileCacheMetric; this.sampleModel = tile.getSampleModel(); this.tileSize = sampleModel.getNumDataElements() * DataBuffer.getDataTypeSize(sampleModel.getTransferType()); this.location = tile.getBounds().getLocation(); this.writable = tile instanceof WritableRaster; this.tileTimeStamp = System.currentTimeMillis(); }
public void testGetTileIndices() { TiledImage image = createImage(); assertEquals(3, image.getNumXTiles()); assertEquals(4, image.getNumYTiles()); Point[] indices = FileTileCache.getTileIndices(image); assertNotNull(indices); Point[] expectedIndices = { new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2), new Point(1, 2), new Point(2, 2), new Point(0, 3), new Point(1, 3), new Point(2, 3), }; assertEquals(expectedIndices.length, indices.length); for (int i = 0; i < expectedIndices.length; i++) { assertEquals("i=" + i, expectedIndices[i], indices[i]); } }
/** * Sets the memory capacity to a desired number of bytes. * If the memory capacity is smaller than the amount of * memory currently used by the cache, tiles are flushed * until the <code>TileCache</code>'s memory usage is less than * <code>memoryCapacity</code>. * * @param memoryCapacity The new capacity, in bytes. */ public synchronized void setMemoryCapacity(long memoryCapacity) { long oldCapacity = this.memoryCapacity; this.memoryCapacity = memoryCapacity; if (this.memoryCapacity < oldCapacity) { memoryControl(); } }
/** * Returns an array of tile <code>Raster</code>s from the cache. * Any or all of the elements of the returned array may be * <code>null</code> if the corresponding tile is not in the cache. * The length of the returned array must be the same as that of the * parameter array and the <i>i</i>th <code>Raster</code> in the * returned array must correspond to the <i>i</i>th tile index in * the parameter array. * * @param owner The <code>RenderedImage</code> that the tile belongs to. * @param tileIndices An array of <code>Point</code>s containing the * <code>tileX</code> and <code>tileY</code> indices for each tile. * @since JAI 1.1 */ public Raster[] getTiles(RenderedImage owner, Point[] tileIndices) { Raster[] tiles = new Raster[tileIndices.length]; for (int i = 0; i < tiles.length; i++) { Point tileIndex = tileIndices[i]; tiles[i] = getTile(owner, tileIndex.x, tileIndex.y); } return tiles; }
final String getImageId(RenderedImage owner) { String id = idMap.get(owner); if (id == null) { synchronized (this) { if (id == null) { id = createImageId(owner, idMap.size()); idMap.put(owner, id); } } } return id; }
/** * Sets the <code>memoryThreshold</code> value to a floating * point number that ranges from 0.0 to 1.0. * When the cache memory is full, the memory * usage will be reduced to this fraction of * the total cache memory capacity. For example, * a value of .75 will cause 25% of the memory * to be cleared, while retaining 75%. * * @param memoryThreshold Retained fraction of memory * @throws IllegalArgumentException if the memoryThreshold * is less than 0.0 or greater than 1.0 * @since JAI 1.1 */ public synchronized void setMemoryThreshold(float memoryThreshold) { float oldThreshold = this.memoryThreshold; this.memoryThreshold = memoryThreshold; if (this.memoryThreshold < oldThreshold) { memoryControl(); } }
/** * Adds an array of tiles to the tile cache. * * @param owner The <code>RenderedImage</code> that the tile belongs to. * @param tileIndices An array of <code>Point</code>s containing the * <code>tileX</code> and <code>tileY</code> indices for each tile. * @param tiles The array of tile <code>Raster</code>s containing tile data. * @param tileCacheMetric Object which provides an ordering metric * associated with the <code>RenderedImage</code> owner. * @since JAI 1.1 */ public void addTiles(RenderedImage owner, Point[] tileIndices, Raster[] tiles, Object tileCacheMetric) { for (int i = 0; i < tileIndices.length; i++) { Point tileIndex = tileIndices[i]; add(owner, tileIndex.x, tileIndex.y, tiles[i], tileCacheMetric); } }