public class ShieldedPlug extends Object
A shielded plug is a database that contains several memory blocks.
A shielded plug can be created at runtime using createDatabase(int)
or be created at startup and retrieved
by getDatabase(int)
.
The list of memory blocks IDs can be retrieve using getIDs()
.
There are two sorts of shielded plugs:
isImmutable()
) that cannot be modified.
isImmutable()
) can be modified by adding or removing blocks using
create(int, int)
or create(int, int, int)
or delete(int)
.
Each block has fixed length (getLength(int)
passing the block ID).
All access to a database is serialized by the implementation: there will be only one access (either read or write) at
a time. Each access to a block is atomic, this avoids inconsistency:
Each memory block has a flag that indicates that an update has occurred since the last read. It is possible to test
this state: isPending(int)
. This flag is set to false when reading, and to true when writing.
A task can wait for the modification of a memory block by using waitFor(int)
. This method suspends the
current task if and only if the method pending returns false on the specified memory block. A task can also wait on
several memory blocks, the task is released when one of the blocks is modified waitFor(int[])
).
Each memory block has a flag indicating if its data are available or not. It is possible to test this flag using
isDataAvailable(int)
. This flag is initially false
and is set to true
when writing
data. It can be set to false
using the method reset(int)
.
Modifier and Type | Method and Description |
---|---|
void |
create(int blockID,
int length)
Creates a block with the given ID.
|
void |
create(int blockID,
int length,
int maxTasks)
Creates a block with the given ID.
|
static ShieldedPlug |
createDatabase(int ID)
Creates a new empty database with the given ID.
|
void |
delete(int blockID)
Deletes the block with the given ID.
|
static ShieldedPlug |
getDatabase(int ID)
Returns the database defined at the given ID.
|
int |
getID()
Gets the ID of this database.
|
int[] |
getIDs()
Gets the list of IDs of the blocks available in this database.
|
int |
getLength(int blockID)
Returns the length of the block with the given ID.
|
int |
getMaxTasks(int blockID)
Gets the maximum number of tasks that can wait at the same time on the block defined with the given ID.
|
SPReader |
getReader(int blockID)
|
int |
getSize()
Gets the number of blocks of this database.
|
SPWriter |
getWriter(int blockID)
|
boolean |
isDataAvailable(int blockID)
Determines whether data in the block with the given ID are available or not.
By default no data is available in a block. |
boolean |
isImmutable()
Gets if this database is immutable or not.
|
boolean |
isPending(int blockID)
Gets if data has been written into the block since last read.
|
void |
read(int blockID,
byte[] data)
Fills the given array with data from the block with the given ID.
|
void |
read(int blockID,
byte[] data,
int destOffset)
Fills the given array with
block.length bytes from the block with the given ID. |
double |
readDouble(int blockID)
Reads eight input bytes from the block with the given ID and returns a
double value.The way the double is built from the eight bytes is platform dependent. |
float |
readFloat(int blockID)
Reads four input bytes from the block with the given ID and returns a
float value.The way the float is built from the four bytes is platform dependent. |
int |
readInt(int blockID)
Reads four input bytes from the block with the given ID and returns an
int value.The way the int is built from the four bytes is platform dependent. |
long |
readLong(int blockID)
Reads eight input bytes from the block with the given ID and returns a
long value.The way the long is built from the eight bytes is platform dependent. |
Object |
readObject(int blockID)
Invokes the
readObject method of the SPReader registered for the block with the given ID. |
void |
reset(int blockID)
Resets data of the block with the given ID.
After execution of this method, isDataAvailable(int) method would return false (unless data
were written after calling reset(int) and before calling isDataAvailable(int) ). |
void |
setReader(int blockID,
SPReader reader)
|
void |
setWriter(int blockID,
SPWriter writer)
|
void |
waitFor(int blockID)
Causes current thread to wait until another thread write data into the block with the given ID.
If data has been written in the block since last read, this method returns immediately. |
int[] |
waitFor(int[] blockIDs)
Causes current thread to wait until another thread write data into at least one block from the blocks with the
given IDs.
If data has been written in one block since last read from it, this method returns immediately. |
void |
write(int blockID,
byte[] data)
Writes block length bytes from the specified byte array to the block with the given ID.
The write(blockID, data) method has the same effect as: |
void |
write(int blockID,
byte[] data,
int srcOffset)
Writes block length bytes from the specified byte array to the block with the given ID.
Element data[destOffset] is the first byte written to the block. |
void |
writeDouble(int blockID,
double value)
Writes a
double value, which is comprised of eight bytes, to the block with the given ID.The way the double is written from the eight bytes is platform dependent. |
void |
writeFloat(int blockID,
float value)
Writes a
float value, which is comprised of four bytes, to the block with the given ID.The way the float is written from the four bytes is platform dependent. |
void |
writeInt(int blockID,
int value)
Writes an
int value, which is comprised of four bytes, to the block with the given ID.The way the int is written from the four bytes is platform dependent. |
void |
writeLong(int blockID,
long value)
Writes a
long value, which is comprised of eight bytes, to the block with the given ID.The way the long is written from the eight bytes is platform dependent. |
void |
writeObject(int blockID,
Object o)
Invokes the
writeObject method of the SPWriter registered for the block with the given ID. |
public void create(int blockID, int length)
blockID
- the ID of the block to createlength
- the length in bytes of the block to createIllegalArgumentException
- if a block is already defined with the given IDSecurityException
- if this database is immutablepublic void create(int blockID, int length, int maxTasks)
blockID
- the ID of the block to createlength
- the length in bytes of the block to createmaxTasks
- maximum number of tasks that can wait at the same time for a modification of the blockIllegalArgumentException
- if a block is already defined with the given IDSecurityException
- if this database is immutablepublic static ShieldedPlug createDatabase(int ID)
ID
- the identification number of the created databaseIllegalArgumentException
- if a database with the given ID already existsSecurityException
- if the platform cannot create dynamically databasespublic void delete(int blockID)
blockID
- the ID of the block to deleteIllegalArgumentException
- if no block is defined with the given IDSecurityException
- if this database is immutablepublic static ShieldedPlug getDatabase(int ID)
ID
- the identification number of the requested databaseIllegalArgumentException
- if no database is defined with the given IDpublic int getID()
public int[] getIDs()
public int getLength(int blockID)
blockID
- the ID of the blockIllegalArgumentException
- if no block is defined with the given IDpublic int getMaxTasks(int blockID)
blockID
- the ID of the blockIllegalArgumentException
- if no block is defined with the given ID@Nullable public SPReader getReader(int blockID)
SPReader
used to de-serialize objects from the block with the given ID.SPReader
is defined for the block, null
is returned.blockID
- the ID of the blockSPReader
set or null if noneIllegalArgumentException
- if no block is defined with the given IDpublic int getSize()
@Nullable public SPWriter getWriter(int blockID)
SPWriter
used to serialize objects into the block with the given ID.SPWriter
is defined for the block, null
is returned.blockID
- the ID of the blockSPWriter
set or null if noneIllegalArgumentException
- if no block is defined with the given IDpublic boolean isDataAvailable(int blockID)
reset(int)
is called.blockID
- the ID of the blocktrue
if data is available in the block false
otherwiseIllegalArgumentException
- if no block is defined with the given IDpublic boolean isImmutable()
true
if no block can be added or remove to this database, false
otherwisepublic boolean isPending(int blockID)
blockID
- the ID of the blocktrue
if data has been written into the block since last read, false
otherwiseIllegalArgumentException
- if no block is defined with the given IDpublic void read(int blockID, byte[] data) throws EmptyBlockException
read(blockID, data)
method has the same effect as:
read(blockID, data, 0)
blockID
- the ID of the blockdata
- the buffer into which the data is readIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if data.length
is lower than block lengthpublic void read(int blockID, byte[] data, int destOffset) throws EmptyBlockException
block.length
bytes from the block with the given ID. The first byte read
is stored into element data[destOffset]
.
If destOffset
is negative or destOffset
+ block length is greater than the length of
the array data
, then an IndexOutOfBoundsException
is thrown.
blockID
- the ID of the blockdata
- the buffer into which the data is readdestOffset
- the start offset in array data
at which the data is writtenIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if destOffset
is negative or if data.length
is lower than
destOffset
+ block lengthpublic double readDouble(int blockID) throws EmptyBlockException
double
value.double
is built from the eight bytes is platform dependent.
This method is suitable for reading bytes written by the writeDouble
method.
blockID
- the ID of the blockdouble
value readIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if block length is not height bytespublic float readFloat(int blockID) throws EmptyBlockException
float
value.float
is built from the four bytes is platform dependent.
This method is suitable for reading bytes written by the writeFloat
method.
blockID
- the ID of the blockfloat
value readIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if block length is not four bytespublic int readInt(int blockID) throws EmptyBlockException
int
value.int
is built from the four bytes is platform dependent.
This method is suitable for reading bytes written by the writeInt
method.
blockID
- the ID of the blockint
value readIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if block length is not four bytespublic long readLong(int blockID) throws EmptyBlockException
long
value.long
is built from the eight bytes is platform dependent.
This method is suitable for reading bytes written by the writeLong
method.
blockID
- the ID of the blocklong
value readIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockIndexOutOfBoundsException
- if block length is not height bytespublic Object readObject(int blockID) throws EmptyBlockException
readObject
method of the SPReader
registered for the block with the given ID.
SPReader
is responsible for the de-serialization of the object from the block.blockID
- the ID of the blockIllegalArgumentException
- if no block is defined with the given IDEmptyBlockException
- if no data is available in the blockNullPointerException
- if no SPReader
has been registered for the blockIndexOutOfBoundsException
- if block length is lower than the size needed for object de-serializationpublic void reset(int blockID)
isDataAvailable(int)
method would return false
(unless data
were written after calling reset(int)
and before calling isDataAvailable(int)
).blockID
- the ID of the blockIllegalArgumentException
- if no block is defined with the given IDpublic void setReader(int blockID, SPReader reader)
SPReader
to de-serialize objects from the block with the given ID.SPReader
is already defined for the block, it is replaced by the given SPReader
.blockID
- the ID of the blockreader
- the SPReader
IllegalArgumentException
- if no block is defined with the given IDpublic void setWriter(int blockID, SPWriter writer)
SPWriter
to serialize objects into the block with the given ID.SPWriter
is already defined for the block, it is replaced by the given SPWriter
.blockID
- the ID of the blockwriter
- the SPWriter
IllegalArgumentException
- if no block is defined with the given IDpublic void waitFor(int blockID) throws InterruptedException
blockID
- the ID of the blockInterruptedException
- if another thread has interrupted the current thread The interrupted status of the current thread is
cleared when this exception is thrownIllegalArgumentException
- if no block is defined with the given IDTooManyWaitingThreadsException
- if too many threads are waiting for new datapublic int[] waitFor(int[] blockIDs) throws InterruptedException
blockIDs
- the list of block IDsInterruptedException
- if another thread has interrupted the current thread The interrupted status of the current thread is
cleared when this exception is thrownIllegalArgumentException
- if one of the ID does not correspond to an existing blockTooManyWaitingThreadsException
- if too many threads are waiting for new datapublic void write(int blockID, byte[] data)
write(blockID, data)
method has the same effect as:
write(blockID, data, 0)
blockID
- the ID of the blockdata
- the data to writeIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if data.length
value is lower than block lengthpublic void write(int blockID, byte[] data, int srcOffset)
data[destOffset]
is the first byte written to the block.
If destOffset
is negative, or destOffset
+ block length is greater than the length of
the array data
, then an IndexOutOfBoundsException
is thrown.
blockID
- the ID of the blockdata
- the data to writesrcOffset
- the start offset in the dataIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if destOffset
is negative or if data.length
is lower than
offset
+ block lengthpublic void writeDouble(int blockID, double value)
double
value, which is comprised of eight bytes, to the block with the given ID.double
is written from the eight bytes is platform dependent.
The bytes written by this method may be read by the readDouble
method, which will then return a
double
equal to value
.
blockID
- ID of the blockvalue
- the double
value to be writtenIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if block length is not eight bytespublic void writeFloat(int blockID, float value)
float
value, which is comprised of four bytes, to the block with the given ID.float
is written from the four bytes is platform dependent.
The bytes written by this method may be read by the readFloat
method, which will then return a
float
equal to value
.
blockID
- the ID of the blockvalue
- the float
value to be writtenIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if block length is not four bytespublic void writeInt(int blockID, int value)
int
value, which is comprised of four bytes, to the block with the given ID.int
is written from the four bytes is platform dependent.
The bytes written by this method may be read by the readInt
method, which will then return an
int
equal to value
.
blockID
- the ID of the blockvalue
- the int
value to be writtenIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if block length is not four bytespublic void writeLong(int blockID, long value)
long
value, which is comprised of eight bytes, to the block with the given ID.long
is written from the eight bytes is platform dependent.
The bytes written by this method may be read by the readLong
method, which will then return a
long
equal to value
.
blockID
- the ID of the blockvalue
- the long
value to be writtenIllegalArgumentException
- if no block is defined with the given IDIndexOutOfBoundsException
- if block length is not eight bytespublic void writeObject(int blockID, Object o)
writeObject
method of the SPWriter
registered for the block with the given ID.
SPWriter
is responsible for the serialization of the object into the block.blockID
- the ID of the blocko
- the object to be writtenIllegalArgumentException
- if no block is defined with the given IDNullPointerException
- if no SPWriter
has been registered for the blockIndexOutOfBoundsException
- if block length is lower than the size needed for object serialization