public class SFSArray extends java.lang.Object implements ISFSArray
These two classes provide a fine-grained control over each data item sent over the network and provide high speed serialization using the default SFS2X binary protocol.
Let's consider this simple example: we need to send the data relative to a combat vehicle in a multiplayer game. In order reduce the amount of bytes sent to a minimum we will send the data as an array avoiding the overhead of key names for each item.
ISFSArray sfsa = new SFSArray(); sfsa.addByte(10); // the vehicle id sfsa.addShort(5000); // the vehicle current health sfsa.addIntArray(Arrays.asList(120,150)); // the x,y position on the terrain as int[] sfsa.addUtfString("Hurricane"); // the vehicle nameIn the above code we can use a single Byte (signed 8-bit) to send any small integer value, a Short (signed 16-bit) for larger values and integers for any number that should be represented as regular 32-bit value. In this example we imagined to have a wide RTS environment and we used an int[] to transmit the x,y position of the vehicle.
The following is a list of type supported by the SFSArray class:
NOTE: SFSArray is not thread safe.
Limitations: An SFSArray can contain a maximum of 32768 elements, also the UTF Strings length is expressed using a signed short integer which means that a string can be long up to 32768 characters.
SFSObject
Constructor and Description |
---|
SFSArray() |
Modifier and Type | Method and Description |
---|---|
void |
add(SFSDataWrapper wrappedObject) |
void |
addBool(boolean value)
Add a boolean
|
void |
addBoolArray(java.util.Collection<java.lang.Boolean> value)
Add an collection of boolean
|
void |
addByte(byte value)
Add a byte (signed 8-bit)
|
void |
addByteArray(byte[] value)
Add an array of bytes
NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
|
void |
addClass(java.lang.Object o)
Add a Class instance.
|
void |
addDouble(double value)
Add a byte (signed decimal 64-bit)
|
void |
addDoubleArray(java.util.Collection<java.lang.Double> value)
Add a collection of double
|
void |
addFloat(float value)
Add a byte (signed decimal 32-bit)
|
void |
addFloatArray(java.util.Collection<java.lang.Float> value)
Add a collection of float
|
void |
addInt(int value)
Add a byte (signed 32-bit)
|
void |
addIntArray(java.util.Collection<java.lang.Integer> value)
Add a collection of int
|
void |
addLong(long value)
Add a byte (signed 64-bit)
|
void |
addLongArray(java.util.Collection<java.lang.Long> value)
Add a collection of long
|
void |
addNull()
Add a null field to the Object.
|
void |
addSFSArray(ISFSArray value)
Add a nested ISFSArray
|
void |
addSFSObject(ISFSObject value)
Add a nested ISFSObject
|
void |
addShort(short value)
Add a byte (signed 16-bit)
|
void |
addShortArray(java.util.Collection<java.lang.Short> value)
Add a collection of short
|
void |
addText(java.lang.String value)
Add a long String encoded in UTF-8 with max length of 2 GBytes
|
void |
addUtfString(java.lang.String value)
Add a String encoded in UTF-8 with max length of 32 KBytes
|
void |
addUtfStringArray(java.util.Collection<java.lang.String> value)
Add a collection of string
|
boolean |
contains(java.lang.Object obj)
Checks if a specific element is contained in the array
|
SFSDataWrapper |
get(int index) |
java.lang.Boolean |
getBool(int index)
Get the element at the specified index as Boolean.
|
java.util.Collection<java.lang.Boolean> |
getBoolArray(int index)
Get the element at the specified index as Collection of Boolean.
|
java.lang.Byte |
getByte(int index)
Get the element at the specified index as Byte.
|
byte[] |
getByteArray(int index)
Get the element at the specified index as byte array.
|
java.lang.Object |
getClass(int index)
Get the element at the specified index as Class instance.
|
java.lang.Double |
getDouble(int index)
Get the element at the specified index as Double.
|
java.util.Collection<java.lang.Double> |
getDoubleArray(int index)
Get the element at the specified index as Collection of Double.
|
java.lang.String |
getDump()
Get a detailed dump of the SFSArray structure
|
java.lang.String |
getDump(boolean noFormat)
Get a detailed dump of the SFSArray structure
|
java.lang.Object |
getElementAt(int index) |
java.lang.Float |
getFloat(int index)
Get the element at the specified index as Float.
|
java.util.Collection<java.lang.Float> |
getFloatArray(int index)
Get the element at the specified index as Collection of Float.
|
java.lang.String |
getHexDump()
Get a pretty-printed hex-dump of the array
|
java.lang.Integer |
getInt(int index)
Get the element at the specified index as Integer.
|
java.util.Collection<java.lang.Integer> |
getIntArray(int index)
Get the element at the specified index as Collection of Int.
|
java.lang.Long |
getLong(int index)
Get the element at the specified index as Long.
|
java.util.Collection<java.lang.Long> |
getLongArray(int index)
Get the element at the specified index as Collection of Long.
|
ISFSArray |
getSFSArray(int index)
Get the element at the specified index as ISFSArray.
|
ISFSObject |
getSFSObject(int index)
Get the element at the specified index as ISFSObject.
|
java.lang.Short |
getShort(int index)
Get the element at the specified index as Short.
|
java.util.Collection<java.lang.Short> |
getShortArray(int index)
Get the element at the specified index as Collection of Short.
|
java.lang.String |
getText(int index)
Get the element at the specified index as long String, with a max length of 2 GBytes.
|
java.lang.Integer |
getUnsignedByte(int index)
Get the element at the specified index as an unsigned Integer (bytes are always signed, -127 < b < 127)
It can be null if no element exists for the specified index |
java.util.Collection<java.lang.Integer> |
getUnsignedByteArray(int index)
Get the element at the specified index as a Collection of unsigned integers.
|
java.lang.String |
getUtfString(int index)
Get the element at the specified index as String, with a max length of 32 KBytes.
|
java.util.Collection<java.lang.String> |
getUtfStringArray(int index)
Get the element at the specified index as Collection of String.
|
boolean |
isNull(int index)
Checks if a specific element is null.
|
java.util.Iterator<SFSDataWrapper> |
iterator() |
static SFSArray |
newFromBinaryData(byte[] bytes)
Rebuild an SFSArray form its binary form
|
static SFSArray |
newFromJsonData(java.lang.String jsonStr)
Creates an SFSObject from a JSON literal.
|
static SFSArray |
newFromResultSet(java.sql.ResultSet rset) |
static SFSArray |
newInstance()
Static constructor, similar to new SFSArray();
|
void |
removeElementAt(int index)
Remove an element at a specific index
|
int |
size()
Return the number of elements contained in the array
|
byte[] |
toBinary()
Return the binary representation of the SFSArray
|
java.lang.String |
toJson()
Return the JSON representation of the SFSArray
|
java.lang.String |
toString() |
public static SFSArray newFromBinaryData(byte[] bytes)
bytes
- the binary datajava.lang.IllegalStateException
- if there's any problem with decoding the binary datapublic static SFSArray newFromResultSet(java.sql.ResultSet rset) throws java.sql.SQLException
java.sql.SQLException
public static SFSArray newFromJsonData(java.lang.String jsonStr)
JSON | SFSObject
Bool
| Bool
| Number
| Integer, Long, Double (autodetect)
| String
| UTF String
| Array
| SFSArray
| Object
| SFSObject
| |
jsonStr
- the JSON literalpublic static SFSArray newInstance()
public java.lang.String getDump()
ISFSArray
public java.lang.String getDump(boolean noFormat)
ISFSArray
public java.lang.String getHexDump()
ISFSArray
getHexDump
in interface ISFSArray
public byte[] toBinary()
ISFSArray
public java.lang.String toJson()
ISFSArray
public boolean isNull(int index)
ISFSArray
public SFSDataWrapper get(int index)
public java.lang.Boolean getBool(int index)
ISFSArray
public java.lang.Byte getByte(int index)
ISFSArray
public java.lang.Integer getUnsignedByte(int index)
ISFSArray
getUnsignedByte
in interface ISFSArray
public java.lang.Short getShort(int index)
ISFSArray
public java.lang.Integer getInt(int index)
ISFSArray
public java.lang.Long getLong(int index)
ISFSArray
public java.lang.Float getFloat(int index)
ISFSArray
public java.lang.Double getDouble(int index)
ISFSArray
public java.lang.String getUtfString(int index)
ISFSArray
getUtfString
in interface ISFSArray
public java.lang.String getText(int index)
ISFSArray
public java.util.Collection<java.lang.Boolean> getBoolArray(int index)
ISFSArray
getBoolArray
in interface ISFSArray
public byte[] getByteArray(int index)
ISFSArray
NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
getByteArray
in interface ISFSArray
public java.util.Collection<java.lang.Integer> getUnsignedByteArray(int index)
ISFSArray
getUnsignedByteArray
in interface ISFSArray
public java.util.Collection<java.lang.Short> getShortArray(int index)
ISFSArray
getShortArray
in interface ISFSArray
public java.util.Collection<java.lang.Integer> getIntArray(int index)
ISFSArray
getIntArray
in interface ISFSArray
public java.util.Collection<java.lang.Long> getLongArray(int index)
ISFSArray
getLongArray
in interface ISFSArray
public java.util.Collection<java.lang.Float> getFloatArray(int index)
ISFSArray
getFloatArray
in interface ISFSArray
public java.util.Collection<java.lang.Double> getDoubleArray(int index)
ISFSArray
getDoubleArray
in interface ISFSArray
public java.util.Collection<java.lang.String> getUtfStringArray(int index)
ISFSArray
getUtfStringArray
in interface ISFSArray
public ISFSArray getSFSArray(int index)
ISFSArray
getSFSArray
in interface ISFSArray
public ISFSObject getSFSObject(int index)
ISFSArray
getSFSObject
in interface ISFSArray
ISFSObject
public java.lang.Object getClass(int index)
ISFSArray
NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
getClass
in interface ISFSArray
SFSObject.newFromObject(Object)
public void addBool(boolean value)
ISFSArray
public void addBoolArray(java.util.Collection<java.lang.Boolean> value)
ISFSArray
addBoolArray
in interface ISFSArray
public void addByte(byte value)
ISFSArray
public void addByteArray(byte[] value)
ISFSArray
NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
addByteArray
in interface ISFSArray
public void addDouble(double value)
ISFSArray
public void addDoubleArray(java.util.Collection<java.lang.Double> value)
ISFSArray
addDoubleArray
in interface ISFSArray
public void addFloat(float value)
ISFSArray
public void addFloatArray(java.util.Collection<java.lang.Float> value)
ISFSArray
addFloatArray
in interface ISFSArray
public void addInt(int value)
ISFSArray
public void addIntArray(java.util.Collection<java.lang.Integer> value)
ISFSArray
addIntArray
in interface ISFSArray
public void addLong(long value)
ISFSArray
public void addLongArray(java.util.Collection<java.lang.Long> value)
ISFSArray
addLongArray
in interface ISFSArray
public void addNull()
ISFSArray
This method will effectively add the key and a byte id to describe the Null value, thus "bloating" the message
public void addSFSArray(ISFSArray value)
ISFSArray
addSFSArray
in interface ISFSArray
public void addSFSObject(ISFSObject value)
ISFSArray
addSFSObject
in interface ISFSArray
public void addShort(short value)
ISFSArray
public void addShortArray(java.util.Collection<java.lang.Short> value)
ISFSArray
addShortArray
in interface ISFSArray
public void addUtfString(java.lang.String value)
ISFSArray
addUtfString
in interface ISFSArray
public void addText(java.lang.String value)
ISFSArray
public void addUtfStringArray(java.util.Collection<java.lang.String> value)
ISFSArray
addUtfStringArray
in interface ISFSArray
public void addClass(java.lang.Object o)
ISFSArray
SerializableSFSType
interface.
NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
addClass
in interface ISFSArray
o
- the instanceSFSObject.newFromObject(Object)
public void add(SFSDataWrapper wrappedObject)
public boolean contains(java.lang.Object obj)
ISFSArray
public java.lang.Object getElementAt(int index)
getElementAt
in interface ISFSArray
public java.util.Iterator<SFSDataWrapper> iterator()
public void removeElementAt(int index)
ISFSArray
removeElementAt
in interface ISFSArray
index
- the index of the element to removepublic int size()
ISFSArray
public java.lang.String toString()
toString
in class java.lang.Object