|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.lts.ipc.sharedmemory.SharedMemory
public class SharedMemory
A shared memory segment.
This class wraps the MappedByteBuffer
class to make it a little easier
to use.
#quickstart
Instances of this class must correspond to a file on the host system that is read/write
to the running process.
Quickstart
SharedMemory(String, int)
.#read(byte[], int, int, int)
.write(byte[], int, int, int)
.
MappedByteBuffer
Field Summary | |
---|---|
protected java.nio.MappedByteBuffer |
byteBuffer
|
protected java.nio.channels.FileChannel |
channel
|
protected boolean |
connected
|
protected java.io.File |
segmentFile
|
protected int |
size
|
Constructor Summary | |
---|---|
SharedMemory()
|
|
SharedMemory(java.io.File file,
int size)
|
|
SharedMemory(java.lang.String fileName,
int size)
|
Method Summary | |
---|---|
void |
connect(java.io.File file,
int size)
Connect this segment to the underlying shared memory segment. |
void |
connect(java.lang.String fileName,
int size)
|
byte |
getByte(int offset)
Return the byte at a particular location in the segment. |
java.nio.MappedByteBuffer |
getByteBuffer()
|
java.lang.String |
getLine(int offset,
byte[] buffer)
Retrieve a line of text from the segment. |
java.lang.String |
getLine(int offset,
int maxBytes)
Return a string from the specified location in the segment; the string may only take the specified number of bytes. |
java.io.File |
getSegmentFile()
|
int |
getSize()
|
boolean |
isConnected()
|
boolean |
isFileCreator()
|
void |
lock()
Reserve the shared memory segment. |
void |
put(int offset,
byte[] data)
Put some data at a location. |
void |
putLine(int offset,
java.lang.String s)
|
void |
putString(int offset,
java.lang.String s)
|
int |
read(byte[] buf)
Read bytes from the start of the segment. |
int |
read(byte[] buf,
int bufferOffset)
Read bytes from the segment into a buffer, storing them in a location offset from the start of the buffer. |
int |
read(int segmentOffset,
byte[] buf)
Read in data from the segment, starting at a particular location in the segment. |
int |
read(int soffset,
byte[] buf,
int boffset,
int length)
Read bytes up to a specified number of bytes into the provided buffer. |
void |
setByteBuffer(java.nio.MappedByteBuffer byteBuffer)
|
void |
setFileCreator(boolean fileCreator)
|
void |
unlock()
|
void |
write(byte[] buf)
|
void |
write(byte[] buf,
int segmentOffset)
|
void |
write(byte[] buf,
int buffOffset,
int bufLength,
int segmentOffset)
Write an array of bytes into a specified location in the segment, starting with a particular byte in the buffer and continuing for a specified number of bytes. |
void |
write(byte value,
int offset)
|
void |
write(int value,
int offset)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected java.io.File segmentFile
protected java.nio.MappedByteBuffer byteBuffer
protected int size
protected boolean connected
protected java.nio.channels.FileChannel channel
Constructor Detail |
---|
public SharedMemory()
public SharedMemory(java.io.File file, int size) throws IPCException
IPCException
public SharedMemory(java.lang.String fileName, int size) throws IPCException
IPCException
Method Detail |
---|
public boolean isFileCreator()
public void setFileCreator(boolean fileCreator)
public java.nio.MappedByteBuffer getByteBuffer()
public void setByteBuffer(java.nio.MappedByteBuffer byteBuffer)
public boolean isConnected()
public java.io.File getSegmentFile()
public int getSize()
public void connect(java.io.File file, int size) throws IPCException
name
- The name of the file for the segment.size
- The size of the segment.
IPCException
public void connect(java.lang.String fileName, int size) throws IPCException
IPCException
public void write(byte[] buf, int buffOffset, int bufLength, int segmentOffset) throws IPCException
buf
- The data to write.buffOffset
- The location, relative to the start of the buffer, where the data should be
obtained.bufLength
- The number of bytes to write.segmentOffset
- The location in the buffer where the data should be written.
IPCException
- The method will throw this exception if the segment is not actually
connected to the underlying shared memory block.public void write(byte value, int offset)
public void write(byte[] buf) throws IPCException
IPCException
public void write(int value, int offset)
public void write(byte[] buf, int segmentOffset) throws IPCException
IPCException
public int read(int soffset, byte[] buf, int boffset, int length) throws IPCException
Read bytes up to a specified number of bytes into the provided buffer.
This method returns the number of bytes that were actually read. If fewer bytes are left in the segment than requested, then the method returns the number of bytes read.
buf
- The buffer where the bytes read should be stored.boffset
- The offset into buf where the bytes should be placed.length
- The number of bytes to try to read.soffset
- The offset within the segment where the read should start.
IPCException
- This method throws this exception with the error set to
IPCException.Errors.NotConnected
if the instance has not been connected
to a segment yet.public int read(byte[] buf, int bufferOffset) throws IPCException
Read bytes from the segment into a buffer, storing them in a location offset from the start of the buffer.
This method is equivalent to calling:
read(0, buf, bufferOffset, buf.length);
buf
- The buffer where the bytes read should be placed.bufferOffset
- The location in the buffer where the bytes read should start.
IPCException
- read(int, byte[], int, int)
.read(int, byte[], int, int)
public int read(byte[] buf) throws IPCException
This method is equivalent to calling
read(0, buf, 0, buf.length);
See that method for details.
buf
- The buffer where bytes read should be placed.
IPCException
- see read(int, byte[], int, int)
.#read(int, byte[], int, int)}
public int read(int segmentOffset, byte[] buf) throws IPCException
This method is equivalent to calling
read(segmentOffset, buf, 0, buf.length);
See that method for details.
segmentOffset
- The byte index into the segment where reading should start.buf
- Where the bytes read should be placed.
IPCException
- see read(int, byte[], int, int)
read(int, byte[], int, int)
public byte getByte(int offset) throws IPCException
offset
- The location of the byte in the segment.
java.lang.IndexOutOfBoundsException
- If offset is negative or larger than the size of the segment minus 1.
IPCException
public void lock() throws java.io.IOException
It is not clear from the JRE documentation whether or not locking the segment will stop modifications to the segment or not. for details.
java.io.IOException
public void unlock() throws java.io.IOException
java.io.IOException
public void put(int offset, byte[] data) throws IPCException
The entire contents of the buffer are written to a position in the segment defined by the offset parameter. If the amount of data exceeds the size of the segment, the method throws an exception.
offset
- The location in the segment, in bytes, where the data will be written.data
- The data to write. The contents of the array will be written.
IPCException
public java.lang.String getLine(int offset, byte[] buffer) throws java.io.IOException, IPCException
Retrieve a line of text from the segment.
This method goes to the specified location in the segment and tries to read in characters. It continues until it hits the end of the segment, or it encounters the end of the string. The end of a string is denoted by any of the following sequences of characters:
The method uses the underlying character set as defined by
Charset.defaultCharset()
The supplied buffer is used to determine the maximum length of the string in that, if no line termination has been encountered before a number of bytes equal to the size of the buffer are read, then the characters accumulated so far are returned.
Any line termination character(s) are not included in the string returned.
offset
- The location in the segment to start reading.buffer
- A buffer to contain the bytes from the shared memory segment.
java.io.IOException
- If the method tries to read past the end of the segment, either because it
started outside the segment or because the string did not have any line
termination.
IPCException
- This exception is thrown if the object is not currently connected to a
shared memory segment.public java.lang.String getLine(int offset, int maxBytes) throws java.io.IOException, IPCException
This method is equivalent to calling
byte[] buf = new byte[maxBytes];
getLine(offset, buf);
See that method for details.
If the underlying string does not end after maxBytes, the method returns the substring that takes up that number of bytes.
offset
- The location within the segment where the string starts.maxBytes
- The maximum number of bytes for the string.
java.io.IOException
- See getLine(int, byte[])
.
IPCException
- See getLine(int, byte[])
.getLine(int, byte[])
public void putString(int offset, java.lang.String s)
public void putLine(int offset, java.lang.String s)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |