Class SendTransaction
- All Implemented Interfaces:
ITransaction
Note: All "synchronization" calls are now removed for performance reasons, so threaded access to a transaction instance should be synchronized by the calling threads.
- Author:
- Christopher Mindus
- See Also:
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Maximum transaction size in bytes = 0x1FFF_FFFF.Fields inherited from interface com.iizix.ITransaction
DESCR_BOOLEAN, DESCR_BYTE, DESCR_BYTES, DESCR_CHAR, DESCR_DOUBLE, DESCR_FLOAT, DESCR_LONG, DESCR_LONG64, DESCR_NUMBER, DESCR_SHORT, DESCR_SIGNED_INT, DESCR_STRING, DESCR_TRANSACTION, DESCR_UNSIGNED_INT, DESCR_USHORT, TYPE_BOOLEAN, TYPE_BYTE, TYPE_BYTES, TYPE_CHAR, TYPE_DOUBLE, TYPE_FLOAT, TYPE_LONG, TYPE_LONG64, TYPE_NUMBER, TYPE_SHORT, TYPE_SIGNED_INT, TYPE_STRING, TYPE_TRANSACTION, TYPE_UNSIGNED_INT, TYPE_USHORT
Constructor Summary
ConstructorDescriptionCreates a new empty transaction that will be filled with streamed data using the append methods.SendTransaction
(WriteCache writeCache) Creates a new empty transaction that will be filled with streamed data using the append methods.Method Summary
Modifier and TypeMethodDescriptionvoid
append
(byte[] appendData) Appends a byte array to the outgoing transaction.void
append
(byte[] appendData, int len) Appends a byte array of a certain length to the outgoing transaction.void
append
(byte[] appendData, int offset, int len) Appends a byte array of a certain length to the outgoing transaction.void
Appends a sub-transaction to this send transaction.void
appendBoolean
(boolean b) Appends a boolean as an 8-bit value.void
appendByte
(int val) Appends an unsigned byte to the outgoing transaction.void
appendChar
(char ch) Appends a character to the outgoing transaction as a one-character wide string encoded to bytes as described below.void
appendDouble
(double val) Appends a double value to the outgoing transaction (64 bits or 8 bytes).void
appendFloat
(float val) Appends a float value to the outgoing transaction (32 bits or 4 bytes).int
appendLong
(int val) Appends a 32 bit signed value to the outgoing transaction.void
appendLong64
(long val) Appends a 64 bit signed value to the outgoing transaction.void
appendNumber
(double n) Appends a Number (JavaScript) as the shortest possible String.void
appendShort
(int val) Appends a 16 bit signed value to the outgoing transaction.void
appendSigned
(int val) Appends an signed integer with the range -0x1FFFFFFF to 0x1FFFFFFF.void
appendString
(String str) Appends a String to the outgoing transaction.void
appendString
(String str, boolean useStringCache) Appends a String to the outgoing transaction.void
appendUnsigned
(int val) Appends an unsigned integer with the range 0x00000000 to 0x3FFFFFFF.void
appendUShort
(int val) Appends a 16 bit unsigned value to the outgoing transaction.boolean
close()
Closes the send transaction.dumpWriteCache
(String indent) Dumps the write cache to a String formatted with a certain indent.protected void
finalize()
Finalized transaction.byte[]
getData()
Gets the byte data of the transaction.byte[]
getData
(int serial) Gets the byte data of the transaction.int
Gets the size of the transaction when appending or sending the transaction.boolean
Checks if this transaction has any data in it.static boolean
Returns if data types are to be included in transactions.static void
setDatatypeIncluded
(boolean on) Returns if data types are to be included in transactions.void
setLong
(int val, int pos) Sets a 32 bit signed value to the outgoing transaction at the specified position.static int
setLongData
(int val, int pos, byte[] data) Fills an array with the long data (4 bytes).
Field Details
MAX_TRANSACTION_SIZE
public static final int MAX_TRANSACTION_SIZEMaximum transaction size in bytes = 0x1FFF_FFFF.- See Also:
Constructor Details
SendTransaction
public SendTransaction()Creates a new empty transaction that will be filled with streamed data using the append methods. This transaction will then be sent on to the other part.This method does not use a write cache.
SendTransaction
Creates a new empty transaction that will be filled with streamed data using the append methods. This transaction will then be sent on to the other part.- Parameters:
writeCache
- The writeCache, or null for none.
Method Details
isDatatypeIncluded
public static boolean isDatatypeIncluded()Returns if data types are to be included in transactions. This requires an extra byte per data type indicating the type.- Returns:
- true if data types should be added, false otherwise.
setDatatypeIncluded
public static void setDatatypeIncluded(boolean on) Returns if data types are to be included in transactions. This requires an extra byte per data type indicating the type.- Parameters:
on
- true if data types should be added, false otherwise.
setLongData
public static int setLongData(int val, int pos, byte[] data) Fills an array with the long data (4 bytes).- Parameters:
val
- The 32 bit value to set in thedata
.pos
- The position (offset) in thedata
.data
- The byte array of data.- Returns:
- The new position, i.e.
pos+4
.
close
public boolean close()Closes the send transaction.- Returns:
- Previous closed state, i.e.
false
if transaction was not closed, ortrue
if transaction was already closed.
finalize
Finalized transaction. Log an error if the transaction has a write cache but has not been closed. This is a situation that is not acceptable. If so, the transaction is closed after logging.getTotalSendSize
public int getTotalSendSize()Gets the size of the transaction when appending or sending the transaction.- Returns:
- The total bytes count appended.
hasAppendedData
public boolean hasAppendedData()Checks if this transaction has any data in it.- Returns:
- true if any data has been appended, false if transaction is new without appended data.
getData
public byte[] getData()Gets the byte data of the transaction. The data is a copy of the internal data in the transaction and is of the exact size of the entire transaction package.When calling this method, the SendTransaction is automatically closed, so no further additions to it can be done, otherwise an IllegalStateException will be thrown.
- Returns:
- The entire transaction byte array.
getData
public byte[] getData(int serial) Gets the byte data of the transaction. The data is a copy of the internal data in the transaction and is of the exact size of the entire transaction package.When calling this method, the SendTransaction is automatically closed, so no further additions to it can be done, otherwise an IllegalStateException will be thrown.
- Parameters:
serial
- The serial number 0x0000 to 0xFFFF, -1 for none.- Returns:
- The byte array of the transaction with an optional serial number.
- Throws:
IllegalArgumentException
- If serial value not -1 or not in range between 0x0000 and 0xFFFF.
appendByte
public void appendByte(int val) Appends an unsigned byte to the outgoing transaction.- Parameters:
val
- The 8-bit value to append. Values outside of range are stripped to the 8 first bits, i.e. doing "and 0xFF" on it.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendChar
public void appendChar(char ch) Appends a character to the outgoing transaction as a one-character wide string encoded to bytes as described below. The character has, due to Java itself, a code point between U+0000 and U+FFFF, i.e. 16 bits. The additional so called "surrogate pair characters" (two "char" in an array) having the code points U+000000 to U+10FFFF cannot be used here.The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters, and not supported here.
Encoding: if the character ASCII (code point) value is up to 254 inclusive (i.e. smaller than 0xFF), a single byte is added, otherwise 3 character.
- Parameters:
ch
- The character to append.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendBoolean
public void appendBoolean(boolean b) Appends a boolean as an 8-bit value.- Parameters:
b
- The boolean value.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendShort
public void appendShort(int val) Appends a 16 bit signed value to the outgoing transaction.- Parameters:
val
- The value.- Throws:
IllegalArgumentException
- If value range not between -0x8000 and 0x7FFF.IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendUShort
public void appendUShort(int val) Appends a 16 bit unsigned value to the outgoing transaction.- Parameters:
val
- The value.- Throws:
IllegalArgumentException
- If value range not between 0x0000 and 0xFFFF.IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendLong
public int appendLong(int val) Appends a 32 bit signed value to the outgoing transaction.- Parameters:
val
- The value.- Returns:
- The position in which the long value was written in, in case the value must be filled in afterwards using a call to
setLong(size,pos)
used for skipping data in case of a transaction part that must be ignored. - Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
setLong
public void setLong(int val, int pos) Sets a 32 bit signed value to the outgoing transaction at the specified position.This method is used in conjunction with
getSize()
in an outgoing transaction when the long value is used for skipping a transaction on the other end (the long value is the size of data to skip, but the size is not known until all sub-transactions are appended).- Parameters:
val
- the long value.pos
- position to write the long value.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendLong64
public void appendLong64(long val) Appends a 64 bit signed value to the outgoing transaction.- Parameters:
val
- The value.- Throws:
IllegalStateException
- If the transaction is closed.
appendSigned
public void appendSigned(int val) Appends an signed integer with the range -0x1FFFFFFF to 0x1FFFFFFF. The amount of bytes appended to the transaction varies from 1 to 4 bytes if the absolute value is larger than 0x1F (2 bytes), 0x1FFF (3 bytes) or 0x1F FFFF (4 bytes). If the range is wrong,IllegalArgumentException
is thrown.- Parameters:
val
- the signed integer to append.- Throws:
IllegalArgumentException
- If value is not in range -0x1FFF_FFFF to 0x1FFF_FFFF.IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendUnsigned
public void appendUnsigned(int val) Appends an unsigned integer with the range 0x00000000 to 0x3FFFFFFF. The amount of bytes appended to the transaction varies from 1 to 4 bytes if the value is larger than 0x3F (2 bytes), 0x3FFF (3 bytes) or 0x3FFFFF (4 bytes). If the range is wrong,IllegalArgumentException
is thrown.- Parameters:
val
- The unsigned integer to append.- Throws:
IllegalStateException
- If the transaction is closed.IllegalArgumentException
- If value is not in range -0x1FFF_FFFF to 0x1FFF_FFFF.
appendFloat
public void appendFloat(float val) Appends a float value to the outgoing transaction (32 bits or 4 bytes).- Parameters:
val
- The value.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendDouble
public void appendDouble(double val) Appends a double value to the outgoing transaction (64 bits or 8 bytes).- Parameters:
val
- The value.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendNumber
public void appendNumber(double n) Appends a Number (JavaScript) as the shortest possible String.- Parameters:
n
- The number.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendString
Appends a String to the outgoing transaction. The appended string will use the String Cache (if present, and if the String meets the cache conditions).- Parameters:
str
- The string.- Throws:
IllegalArgumentException
- If the string length is longer than 0xFF_FFFF (268_435_455), or the UTF-8 encoded string is longer than 0xFF_FFFF (268_435_455 bytes).IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
appendString
Appends a String to the outgoing transaction.The appended string will use the String Cache depending on the "useStringCache" flag (and if the String Cache present, and the String meets the cache conditions).
- Parameters:
str
- The string.useStringCache
- Flag to avoid using the cache if false, true to use the write cache (if present).- Throws:
IllegalArgumentException
- If the string length is longer than 0xFF_FFFF (268_435_455), or the UTF-8 encoded string is longer than 0xFF_FFFF (268_435_455 bytes).IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
append
Appends a sub-transaction to this send transaction. The send transaction as parameter will first be closed, so no further writing to it is possible.- Parameters:
t
- Another transaction to append.- Throws:
IllegalArgumentException
- If the transaction appended does not contain data, or if the total length of the resulting transaction data exceeds 0x3FFF_FFFF.
append
public void append(byte[] appendData) Appends a byte array to the outgoing transaction.- Parameters:
appendData
- The array of bytes to append.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
append
public void append(byte[] appendData, int len) Appends a byte array of a certain length to the outgoing transaction.- Parameters:
appendData
- The array of bytes to append.len
- The length to append (can be less than the total length of the array).- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
append
public void append(byte[] appendData, int offset, int len) Appends a byte array of a certain length to the outgoing transaction.- Parameters:
appendData
- The array of bytes to append.offset
- Starting offset for append in the array.len
- The byte length to append.- Throws:
IllegalStateException
- If the transaction is closed or if the transaction has been sealed for further append of new data of it. A transaction becomes sealed if you call
orgetData()
. A call togetData(int)
will seal the current transaction and the transaction being appended.append(SendTransaction)
dumpWriteCache
Dumps the write cache to a String formatted with a certain indent.- Parameters:
indent
- The string indent for each line. The indent is preceded with '\n' for each line not being the first line.- Returns:
- A formatted string for the write cache, or
*** NO WRITE CACHE ***
when a WriteCache instance is not assigned to the transaction.