Class StorageFs
- java.lang.Object
-
- ej.storage.fs.StorageFs
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description OutputStreamappend(String id)Appends some data to an entry referenced with an ID.booleanexists(String id)Tests whether an entry exists for this ID.protected FilegetFile(String id)Returns the file associated to the given storage id.String[]getIds()Returns all IDs of the stored entries.protected static StringgetRootFolderPropertyOrDefault()Gets the root folder set withej.storage.fs.StorageFs.rootor "storage" if none.longgetSize(String id)Returns the size of the entry stored with an ID.InputStreamload(String id)Loads the data stored with a specific ID ornullif there is none.OutputStreammodify(String id, int offset)Modifies the entry that is referenced by an ID.voidmove(String sourceId, String destinationId)Changes the ID of an entry.voidremove(String id)Removes the entry stored with an ID.OutputStreamstore(String id)Stores data that will be referenced with an ID.
-
-
-
Constructor Detail
-
StorageFs
public StorageFs() throws IOExceptionCreates a storage on file system.The root folder where the files will be created can be specified with the property
ej.storage.fs.StorageFs.root. If not set,"storage"will be used.- Throws:
IOException- if the specified root already exists and is not a directory.IOException- if the specified root cannot be created.
-
StorageFs
public StorageFs(String root) throws IOException
Creates a storage on file system specifying the root folder where the files will be created.- Parameters:
root- the root.- Throws:
IOException- if the specified root already exists and is not a directory.IOException- if the specified root cannot be created.
-
-
Method Detail
-
getRootFolderPropertyOrDefault
protected static String getRootFolderPropertyOrDefault()
Gets the root folder set withej.storage.fs.StorageFs.rootor "storage" if none.- Returns:
- the root folder.
-
store
public OutputStream store(String id) throws IOException
Description copied from interface:StorageStores data that will be referenced with an ID.If an entry is already stored with this ID, its existing data is fully removed and replaced by the given data. Otherwise, a new entry is created.
This operation may block the current thread depending on the implementation.
The returned output stream must be flushed or closed to ensure that the data is available. Example of usage:
try(OutputStream outputStream = store(id)) { outputStream.write(…); outputStream.write(…); }- Specified by:
storein interfaceStorage- Parameters:
id- the ID of the stored entry.- Returns:
- the output stream to write data.
- Throws:
IOException- if an I/O error occurs.
-
modify
public OutputStream modify(String id, int offset) throws IOException
Description copied from interface:StorageModifies the entry that is referenced by an ID.If an entry is already stored with this ID, its existing data is not removed and only the targeted bytes will be overwritten. Otherwise, a new entry is created.
This operation may block the current thread depending on the implementation.
The returned output stream must be flushed or closed to ensure that the data is available.
- Specified by:
modifyin interfaceStorage- Parameters:
id- the ID of the stored entry.offset- the offset at which to write data.- Returns:
- the output stream to write data.
- Throws:
IOException- if an I/O error occurs.
-
getFile
protected File getFile(String id)
Returns the file associated to the given storage id.- Parameters:
id- the storage identifier.- Returns:
- the file associated to the given storage id.
-
append
public OutputStream append(String id) throws IOException
Description copied from interface:StorageAppends some data to an entry referenced with an ID.If an entry is already stored with this ID, its existing data is not removed and the given data will be added to the existing entry. Otherwise, a new entry is created.
This operation may block the current thread depending on the implementation.
The returned output stream must be flushed or closed to ensure that the data is available. Example of usage:
try(OutputStream outputStream = append(id)) { outputStream.write(…); outputStream.write(…); }- Specified by:
appendin interfaceStorage- Parameters:
id- the ID of the stored entry.- Returns:
- the output stream to write data.
- Throws:
IOException- if an I/O error occurs.
-
load
@Nullable public InputStream load(String id) throws IOException
Description copied from interface:StorageLoads the data stored with a specific ID ornullif there is none.The returned input stream must be closed by the caller.
This operation may block the current thread depending on the implementation.
- Specified by:
loadin interfaceStorage- Parameters:
id- the ID of the entry to be returned.- Returns:
- the data of the entry stored with the given ID or
null. - Throws:
IOException- if an I/O error occurs.
-
move
public void move(String sourceId, String destinationId) throws IOException
Description copied from interface:StorageChanges the ID of an entry.If an entry is already stored with the destination ID, it is overwritten.
This operation may block the current thread depending on the implementation.
- Specified by:
movein interfaceStorage- Parameters:
sourceId- the old ID of the entry.destinationId- the new ID of the entry.- Throws:
IOException- if an I/O error occurs.
-
remove
public void remove(String id) throws IOException
Description copied from interface:StorageRemoves the entry stored with an ID.This operation may block the current thread depending on the implementation.
- Specified by:
removein interfaceStorage- Parameters:
id- the ID of the entry to remove.- Throws:
IOException- if an I/O error occurs.
-
getSize
public long getSize(String id) throws IOException
Description copied from interface:StorageReturns the size of the entry stored with an ID.- Specified by:
getSizein interfaceStorage- Parameters:
id- the ID of the entry.- Returns:
- the size of the entry.
- Throws:
IOException- if an I/O error occurs.
-
exists
public boolean exists(String id) throws IOException
Description copied from interface:StorageTests whether an entry exists for this ID.- Specified by:
existsin interfaceStorage- Parameters:
id- the ID to check.- Returns:
trueif the given ID exists,falseotherwise- Throws:
IOException- if an I/O error occurs.
-
getIds
public String[] getIds() throws IOException
Description copied from interface:StorageReturns all IDs of the stored entries.- Specified by:
getIdsin interfaceStorage- Returns:
- an array of all IDs.
- Throws:
IOException- if an I/O error occurs.
-
-