public class ScratchFile extends Object implements Closeable
RandomAccess
buffers each having its set of pages (implemented by
ScratchFileBuffer
). A buffer is created calling createBuffer()
.
Pages can be stored in main memory or in a temporary file. A mixed mode is supported storing a certain amount of pages in memory and only the additional ones in temporary file (defined by maximum main memory to be used).
Pages can be marked as 'free' in order to re-use them. For in-memory pages this will release the used memory while for pages in temporary file this simply marks the area as free to re-use.
If a temporary file was created (done with the first page to be stored
in temporary file) it is deleted when close()
is called.
Using this class for RandomAccess
buffers allows for a direct control
on the maximum memory usage and allows processing large files for which we
otherwise would get an OutOfMemoryError
in case of using RandomAccessBuffer
.
This base class for providing pages is thread safe (the buffer implementations are not).
Constructor and Description |
---|
ScratchFile(File scratchFileDirectory)
Initializes page handler.
|
ScratchFile(MemoryUsageSetting memUsageSetting)
Initializes page handler.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes and deletes the temporary file.
|
RandomAccess |
createBuffer()
Creates a new buffer using this page handler.
|
RandomAccess |
createBuffer(InputStream input)
Creates a new buffer using this page handler and initializes it with the
data read from provided input stream (input stream is copied to buffer).
|
static ScratchFile |
getMainMemoryOnlyInstance()
Getter for an instance using only unrestricted main memory for buffering
(same as
new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly()) ). |
public ScratchFile(File scratchFileDirectory) throws IOException
scratchFileDirectory
is supplied,
then the scratch file will be created in that directory.
All pages will be stored in the scratch file.
scratchFileDirectory
- The directory in which to create the scratch file
or null
to created it in the default temporary directory.IOException
- If scratch file directory was given but don't exist.public ScratchFile(MemoryUsageSetting memUsageSetting) throws IOException
scratchFileDirectory
is supplied,
then the scratch file will be created in that directory.
Depending on the size of allowed memory usage a number of pages (memorySize/PAGE_SIZE
)
will be stored in-memory and only additional pages will be written to/read from scratch file.
memUsageSetting
- set how memory/temporary files are used for buffering streams etc.IOException
- If scratch file directory was given but don't exist.public static ScratchFile getMainMemoryOnlyInstance()
new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly())
).public RandomAccess createBuffer() throws IOException
IOException
- If an error occurred.public RandomAccess createBuffer(InputStream input) throws IOException
input
- The input stream that is to be copied into the buffer.IOException
- If an error occurred.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- If there was a problem closing or deleting the temporary file.Copyright © 2002–2018 The Apache Software Foundation. All rights reserved.