public class BinStore extends Object implements Serializable
This is an adaptation of org.apache.commons.fileupload.disk.DiskFileItem.java
After retrieving an instance of this class, you may either request all contents of file at once using
get() or request an InputStream with getInputStream()
and process the file without attempting to load it into memory, which may
come handy with large files.
Temporary files, which are created for file items, should be deleted later on. However, if you do use such a tracker,
then you must consider the following: Temporary files are automatically deleted as soon as they are no longer needed.
(More precisely, when the corresponding instance of File is garbage collected.)
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_THRESHOLD |
| Constructor and Description |
|---|
BinStore() |
BinStore(byte[] data) |
BinStore(int sizeThreshold)
Constructs a new
BinStore instance. |
BinStore(int sizeThreshold,
byte[] data) |
| Modifier and Type | Method and Description |
|---|---|
void |
delete()
Deletes the underlying storage for a file item, including deleting any
associated temporary disk file.
|
protected void |
finalize()
Removes the file contents from the temporary storage.
|
byte[] |
get()
Returns the contents of the file as an array of bytes.
|
InputStream |
getInputStream()
Returns an
InputStream that can be
used to retrieve the contents of the file. |
OutputStream |
getOutputStream() |
OutputStream |
getOutputStream(File directory)
Returns an
OutputStream that can
be used for storing the contents of the file. |
long |
getSize()
Returns the size of the file.
|
File |
getStoreLocation()
Returns the
File object for the FileItem's
data's temporary location on the disk. |
protected File |
getTempFile(File directory)
Creates and returns a
File representing a uniquely
named temporary file in the configured repository path. |
boolean |
isInMemory()
Provides a hint as to whether or not the file contents will be read
from memory.
|
void |
set(byte[] data)
Sets the contents using an array of bytes as a source
|
void |
set(File source)
Sets the contents using a File as a source
|
void |
set(InputStream source)
Sets the contents using a InputStream as a source
|
String |
toString()
Returns a string representation of this object.
|
void |
write(File file)
A convenience method to write an uploaded item to disk.
|
public static final int DEFAULT_THRESHOLD
public BinStore()
public BinStore(byte[] data)
public BinStore(int sizeThreshold,
byte[] data)
public BinStore(int sizeThreshold)
BinStore instance.sizeThreshold - The threshold, in bytes, below which items will be
retained in memory and above which they will be
stored as a file.public InputStream getInputStream() throws IOException
InputStream that can be
used to retrieve the contents of the file.InputStream that can be
used to retrieve the contents of the file.IOException - if an error occurs.public boolean isInMemory()
true if the file contents will be read
from memory; false otherwise.public long getSize()
public byte[] get()
public void set(byte[] data)
data - The array of bytes with the datapublic void set(File source) throws IOException
source - The file with the dataIOExceptionpublic void set(InputStream source) throws IOException
source - The InputStream with the dataIOExceptionpublic void write(File file) throws Exception
This implementation first attempts to rename the uploaded item to the specified destination file, if the item was originally written to disk. Otherwise, the data will be copied to the specified file.
This method is only guaranteed to work once, the first time it is invoked for a particular item. This is because, in the event that the method renames a temporary file, that file will no longer be available to copy or rename again at a later time.
file - The File into which the uploaded item should
be stored.Exception - if an error occurs.public void delete()
FileItem instance is garbage
collected, this method can be used to ensure that this is done at an
earlier time, thus preserving system resources.public OutputStream getOutputStream() throws IOException
IOExceptionpublic OutputStream getOutputStream(File directory) throws IOException
OutputStream that can
be used for storing the contents of the file.directory - The directory where the temporary file is created.null it's created in the default directory.OutputStream that can be used
for storing the contents of the file.IOException - if an error occurs.public File getStoreLocation()
File object for the FileItem's
data's temporary location on the disk. Note that for FileItems that have their data stored in memory,
this method will return null. When handling large
files, you can use File.renameTo(java.io.File) to
move the file to new location without copying the data, if the
source and destination locations reside within the same logical
volume.null if the data is stored in
memory.protected void finalize()
protected File getTempFile(File directory)
File representing a uniquely
named temporary file in the configured repository path. The lifetime of
the file is tied to the lifetime of the FileItem instance;
the file will be deleted when the instance is garbage collected.directory - The directory where this temporary file is createdFile to be used for temporary storage.Copyright © 2019. All rights reserved.