Class SandboxedStorage
- java.lang.Object
-
- com.microej.kf.util.storage.SandboxedStorage
-
-
Constructor Summary
Constructors Constructor Description SandboxedStorage()Creates a sandboxed storage on file system.SandboxedStorage(String root)Creates a storage on file system specifying the root folder where the root folder will be created.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description OutputStreamappend(String s)Appends some data to an entry referenced with an ID.booleanexists(String s)Tests whether an entry exists for this ID.String[]getIds()Returns all IDs of the stored entries.StringgetRootFolder(Module module)Gets the root folder of the storage service for a given module.longgetSize(String s)Returns the size of the entry stored with an ID.InputStreamload(String s)Loads the data stored with a specific ID ornullif there is none.OutputStreammodify(String s, int i)Modifies the entry that is referenced by an ID.voidmove(String s, String s1)Changes the ID of an entry.voidremove(String s)Removes the entry stored with an ID.OutputStreamstore(String s)Stores data that will be referenced with an ID.
-
-
-
Constructor Detail
-
SandboxedStorage
public SandboxedStorage() throws IOExceptionCreates a sandboxed storage on file system.The root folder is determined dynamically depending on the KF context. Each module has a different one.
- Throws:
IOException- if the root cannot be created.- See Also:
Kernel.getContextOwner(),StorageFs()
-
SandboxedStorage
public SandboxedStorage(String root) throws IOException
Creates a storage on file system specifying the root folder where the root folder will be created.The root folder is determined dynamically depending on the KF context. Each module has a different one.
- Parameters:
root- the root folder.- Throws:
IllegalArgumentException- if the root already exists and is not a directory.IOException- if the root cannot be created.
-
-
Method Detail
-
getRootFolder
public String getRootFolder(Module module)
Gets the root folder of the storage service for a given module.- Parameters:
module- the module.- Returns:
- the root folder.
-
store
public OutputStream store(String s) 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:
s- 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 s, int i) 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:
s- the ID of the stored entry.i- the offset at which to write data.- Returns:
- the output stream to write data.
- Throws:
IOException- if an I/O error occurs.
-
append
public OutputStream append(String s) 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:
s- the ID of the stored entry.- Returns:
- the output stream to write data.
- Throws:
IOException- if an I/O error occurs.
-
load
public InputStream load(String s) 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:
s- 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 s, String s1) 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:
s- the old ID of the entry.s1- the new ID of the entry.- Throws:
IOException- if an I/O error occurs.
-
remove
public void remove(String s) 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:
s- the ID of the entry to remove.- Throws:
IOException- if an I/O error occurs.
-
getSize
public long getSize(String s) throws IOException
Description copied from interface:StorageReturns the size of the entry stored with an ID.- Specified by:
getSizein interfaceStorage- Parameters:
s- the ID of the entry.- Returns:
- the size of the entry.
- Throws:
IOException- if an I/O error occurs.
-
exists
public boolean exists(String s) throws IOException
Description copied from interface:StorageTests whether an entry exists for this ID.- Specified by:
existsin interfaceStorage- Parameters:
s- 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.
-
-