Package net.sf.jasperreports.engine.util
Class JRSwapFile
- java.lang.Object
-
- net.sf.jasperreports.engine.util.JRSwapFile
-
- Direct Known Subclasses:
JRConcurrentSwapFile
public class JRSwapFile extends java.lang.ObjectSwap file implementation that can be used as a disk cache for arbitrary binary data.Fixed-size blocks are allocated inside the swap file when a caller wants to write data. The caller receives a handle to the allocated area based on which it can read the data or free the area.
The implementation is thread-safe. I/O operations are performed in synchronized blocks, only one thread would do a read or write at one moment.
- Author:
- Lucian Chirita (lucianc@users.sourceforge.net)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classJRSwapFile.LongQueuestatic classJRSwapFile.SwapHandle
-
Field Summary
Fields Modifier and Type Field Description protected java.io.RandomAccessFilefilestatic java.lang.StringPROPERTY_DELETE_ON_EXITProperty that instructs whetherdeleteOnExitis to be requested for swap files.protected java.io.FileswapFile
-
Constructor Summary
Constructors Constructor Description JRSwapFile(java.lang.String directory, int blockSize, int minGrowCount)Creates a swap file.JRSwapFile(JasperReportsContext jasperReportsContext, java.lang.String directory, int blockSize, int minGrowCount)Creates a swap file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddispose()Closes and deletes the swap file.protected voidfinalize()voidfree(JRSwapFile.SwapHandle handle)Frees an allocated area.protected voidfreeBlocks(long[] offsets)protected voidread(byte[] data, int dataOffset, int dataLength, long fileOffset)byte[]read(JRSwapFile.SwapHandle handle, boolean free)Reads all the data from an allocated area.protected long[]reserveFreeBlocks(int blockCount)java.lang.StringtoString()JRSwapFile.SwapHandlewrite(byte[] data)Allocates an area in the swap file and writes data in it.protected voidwrite(byte[] data, int dataSize, int dataOffset, long fileOffset)
-
-
-
Field Detail
-
PROPERTY_DELETE_ON_EXIT
public static final java.lang.String PROPERTY_DELETE_ON_EXIT
Property that instructs whetherdeleteOnExitis to be requested for swap files. Swap files are explicitly deleted ondispose()and garbage collection.- See Also:
- Constant Field Values
-
swapFile
protected final java.io.File swapFile
-
file
protected final java.io.RandomAccessFile file
-
-
Constructor Detail
-
JRSwapFile
public JRSwapFile(java.lang.String directory, int blockSize, int minGrowCount)Creates a swap file. The file name is generated automatically.- Parameters:
directory- the directory where the file should be created.blockSize- the size of the blocks allocated by the swap fileminGrowCount- the minimum number of blocks by which the swap file grows when full
-
JRSwapFile
public JRSwapFile(JasperReportsContext jasperReportsContext, java.lang.String directory, int blockSize, int minGrowCount)
Creates a swap file. The file name is generated automatically.- Parameters:
jasperReportsContext- the JasperReportsContext to read configuration from.directory- the directory where the file should be created.blockSize- the size of the blocks allocated by the swap fileminGrowCount- the minimum number of blocks by which the swap file grows when full
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
write
public JRSwapFile.SwapHandle write(byte[] data) throws java.io.IOException
Allocates an area in the swap file and writes data in it.- Parameters:
data- the data for which to allocate an area in the file- Returns:
- a handle to the allocated area
- Throws:
java.io.IOException
-
write
protected void write(byte[] data, int dataSize, int dataOffset, long fileOffset) throws java.io.IOException- Throws:
java.io.IOException
-
read
public byte[] read(JRSwapFile.SwapHandle handle, boolean free) throws java.io.IOException
Reads all the data from an allocated area.- Parameters:
handle- the allocated area handlefree- whether to free the area after reading- Returns:
- the whole data saved in an allocated area
- Throws:
java.io.IOException
-
read
protected void read(byte[] data, int dataOffset, int dataLength, long fileOffset) throws java.io.IOException- Throws:
java.io.IOException
-
free
public void free(JRSwapFile.SwapHandle handle)
Frees an allocated area.- Parameters:
handle- the allocated area handle
-
dispose
public void dispose()
Closes and deletes the swap file.
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
reserveFreeBlocks
protected long[] reserveFreeBlocks(int blockCount) throws java.io.IOException- Throws:
java.io.IOException
-
freeBlocks
protected void freeBlocks(long[] offsets)
-
-