public final class TLVElement extends Object
Representation of the Type-Length-Value (TLV) element. The TLV scheme is used to encode both, the KSI data structures and also protocol data units.
For space efficiency, two TLV encodings are used:
Modifier and Type | Field and Description |
---|---|
static int |
MAX_TLV16_CONTENT_LENGTH |
Constructor and Description |
---|
TLVElement(boolean inputTlv16,
boolean nonCritical,
boolean forwarded,
int type) |
TLVElement(boolean nonCritical,
boolean forwarded,
int type) |
Modifier and Type | Method and Description |
---|---|
void |
addChildElement(TLVElement element) |
void |
addFirstChildElement(TLVElement element) |
static TLVElement |
create(byte[] bytes)
Creates TLVElement form byte array.
|
static TLVElement |
create(int type,
byte[] value)
Creates TLV element with byte array content.
|
static TLVElement |
create(int type,
DataHash value)
Creates TLV element with
DataHash content. |
static TLVElement |
create(int type,
Date value)
Creates TLV element with
Date content. |
static TLVElement |
create(int type,
HashAlgorithm value)
Creates TLV element with
HashAlgorithm content. |
static TLVElement |
create(int type,
long value)
Creates TLV element with
Long content. |
static TLVElement |
create(int type,
String value)
Creates TLV element with
String content. |
byte[] |
encodeHeader()
Encodes TLV header.
|
boolean |
equals(Object o) |
List<TLVElement> |
getChildElements() |
List<TLVElement> |
getChildElements(int... tags) |
List<TLVElement> |
getChildElements(int tag)
Returns all the tags with the specified tag.
|
byte[] |
getContent()
Returns the TLV content.
|
int |
getContentLength() |
DataHash |
getDecodedDataHash()
Converts TLV element content data to
DataHash object. |
Date |
getDecodedDate()
Gets the Date object from TLV element.
|
HashAlgorithm |
getDecodedHashAlgorithm()
Gets HashAlgorithm form TLV element.
|
Long |
getDecodedLong()
This method is used to convert TLV element content data to actual java
Long object. |
String |
getDecodedString()
Converts the TLV element content data to UTF-8 string.
|
byte[] |
getEncoded() |
TLVElement |
getFirstChildElement() |
TLVElement |
getFirstChildElement(int tag)
Returns the first child element with specified tag.
|
int |
getHeaderLength() |
TLVElement |
getLastChildElement() |
int |
getType() |
int |
hashCode() |
boolean |
isForwarded() |
boolean |
isInputTlv16() |
boolean |
isNonCritical() |
boolean |
isOutputTlv16() |
void |
remove(TLVElement elementToRemoved) |
void |
replace(TLVElement childToBeReplaced,
TLVElement newChild)
Replaces first element with given one.
|
void |
setContent(byte[] content)
Sets the value to TLV element content.
|
void |
setDataHashContent(DataHash dataHash) |
void |
setDateContent(Date date) |
void |
setHashAlgorithmContent(HashAlgorithm hashAlgorithm) |
void |
setLongContent(long value) |
void |
setStringContent(String s)
Encodes the instance of
String . |
void |
setType(int type) |
String |
toString() |
void |
writeTo(OutputStream out)
Writes the encoded TLV element to the specified output stream.
|
public static final int MAX_TLV16_CONTENT_LENGTH
public TLVElement(boolean nonCritical, boolean forwarded, int type)
public TLVElement(boolean inputTlv16, boolean nonCritical, boolean forwarded, int type)
public static TLVElement create(byte[] bytes) throws TLVParserException
bytes
- byte array to create the TLV element from.TLVElement
TLVParserException
public static TLVElement create(int type, long value) throws TLVParserException
Long
content.
TLV element nonCritical and forwarded flags are set to false.type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public static TLVElement create(int type, Date value) throws TLVParserException
Date
content.
TLV element nonCritical and forwarded flags are set to false.type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public static TLVElement create(int type, DataHash value) throws TLVParserException
DataHash
content.
TLV element nonCritical and forwarded flags are set to false.type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public static TLVElement create(int type, String value) throws TLVParserException
String
content.
TLV element nonCritical and forwarded flags are set to false.type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public static TLVElement create(int type, byte[] value) throws TLVParserException
type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public static TLVElement create(int type, HashAlgorithm value) throws TLVParserException
HashAlgorithm
content.
TLV element nonCritical and forwarded flags are set to false.type
- TLV element type.value
- value to be the content of the TLV element.TLVElement
TLVParserException
public final Long getDecodedLong() throws TLVParserException
Long
object.Integer.MAX_VALUE
TLVParserException
- - content contains leading zeros or content contains more than 63 unsigned bitspublic final String getDecodedString() throws TLVParserException
TLVParserException
- when content string isn't null terminated or is malformed UTF-8 data.public final DataHash getDecodedDataHash() throws TLVParserException
DataHash
object.TLVParserException
- when content can not be decoded to data hash.public final Date getDecodedDate() throws TLVParserException
TLVParserException
- when content can not be decoded to a Date object.public HashAlgorithm getDecodedHashAlgorithm() throws TLVParserException
HashAlgorithm
.TLVParserException
public byte[] getContent() throws TLVParserException
TLVParserException
public void setContent(byte[] content) throws TLVParserException
content
- value to set.TLVParserException
public void setStringContent(String s) throws TLVParserException
String
. TLV encoded string is always terminated with a zero octet.s
- string to decode.TLVParserException
public void setLongContent(long value) throws TLVParserException
TLVParserException
public void setDataHashContent(DataHash dataHash) throws TLVParserException
TLVParserException
public void setDateContent(Date date) throws TLVParserException
TLVParserException
public void setHashAlgorithmContent(HashAlgorithm hashAlgorithm) throws TLVParserException
TLVParserException
public TLVElement getFirstChildElement(int tag)
tag
- tag to search.TLVElement
with specified tag,
or null when the child element with specified tag doesn't exist.public TLVElement getFirstChildElement()
TLVElement
.
If current element doesn't contain child elements then null is returned.public TLVElement getLastChildElement()
TLVElement
.
If current element doesn't contain child elements then null is returned.public List<TLVElement> getChildElements(int tag)
tag
- tag to search.TLVElement
's with specified tag or empty list.public List<TLVElement> getChildElements()
public List<TLVElement> getChildElements(int... tags)
public int getType()
public void setType(int type)
public boolean isOutputTlv16()
public boolean isInputTlv16()
public boolean isNonCritical()
public boolean isForwarded()
public byte[] encodeHeader() throws TLVParserException
TLVParserException
- when TLV header encoding fails or I/O error occurs.public int getContentLength()
public int getHeaderLength()
public void replace(TLVElement childToBeReplaced, TLVElement newChild)
childToBeReplaced
- TLV element to be replaced.newChild
- new TLV element.public void remove(TLVElement elementToRemoved)
public void addChildElement(TLVElement element) throws TLVParserException
TLVParserException
public void addFirstChildElement(TLVElement element) throws TLVParserException
TLVParserException
public void writeTo(OutputStream out) throws TLVParserException
out
- the output stream to which to write the TLV element data.TLVParserException
- when I/O error occurred or TLV encoding failed.public byte[] getEncoded() throws TLVParserException
TLVParserException
Copyright © 2024 Guardtime. All rights reserved.