org.snmp4j.smi
Class OctetString

java.lang.Object
  extended by org.snmp4j.smi.AbstractVariable
      extended by org.snmp4j.smi.OctetString
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, BERSerializable, AssignableFromByteArray, AssignableFromString, Variable
Direct Known Subclasses:
BitString, Opaque

public class OctetString
extends AbstractVariable
implements AssignableFromByteArray, AssignableFromString

The OctetString class 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
OctetString()
          Creates a zero length octet string.
OctetString(byte[] rawValue)
          Creates an octet string from an byte array.
OctetString(byte[] rawValue, int offset, int length)
          Creates an octet string from an byte array.
OctetString(OctetString other)
          Creates an octet string from another OctetString by cloning its value.
OctetString(java.lang.String stringValue)
          Creates an octet string from a java string.
 
Method Summary
 void append(byte b)
          Appends a single byte to this octet string.
 void append(byte[] bytes)
          Appends an array of bytes to this octet string.
 void append(OctetString octetString)
          Appends an octet string.
 void append(java.lang.String string)
          Appends the supplied string to this OctetString.
 void clear()
          Sets the value of the octet string to a zero length string.
 java.lang.Object clone()
          Clones this variable.
 int compareTo(java.lang.Object o)
           
 void decodeBER(BERInputStream inputStream)
          Decodes a Variable from an InputStream.
 void encodeBER(java.io.OutputStream outputStream)
          Encodes a Variable to an OutputStream.
 boolean equals(java.lang.Object o)
           
static OctetString fromByteArray(byte[] value)
          Creates an OctetString from an byte array.
static OctetString fromHexString(java.lang.String hexString)
           
static OctetString fromHexString(java.lang.String hexString, char delimiter)
           
static OctetString fromString(java.lang.String string, char delimiter, int radix)
           
static OctetString fromString(java.lang.String string, int radix)
          Creates an OctetString from a string represantation in the specified radix.
 void fromSubIndex(OID subIndex, boolean impliedLength)
          Sets the value of this Variable from the supplied (sub-)index.
 byte get(int index)
          Gets the byte at the specified index.
 int getBERLength()
          Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).
 int getBERPayloadLength()
          Returns the length of the payload of this BERSerializable object in bytes when encoded according to the Basic Encoding Rules (BER).
 int getSyntax()
          Gets the ASN.1 syntax identifier value of this SNMP variable.
 byte[] getValue()
           
 int hashCode()
           
 boolean isPrintable()
          Determines whether this octet string contains non ISO control characters only.
 int length()
          Gets the length of the byte string.
 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.
 void set(int index, byte b)
          Sets the byte value at the specified index.
 void setValue(byte[] value)
          Sets the value of this object from the supplied byte array.
 void setValue(java.lang.String value)
           
static java.util.Collection split(OctetString octetString, OctetString delimOctets)
          Splits an OctetString using a set of delimiter characters similar to how a StringTokenizer would do it.
 boolean startsWith(OctetString prefix)
          Tests if this octet string starts with the specified prefix.
 OctetString substring(int beginIndex, int endIndex)
          Returns a new string that is a substring of this string.
 java.lang.String toASCII(char placeholder)
          Formats the content into a ASCII string.
 byte[] toByteArray()
          Returns the value of this object as a byte array.
 java.lang.String toHexString()
           
 java.lang.String toHexString(char separator)
           
 int toInt()
          Returns an integer representation of this variable if such a representation exists.
 long toLong()
          Returns a long representation of this variable if such a representation exists.
 java.lang.String toString()
          Gets a string representation of the variable.
 java.lang.String toString(char separator, int radix)
           
 java.lang.String toString(int radix)
          Returns a string representation of this octet string in the radix specified.
 OID toSubIndex(boolean impliedLength)
          Converts the value of this Variable to a (sub-)index value.
 
Methods inherited from class org.snmp4j.smi.AbstractVariable
createFromBER, createFromSyntax, getSyntaxFromString, getSyntaxString, getSyntaxString, isDynamic, isException
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

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 from rawValueinto the new OctetSring.
length - the number of bytes to be copied.

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 - an OctetString instance.
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 - an OctetString to append to this octet string.

append

public void append(java.lang.String string)
Appends the supplied string to this OctetString. 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.IOException
Description copied from class: AbstractVariable
Encodes a Variable to an OutputStream.

Specified by:
encodeBER in interface BERSerializable
Specified by:
encodeBER in class AbstractVariable
Parameters:
outputStream - an OutputStream.
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: AbstractVariable
Decodes a Variable from an InputStream.

Specified by:
decodeBER in interface BERSerializable
Specified by:
decodeBER in class AbstractVariable
Parameters:
inputStream - an InputStream containing 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: AbstractVariable
Returns the length of this Variable in bytes when encoded according to the Basic Encoding Rules (BER).

