- java.lang.Object
-
- org.snmp4j.smi.AbstractVariable
-
- org.snmp4j.smi.OctetString
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Comparable<Variable>,BERSerializable,AssignableFromByteArray,AssignableFromString,Variable
- Direct Known Subclasses:
BitString,Opaque,TsmSecurityParameters
public class OctetString extends AbstractVariable implements AssignableFromByteArray, AssignableFromString
TheOctetStringclass represents the SMI type OCTET STRING.- Since:
- 1.0
- Version:
- 1.8
- Author:
- Frank Fock
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class org.snmp4j.smi.AbstractVariable
SMISYNTAXES_PROPERTIES
-
-
Constructor Summary
Constructors Constructor Description OctetString()Creates a zero length octet string.OctetString(byte[] rawValue)Creates an octet string from an byte array.OctetString(byte[] rawValuePrefix, byte[] rawValueSuffix)Creates a concatenated octet string from two byte arrays.OctetString(byte[] rawValue, int offset, int length)Creates an octet string from an byte array.OctetString(java.lang.String stringValue)Creates an octet string from a java string.OctetString(OctetString other)Creates an octet string from another OctetString by cloning its value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(byte b)Appends a single byte to this octet string.voidappend(byte[] bytes)Appends an array of bytes to this octet string.voidappend(java.lang.String string)Appends the supplied string to thisOctetString.voidappend(OctetString octetString)Appends an octet string.voidclear()Sets the value of the octet string to a zero length string.java.lang.Objectclone()Clones this variable.intcompareTo(Variable o)voiddecodeBER(BERInputStream inputStream)Decodes aVariablefrom anBERInputStream.voidencodeBER(java.io.OutputStream outputStream)Encodes aVariableto anOutputStream.booleanequals(java.lang.Object o)booleanequalsValue(byte[] v)Checks if the value of this OctetString equals the argument.static OctetStringfromByteArray(byte[] value)Creates anOctetStringfrom an byte array.static OctetStringfromCharArray(char[] chars)Creates an OctetString from a UTF-8 encoded character array in a secure manner by zeroing out any intermediate buffers immediately and not using anStringobject.static OctetStringfromCharArray(char[] string, char delimiter, int radix)Creates an OctetString from achararray by converting the delimited character sequence using the given radix into a byte array internally.static OctetStringfromHexString(java.lang.String hexString)static OctetStringfromHexString(java.lang.String hexString, char delimiter)static OctetStringfromHexStringPairs(java.lang.String hexString)Create an OctetString from a hexadecimal string of 2-byte pairs without delimiter.static OctetStringfromIndex(OID subIndex, int beginIndex, boolean impliedLength)Returns anOctetStringfrom the provided index OID decoded as OctetString with or without implied length.static OctetStringfromString(java.lang.String stringOrNull)Creates anOctetStringfrom a string valuestatic OctetStringfromString(java.lang.String string, char delimiter, int radix)static OctetStringfromString(java.lang.String string, int radix)Creates an OctetString from a string representation in the specified radix.voidfromSubIndex(OID subIndex, boolean impliedLength)Sets the value of thisVariablefrom the supplied (sub-)index.byteget(int index)Gets the byte at the specified index.intgetBERLength()Returns the length of thisVariablein bytes when encoded according to the Basic Encoding Rules (BER).intgetBERPayloadLength()Returns the length of the payload of thisBERSerializableobject in bytes when encoded according to the Basic Encoding Rules (BER).intgetSyntax()Gets the ASN.1 syntax identifier value of this SNMP variable.byte[]getValue()inthashCode()booleanisPrintable()Determines whether this octet string contains non ISO control characters only.intlength()Gets the length of the byte string.OctetStringmask(OctetString mask)Returns a copy of this OctetString where each bit not set in the supplied mask zeros the corresponding bit in the returned OctetString.voidset(int index, byte b)Sets the byte value at the specified index.voidsetValue(byte[] value)Sets the value of this object from the supplied byte array.voidsetValue(java.lang.String value)static java.util.Collection<OctetString>split(OctetString octetString, OctetString delimOctets)Splits anOctetStringusing a set of delimiter characters similar to how a StringTokenizer would do it.booleanstartsWith(OctetString prefix)Tests if this octet string starts with the specified prefix.OctetStringsubstring(int beginIndex, int endIndex)Returns a new string that is a substring of this string.java.lang.StringtoASCII(char placeholder)Formats the content into a ASCII string.byte[]toByteArray()Returns the value of this object as a byte array.java.lang.StringtoHexString()java.lang.StringtoHexString(char separator)inttoInt()Returns an integer representation of this variable if such a representation exists.longtoLong()Returns a long representation of this variable if such a representation exists.java.lang.StringtoString()Gets a string representation of the variable.java.lang.StringtoString(char separator, int radix)java.lang.StringtoString(int radix)Returns a string representation of this octet string in the radix specified.OIDtoSubIndex(boolean impliedLength)Converts the value of thisVariableto a (sub-)index value.-
Methods inherited from class org.snmp4j.smi.AbstractVariable
createFromBER, createFromSyntax, equal, getSyntaxFromString, getSyntaxString, getSyntaxString, isDynamic, isException
-
-
-
-
Constructor Detail
-
OctetString
public OctetString()
Creates a zero length octet string.
-
OctetString
public OctetString(byte[] rawValue)
Creates an octet string from an byte array.- Parameters:
rawValue- an array of bytes.
-
OctetString
public OctetString(byte[] rawValue, int offset, int length)Creates an octet string from an byte array.- Parameters:
rawValue- an array of bytes.offset- the position (zero based) of the first byte to be copied fromrawValueinto the newOctetString.length- the number of bytes to be copied.
-
OctetString
public OctetString(byte[] rawValuePrefix, byte[] rawValueSuffix)Creates a concatenated octet string from two byte arrays.- Parameters:
rawValuePrefix- an array of bytes.rawValueSuffix- an array of bytes which will appended to rawValuePrefix to form this new OctetString. If rawValueSuffix isnullthen the result will be the same as with a zero length suffix array.- Since:
- 2.6.0
-
OctetString
public OctetString(java.lang.String stringValue)
Creates an octet string from a java string.- Parameters:
stringValue- a Java string.
-
OctetString
public OctetString(OctetString other)
Creates an octet string from another OctetString by cloning its value.- Parameters:
other- anOctetStringinstance.
-
-
Method Detail
-
append
public void append(byte b)
Appends a single byte to this octet string.- Parameters:
b- a byte value.
-
append
public void append(byte[] bytes)
Appends an array of bytes to this octet string.- Parameters:
bytes- an array of bytes.
-
append
public void append(OctetString octetString)
Appends an octet string.- Parameters:
octetString- anOctetStringto append to this octet string.
-
append
public void append(java.lang.String string)
Appends the supplied string to thisOctetString. Calling this method is identical to append(string.getBytes()).- Parameters:
string- a String instance.
-
clear
public void clear()
Sets the value of the octet string to a zero length string.
-
encodeBER
public void encodeBER(java.io.OutputStream outputStream) throws java.io.IOExceptionDescription copied from class:AbstractVariableEncodes aVariableto anOutputStream.- Specified by:
encodeBERin interfaceBERSerializable- Specified by:
encodeBERin classAbstractVariable- Parameters:
outputStream- anOutputStream.- Throws:
java.io.IOException- if an error occurs while writing to the stream.
-
decodeBER
public void decodeBER(BERInputStream inputStream) throws java.io.IOException
Description copied from class:AbstractVariableDecodes aVariablefrom anBERInputStream.- Specified by:
decodeBERin interfaceBERSerializable- Specified by:
decodeBERin classAbstractVariable- Parameters:
inputStream- anBERInputStreamcontaining a BER encoded byte stream.- Throws:
java.io.IOException- if the stream could not be decoded by using BER rules.
-
getBERLength
public int getBERLength()
Description copied from class:AbstractVariableReturns the length of thisVariablein bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERLengthin interfaceBERSerializable- Specified by:
getBERLengthin classAbstractVariable- Returns:
- the BER encoded length of this variable.
-
getSyntax
public int getSyntax()
Description copied from class:AbstractVariableGets the ASN.1 syntax identifier value of this SNMP variable.- Specified by:
getSyntaxin interfaceVariable- Specified by:
getSyntaxin classAbstractVariable- Returns:
- an integer value less than 128 for regular SMI objects and a value greater or equal than 128 for exception values like noSuchObject, noSuchInstance, and endOfMibView.
-
get
public final byte get(int index)
Gets the byte at the specified index.- Parameters:
index- a zero-based index into the octet string.- Returns:
- the byte value at the specified index.
- Throws:
java.lang.ArrayIndexOutOfBoundsException- ifindex< 0 or >length().
-
set
public final void set(int index, byte b)Sets the byte value at the specified index.- Parameters:
index- an index value greater or equal 0 and less thanlength().b- the byte value to set.- Since:
- v1.2
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceVariable- Specified by:
hashCodein classAbstractVariable
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equalsin interfaceVariable- Specified by:
equalsin classAbstractVariable
-
equalsValue
public boolean equalsValue(byte[] v)
Checks if the value of this OctetString equals the argument.- Parameters:
v- the byte array to compare with this OctetStrings value member.- Returns:
Arrays.equals(value, (byte[])v)- Since:
- 2.0
-
compareTo
public int compareTo(Variable o)
- Specified by:
compareToin interfacejava.lang.Comparable<Variable>- Specified by:
compareToin interfaceVariable- Specified by:
compareToin classAbstractVariable
-
substring
public OctetString substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specifiedbeginIndexand extends to the character at indexendIndex - 1. Thus the length of the substring isendIndex-beginIndex.- Parameters:
beginIndex- the beginning index, inclusive.endIndex- the ending index, exclusive.- Returns:
- the specified substring.
- Since:
- 1.3
-
startsWith
public boolean startsWith(OctetString prefix)
Tests if this octet string starts with the specified prefix.- Parameters:
prefix- the prefix.- Returns:
trueif the bytes of this octet string up to the length ofprefixequal those ofprefix.- Since:
- 1.2
-
isPrintable
public boolean isPrintable()
Determines whether this octet string contains non ISO control characters only.- Returns:
falseif this octet string contains any ISO control characters as defined byCharacter.isISOControl(char)except if these ISO control characters are all whitespace characters as defined byCharacter.isWhitespace(char)and not'\u001C'-'\u001F'.
-
toString
public java.lang.String toString()
Description copied from class:AbstractVariableGets a string representation of the variable.- Specified by:
toStringin interfaceVariable- Specified by:
toStringin classAbstractVariable- Returns:
- a string representation of the variable's value.
-
toHexString
public java.lang.String toHexString()
-
toHexString
public java.lang.String toHexString(char separator)
-
fromHexString
public static OctetString fromHexString(java.lang.String hexString)
-
fromHexString
public static OctetString fromHexString(java.lang.String hexString, char delimiter)
-
fromString
public static OctetString fromString(java.lang.String string, char delimiter, int radix)
-
fromHexStringPairs
public static OctetString fromHexStringPairs(java.lang.String hexString)
Create an OctetString from a hexadecimal string of 2-byte pairs without delimiter. For example: 08A69E- Parameters:
hexString- a string of characters a-f,A-F,0-9 with length 2*b, where b is the length of the string in bytes.- Returns:
- an OctetString instance with the length
hexString.length()/2ornullifhexStringisnull. - Since:
- 2.1
-
fromString
public static OctetString fromString(java.lang.String string, int radix)
Creates an OctetString from a string representation in the specified radix.- Parameters:
string- the string representation of an octet string ornullradix- the radix of the string representation.- Returns:
- the OctetString instance or
nullifstringisnull. - Since:
- 1.6
-
fromCharArray
public static OctetString fromCharArray(char[] string, char delimiter, int radix)
Creates an OctetString from achararray by converting the delimited character sequence using the given radix into a byte array internally. Intermediate conversion buffers will be filled with zero data immediately.- Parameters:
string- a UTF-8 character sequence of encoded bytes by radix (i.e. radix of 16 parses a hexadecimal string.delimiter- the delimiter character between the byte representation character sequences.radix- the radix to be used while parsing.- Returns:
- an OctetString with the bytes parsed from
string. - Since:
- 3.4.1
-
fromCharArray
public static OctetString fromCharArray(char[] chars)
Creates an OctetString from a UTF-8 encoded character array in a secure manner by zeroing out any intermediate buffers immediately and not using anStringobject.- Parameters:
chars- an array of UTF-8 encoded characters.- Returns:
- an OctetString instance with the characters converted to bytes.
- Since:
- 3.4.1
-
fromString
public static OctetString fromString(java.lang.String stringOrNull)
Creates anOctetStringfrom a string value- Parameters:
stringOrNull- a string ornull.- Returns:
- the
OctetStringrepresentation ornullifstringOrNullisnull. - Since:
- 3.4.0
-
toString
public java.lang.String toString(char separator, int radix)
-
toString
public java.lang.String toString(int radix)
Returns a string representation of this octet string in the radix specified. There will be no separation characters, but each byte will be represented byround(log(256)/log(radix))digits.- Parameters:
radix- the radix to use in the string representation.- Returns:
- a string representation of this ocetet string in the specified radix.
- Since:
- 1.6
-
toASCII
public java.lang.String toASCII(char placeholder)
Formats the content into a ASCII string. Non-printable characters are replaced by the supplied placeholder character.- Parameters:
placeholder- a placeholder character, for example '.'.- Returns:
- the contents of this octet string as ASCII formatted string.
- Since:
- 1.6
-
setValue
public void setValue(java.lang.String value)
- Specified by:
setValuein interfaceAssignableFromString
-
setValue
public void setValue(byte[] value)
Description copied from interface:AssignableFromByteArraySets the value of this object from the supplied byte array.- Specified by:
setValuein interfaceAssignableFromByteArray- Parameters:
value- a byte array.
-
getValue
public byte[] getValue()
-
length
public final int length()
Gets the length of the byte string.- Returns:
- a zero or positive integer value.
-
clone
public java.lang.Object clone()
Description copied from interface:VariableClones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a clone for internal processing. The content of this object is independent to the content of the clone. Thus, changes to the clone will have no effect to this object.- Specified by:
clonein interfaceVariable- Specified by:
clonein classAbstractVariable- Returns:
- a new instance of this
Variablewith the same value.
-
getBERPayloadLength
public int getBERPayloadLength()
Returns the length of the payload of thisBERSerializableobject in bytes when encoded according to the Basic Encoding Rules (BER).- Specified by:
getBERPayloadLengthin interfaceBERSerializable- Overrides:
getBERPayloadLengthin classAbstractVariable- Returns:
- the BER encoded length of this variable.
-
toInt
public int toInt()
Description copied from class:AbstractVariableReturns an integer representation of this variable if such a representation exists.- Specified by:
toIntin interfaceVariable- Specified by:
toIntin classAbstractVariable- Returns:
- an integer value (if the native representation of this variable would be a long, then the long value will be casted to int).
-
toLong
public long toLong()
Description copied from class:AbstractVariableReturns a long representation of this variable if such a representation exists.- Specified by:
toLongin interfaceVariable- Specified by:
toLongin classAbstractVariable- Returns:
- a long value.
-
mask
public OctetString mask(OctetString mask)
Returns a copy of this OctetString where each bit not set in the supplied mask zeros the corresponding bit in the returned OctetString.- Parameters:
mask- a mask where the n-th bit corresponds to the n-th bit in the returned OctetString.- Returns:
- the masked OctetString.
- Since:
- 1.7
-
toSubIndex
public OID toSubIndex(boolean impliedLength)
Description copied from class:AbstractVariableConverts the value of thisVariableto a (sub-)index value.- Specified by:
toSubIndexin interfaceVariable- Specified by:
toSubIndexin classAbstractVariable- Parameters:
impliedLength- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetStringandOID). For other variables it has no effect.- Returns:
- an OID that represents this value as an (sub-)index.
-
fromIndex
public static OctetString fromIndex(OID subIndex, int beginIndex, boolean impliedLength)
Returns anOctetStringfrom the provided index OID decoded as OctetString with or without implied length.- Parameters:
subIndex- an index OID sub-identifier sequence.beginIndex- that starting sub-identifier (zero-based) where the encoded OCTET STRING begins (inclusive)impliedLength- specifies if the first sub-identifier (at begin index) contains the encoded length of the string or iffalsethe rest of thesubIndexwill be decoded as OCTET STRING.- Returns:
- the specified OctetString.
- Since:
- 3.4.1
-
fromSubIndex
public void fromSubIndex(OID subIndex, boolean impliedLength)
Description copied from class:AbstractVariableSets the value of thisVariablefrom the supplied (sub-)index.- Specified by:
fromSubIndexin interfaceVariable- Specified by:
fromSubIndexin classAbstractVariable- Parameters:
subIndex- the sub-index OID.impliedLength- specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g.OctetStringandOID). For other variables it has no effect.
-
split
public static java.util.Collection<OctetString> split(OctetString octetString, OctetString delimOctets)
Splits anOctetStringusing a set of delimiter characters similar to how a StringTokenizer would do it.- Parameters:
octetString- the input string to tokenize.delimOctets- a set of delimiter octets.- Returns:
- a Collection of OctetString instances that contain the tokens.
-
fromByteArray
public static OctetString fromByteArray(byte[] value)
Creates anOctetStringfrom an byte array.- Parameters:
value- a byte array that is copied into the value of the createdOctetStringornull.- Returns:
- an OctetString or
nullifvalueisnull. - Since:
- 1.7
-
toByteArray
public byte[] toByteArray()
Description copied from interface:AssignableFromByteArrayReturns the value of this object as a byte array.- Specified by:
toByteArrayin interfaceAssignableFromByteArray- Returns:
- a byte array.
-
-