Class SFSArray
- java.lang.Object
-
- com.smartfoxserver.v2.entities.data.SFSArray
-
- All Implemented Interfaces:
ISFSArray,java.io.Serializable
- Direct Known Subclasses:
SFSArrayLite
public class SFSArray extends java.lang.Object implements ISFSArray, java.io.Serializable
SFSArray and SFSObject represent a platform-neutral, high-level objects that abstract the data transport between client and server. They are used to respectively represent data in form of a Map/Dictionary and List/Array. They can be nested and they can transport many different data types (from bytes to integers, doubles, strings and a lot more)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:
- null
- boolean
- byte
- short
- int
- long
- float
- double
- utf-string
- boolean array
- byte array
- short array
- int array
- long array
- float array
- double array
- utf-string array
- SFSObject
- SFSArray
SFSArrays and SFSObjects can be nested to create complex data structures.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.
- See Also:
SFSObject, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SFSArray()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(SFSDataWrapper wrappedObject)voidaddBool(boolean value)Add a booleanvoidaddBoolArray(java.util.Collection<java.lang.Boolean> value)Add an collection of booleanvoidaddByte(byte value)Add a byte (signed 8-bit)voidaddByteArray(byte[] value)Add an array of bytesvoidaddClass(java.lang.Object o)Add a Class instance.voidaddDouble(double value)Add a byte (signed decimal 64-bit)voidaddDoubleArray(java.util.Collection<java.lang.Double> value)Add a collection of doublevoidaddFloat(float value)Add a byte (signed decimal 32-bit)voidaddFloatArray(java.util.Collection<java.lang.Float> value)Add a collection of floatvoidaddInt(int value)Add a byte (signed 32-bit)voidaddIntArray(java.util.Collection<java.lang.Integer> value)Add a collection of intvoidaddLong(long value)Add a byte (signed 64-bit)voidaddLongArray(java.util.Collection<java.lang.Long> value)Add a collection of longvoidaddNull()Add a null field to the Object.voidaddSFSArray(ISFSArray value)Add a nested ISFSArrayvoidaddSFSObject(ISFSObject value)Add a nested ISFSObjectvoidaddShort(short value)Add a byte (signed 16-bit)voidaddShortArray(java.util.Collection<java.lang.Short> value)Add a collection of shortvoidaddText(java.lang.String value)Add a long String encoded in UTF-8 with max length of 2 GBytesvoidaddUtfString(java.lang.String value)Add a String encoded in UTF-8 with max length of 32 KBytesvoidaddUtfStringArray(java.util.Collection<java.lang.String> value)Add a collection of stringbooleancontains(java.lang.Object obj)Checks if a specific element is contained in the arraySFSDataWrapperget(int index)java.lang.BooleangetBool(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.BytegetByte(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.ObjectgetClass(int index)Get the element at the specified index as Class instance.java.lang.DoublegetDouble(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.StringgetDump()Get a detailed dump of the SFSArray structurejava.lang.StringgetDump(boolean noFormat)Get a detailed dump of the SFSArray structurejava.lang.ObjectgetElementAt(int index)java.lang.FloatgetFloat(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.StringgetHexDump()Get a pretty-printed hex-dump of the arrayjava.lang.IntegergetInt(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.LonggetLong(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.ISFSArraygetSFSArray(int index)Get the element at the specified index as ISFSArray.ISFSObjectgetSFSObject(int index)Get the element at the specified index as ISFSObject.java.lang.ShortgetShort(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.StringgetText(int index)Get the element at the specified index as long String, with a max length of 2 GBytes.java.lang.IntegergetUnsignedByte(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 indexjava.util.Collection<java.lang.Integer>getUnsignedByteArray(int index)Get the element at the specified index as a Collection of unsigned integers.java.lang.StringgetUtfString(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.booleanisNull(int index)Checks if a specific element is null.java.util.Iterator<SFSDataWrapper>iterator()static SFSArraynewFromBinaryData(byte[] bytes)Rebuild an SFSArray form its binary formstatic SFSArraynewFromJsonData(java.lang.String jsonStr)Creates an SFSObject from a JSON literal.static SFSArraynewFromResultSet(java.sql.ResultSet rset)static SFSArraynewInstance()Static constructor, similar to new SFSArray();voidremoveElementAt(int index)Remove an element at a specific indexintsize()Return the number of elements contained in the arraybyte[]toBinary()Return the binary representation of the SFSArrayjava.lang.StringtoJson()Return the JSON representation of the SFSArrayjava.lang.StringtoString()
-
-
-
Method Detail
-
newFromBinaryData
public static SFSArray newFromBinaryData(byte[] bytes)
Rebuild an SFSArray form its binary form- Parameters:
bytes- the binary data- Returns:
- the original SFSArray
- Throws:
java.lang.IllegalStateException- if there's any problem with decoding the binary data
-
newFromResultSet
public static SFSArray newFromResultSet(java.sql.ResultSet rset) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
newFromJsonData
public static SFSArray newFromJsonData(java.lang.String jsonStr)
Creates an SFSObject from a JSON literal. Type conversion is done as follows:JSON SFSObject Bool Bool Number Integer, Long, Double (autodetect) String UTF String Array SFSArray Object SFSObject - Parameters:
jsonStr- the JSON literal- Returns:
- the SFSObject
-
newInstance
public static SFSArray newInstance()
Static constructor, similar to new SFSArray();- Returns:
- a new SFSArray
-
getDump
public java.lang.String getDump()
Description copied from interface:ISFSArrayGet a detailed dump of the SFSArray structure
-
getDump
public java.lang.String getDump(boolean noFormat)
Description copied from interface:ISFSArrayGet a detailed dump of the SFSArray structure
-
getHexDump
public java.lang.String getHexDump()
Description copied from interface:ISFSArrayGet a pretty-printed hex-dump of the array- Specified by:
getHexDumpin interfaceISFSArray- Returns:
- a pretty-printed hex-dump of the array
-
toBinary
public byte[] toBinary()
Description copied from interface:ISFSArrayReturn the binary representation of the SFSArray
-
toJson
public java.lang.String toJson()
Description copied from interface:ISFSArrayReturn the JSON representation of the SFSArray
-
isNull
public boolean isNull(int index)
Description copied from interface:ISFSArrayChecks if a specific element is null.
-
get
public SFSDataWrapper get(int index)
-
getBool
public java.lang.Boolean getBool(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Boolean. It can be null if no element exists for the specified index
-
getByte
public java.lang.Byte getByte(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Byte. It can be null if no element exists for the specified index
-
getUnsignedByte
public java.lang.Integer getUnsignedByte(int index)
Description copied from interface:ISFSArrayGet 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- Specified by:
getUnsignedBytein interfaceISFSArray- Returns:
- the element, or null
-
getShort
public java.lang.Short getShort(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Short. It can be null if no element exists for the specified index
-
getInt
public java.lang.Integer getInt(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Integer. It can be null if no element exists for the specified index
-
getLong
public java.lang.Long getLong(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Long. It can be null if no element exists for the specified index
-
getFloat
public java.lang.Float getFloat(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Float. It can be null if no element exists for the specified index
-
getDouble
public java.lang.Double getDouble(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Double. It can be null if no element exists for the specified index
-
getUtfString
public java.lang.String getUtfString(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as String, with a max length of 32 KBytes. It can be null if no element exists for the specified index- Specified by:
getUtfStringin interfaceISFSArray- Returns:
- the element, or null
-
getText
public java.lang.String getText(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as long String, with a max length of 2 GBytes. It can be null if no element exists for the specified index
-
getBoolArray
public java.util.Collection<java.lang.Boolean> getBoolArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Boolean. It can be null if no element exists for the specified index- Specified by:
getBoolArrayin interfaceISFSArray- Returns:
- the element, or null
-
getByteArray
public byte[] getByteArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as byte array. It can be null if no element exists for the specified indexNOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
- Specified by:
getByteArrayin interfaceISFSArray- Returns:
- the element, or null
-
getUnsignedByteArray
public java.util.Collection<java.lang.Integer> getUnsignedByteArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as a Collection of unsigned integers. It can be null if no element exists for the specified index- Specified by:
getUnsignedByteArrayin interfaceISFSArray- Returns:
- the element, or null
-
getShortArray
public java.util.Collection<java.lang.Short> getShortArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Short. It can be null if no element exists for the specified index- Specified by:
getShortArrayin interfaceISFSArray- Returns:
- the element, or null
-
getIntArray
public java.util.Collection<java.lang.Integer> getIntArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Int. It can be null if no element exists for the specified index- Specified by:
getIntArrayin interfaceISFSArray- Returns:
- the element, or null
-
getLongArray
public java.util.Collection<java.lang.Long> getLongArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Long. It can be null if no element exists for the specified index- Specified by:
getLongArrayin interfaceISFSArray- Returns:
- the element, or null
-
getFloatArray
public java.util.Collection<java.lang.Float> getFloatArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Float. It can be null if no element exists for the specified index- Specified by:
getFloatArrayin interfaceISFSArray- Returns:
- the element, or null
-
getDoubleArray
public java.util.Collection<java.lang.Double> getDoubleArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of Double. It can be null if no element exists for the specified index- Specified by:
getDoubleArrayin interfaceISFSArray- Returns:
- the element, or null
-
getUtfStringArray
public java.util.Collection<java.lang.String> getUtfStringArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Collection of String. It can be null if no element exists for the specified index- Specified by:
getUtfStringArrayin interfaceISFSArray- Returns:
- the element, or null
-
getSFSArray
public ISFSArray getSFSArray(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as ISFSArray. It can be null if no element exists for the specified index- Specified by:
getSFSArrayin interfaceISFSArray- Returns:
- the element, or null
-
getSFSObject
public ISFSObject getSFSObject(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as ISFSObject. It can be null if no element exists for the specified index- Specified by:
getSFSObjectin interfaceISFSArray- Returns:
- the element, or null
- See Also:
ISFSObject
-
getClass
public java.lang.Object getClass(int index)
Description copied from interface:ISFSArrayGet the element at the specified index as Class instance. The class definition is contained in the instance itself and it will be rebuilt using reflection. It can be null if no element exists for the specified indexNOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
- Specified by:
getClassin interfaceISFSArray- Returns:
- the element, or null
- See Also:
SFSObject.newFromObject(Object)
-
addBool
public void addBool(boolean value)
Description copied from interface:ISFSArrayAdd a boolean
-
addBoolArray
public void addBoolArray(java.util.Collection<java.lang.Boolean> value)
Description copied from interface:ISFSArrayAdd an collection of boolean- Specified by:
addBoolArrayin interfaceISFSArray
-
addByte
public void addByte(byte value)
Description copied from interface:ISFSArrayAdd a byte (signed 8-bit)
-
addByteArray
public void addByteArray(byte[] value)
Description copied from interface:ISFSArrayAdd an array of bytesNOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
- Specified by:
addByteArrayin interfaceISFSArray
-
addDouble
public void addDouble(double value)
Description copied from interface:ISFSArrayAdd a byte (signed decimal 64-bit)
-
addDoubleArray
public void addDoubleArray(java.util.Collection<java.lang.Double> value)
Description copied from interface:ISFSArrayAdd a collection of double- Specified by:
addDoubleArrayin interfaceISFSArray
-
addFloat
public void addFloat(float value)
Description copied from interface:ISFSArrayAdd a byte (signed decimal 32-bit)
-
addFloatArray
public void addFloatArray(java.util.Collection<java.lang.Float> value)
Description copied from interface:ISFSArrayAdd a collection of float- Specified by:
addFloatArrayin interfaceISFSArray
-
addInt
public void addInt(int value)
Description copied from interface:ISFSArrayAdd a byte (signed 32-bit)
-
addIntArray
public void addIntArray(java.util.Collection<java.lang.Integer> value)
Description copied from interface:ISFSArrayAdd a collection of int- Specified by:
addIntArrayin interfaceISFSArray
-
addLong
public void addLong(long value)
Description copied from interface:ISFSArrayAdd a byte (signed 64-bit)
-
addLongArray
public void addLongArray(java.util.Collection<java.lang.Long> value)
Description copied from interface:ISFSArrayAdd a collection of long- Specified by:
addLongArrayin interfaceISFSArray
-
addNull
public void addNull()
Description copied from interface:ISFSArrayAdd a null field to the Object. Normally we recommend that null values are simply not sent. On the other end of the application you can simply check if a specific key exists or not, to detect a null.This method will effectively add the key and a byte id to describe the Null value, thus "bloating" the message
-
addSFSArray
public void addSFSArray(ISFSArray value)
Description copied from interface:ISFSArrayAdd a nested ISFSArray- Specified by:
addSFSArrayin interfaceISFSArray
-
addSFSObject
public void addSFSObject(ISFSObject value)
Description copied from interface:ISFSArrayAdd a nested ISFSObject- Specified by:
addSFSObjectin interfaceISFSArray
-
addShort
public void addShort(short value)
Description copied from interface:ISFSArrayAdd a byte (signed 16-bit)
-
addShortArray
public void addShortArray(java.util.Collection<java.lang.Short> value)
Description copied from interface:ISFSArrayAdd a collection of short- Specified by:
addShortArrayin interfaceISFSArray
-
addUtfString
public void addUtfString(java.lang.String value)
Description copied from interface:ISFSArrayAdd a String encoded in UTF-8 with max length of 32 KBytes- Specified by:
addUtfStringin interfaceISFSArray
-
addText
public void addText(java.lang.String value)
Description copied from interface:ISFSArrayAdd a long String encoded in UTF-8 with max length of 2 GBytes
-
addUtfStringArray
public void addUtfStringArray(java.util.Collection<java.lang.String> value)
Description copied from interface:ISFSArrayAdd a collection of string- Specified by:
addUtfStringArrayin interfaceISFSArray
-
addClass
public void addClass(java.lang.Object o)
Description copied from interface:ISFSArrayAdd a Class instance. The instance must implement theSerializableSFSTypeinterface.NOTE: This is not supported for HTML5 / Websocket clients, which includes Unity WebGL exports.
- Specified by:
addClassin interfaceISFSArray- Parameters:
o- the instance- See Also:
SFSObject.newFromObject(Object)
-
add
public void add(SFSDataWrapper wrappedObject)
-
contains
public boolean contains(java.lang.Object obj)
Description copied from interface:ISFSArrayChecks if a specific element is contained in the array
-
getElementAt
public java.lang.Object getElementAt(int index)
- Specified by:
getElementAtin interfaceISFSArray
-
iterator
public java.util.Iterator<SFSDataWrapper> iterator()
-
removeElementAt
public void removeElementAt(int index)
Description copied from interface:ISFSArrayRemove an element at a specific index- Specified by:
removeElementAtin interfaceISFSArray- Parameters:
index- the index of the element to remove
-
size
public int size()
Description copied from interface:ISFSArrayReturn the number of elements contained in the array
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-