Specified by:
getBERLength in interface BERSerializable
Specified by:
getBERLength in class AbstractVariable
Returns:
the BER encoded length of this variable.

getSyntax

public int getSyntax()
Description copied from class: AbstractVariable
Gets the ASN.1 syntax identifier value of this SNMP variable.

Specified by:
getSyntax in interface Variable
Specified by:
getSyntax in class AbstractVariable
Returns:
an integer value < 128 for regular SMI objects and a value >= 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 - if index < 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 than length().
b - the byte value to set.
Since:
v1.2

hashCode

public int hashCode()
Specified by:
hashCode in interface Variable
Specified by:
hashCode in class AbstractVariable

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface Variable
Specified by:
equals in class AbstractVariable

compareTo

public int compareTo(java.lang.Object o)
Specified by:
compareTo in interface java.lang.Comparable
Specified by:
compareTo in interface Variable
Specified by:
compareTo in class AbstractVariable

substring

public OctetString substring(int beginIndex,
                             int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-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:
true if the bytes of this octet string up to the length of prefix equal those of prefix.
Since:
1.2

isPrintable

public boolean isPrintable()
Determines whether this octet string contains non ISO control characters only.

Returns:
false if this octet string contains any ISO control characters as defined by Character.isISOControl(char) except if these ISO control characters are all whitespace characters as defined by Character.isWhitespace(char) and not '\u001C'-'\u001F'.

toString

public java.lang.String toString()
Description copied from class: AbstractVariable
Gets a string representation of the variable.

Specified by:
toString in interface Variable
Specified by:
toString in class AbstractVariable
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)

fromString

public static OctetString fromString(java.lang.String string,
                                     int radix)
Creates an OctetString from a string represantation in the specified radix.

Parameters:
string - the string representation of an octet string.
radix - the radix of the string represantion.
Returns:
the OctetString instance.
Since:
1.6

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 by round(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:
setValue in interface AssignableFromString

setValue

public void setValue(byte[] value)
Description copied from interface: AssignableFromByteArray
Sets the value of this object from the supplied byte array.

Specified by:
setValue in interface AssignableFromByteArray
Parameters:
value - a byte array.

getValue

public byte[] getValue()

length

public final int length()
Gets the length of the byte string.

Returns:
an integer >= 0.

clone

public java.lang.Object clone()
Description copied from interface: Variable
Clones this variable. Cloning can be used by the SNMP4J API to better support concurrency by creating a immutable clone for internal processing.

Specified by:
clone in interface Variable
Specified by:
clone in class AbstractVariable
Returns:
a new instance of this Variable with the same value.

getBERPayloadLength

public int getBERPayloadLength()
Returns the length of the payload of this BERSerializable object in bytes when encoded according to the Basic Encoding Rules (BER).

Specified by:
getBERPayloadLength in interface BERSerializable
Overrides:
getBERPayloadLength in class AbstractVariable
Returns:
the BER encoded length of this variable.

toInt

public int toInt()
Description copied from class: AbstractVariable
Returns an integer representation of this variable if such a representation exists.

Specified by:
toInt in interface Variable
Specified by:
toInt in class AbstractVariable
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: AbstractVariable
Returns a long representation of this variable if such a representation exists.

Specified by:
toLong in interface Variable
Specified by:
toLong in class AbstractVariable
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: AbstractVariable
Converts the value of this Variable to a (sub-)index value.

Specified by:
toSubIndex in interface Variable
Specified by:
toSubIndex in class AbstractVariable
Parameters:
impliedLength - specifies if the sub-index has an implied length. This parameter applies to variable length variables only (e.g. OctetString and OID). For other variables it has no effect.
Returns:
an OID that represents this value as an (sub-)index.

fromSubIndex

public void fromSubIndex(OID subIndex,
                         boolean impliedLength)
Description copied from class: AbstractVariable
Sets the value of this Variable from the supplied (sub-)index.

Specified by:
fromSubIndex in interface Variable
Specified by:
fromSubIndex in class AbstractVariable
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. OctetString and OID). For other variables it has no effect.

split

public static final java.util.Collection split(OctetString octetString,
                                               OctetString delimOctets)
Splits an OctetString using 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 an OctetString from an byte array.

Parameters:
value - a byte array that is copied into the value of the created OctetString or null.
Returns:
an OctetString or null if value is null.
Since:
1.7

toByteArray

public byte[] toByteArray()
Description copied from interface: AssignableFromByteArray
Returns the value of this object as a byte array.

Specified by:
toByteArray in interface AssignableFromByteArray
Returns:
a byte array.

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.