org.snmp4j.asn1
Class BER

java.lang.Object
  extended by org.snmp4j.asn1.BER

public class BER
extends java.lang.Object

The BER class provides utility methods for the BER encoding and decoding.

Version:
1.7.4
Author:
Jochen Katz & Frank Fock

Nested Class Summary
static class BER.MutableByte
          The MutableByte class serves for exchanging type information from the various decode* methods.
 
Field Summary
static byte ASN_APPLICATION
           
static byte ASN_BIT_STR
           
static byte ASN_BIT8
           
static byte ASN_BOOLEAN
           
static byte ASN_CONSTRUCTOR
           
static byte ASN_CONTEXT
           
static byte ASN_EXTENSION_ID
           
static byte ASN_INTEGER
           
static byte ASN_LONG_LEN
           
static byte ASN_NULL
           
static byte ASN_OBJECT_ID
           
static byte ASN_OCTET_STR
           
static byte ASN_PRIMITIVE
           
static byte ASN_PRIVATE
           
static byte ASN_SEQUENCE
           
static byte ASN_SET
           
static byte ASN_UNIVERSAL
           
static byte BITSTRING
           
static byte COUNTER
           
static byte COUNTER32
           
static byte COUNTER64
           
static int ENDOFMIBVIEW
           
static byte GAUGE
           
static byte GAUGE32
           
static byte INTEGER
           
static byte INTEGER32
           
static byte IPADDRESS
           
static int MAX_OID_LENGTH
           
static int NOSUCHINSTANCE
           
static int NOSUCHOBJECT
           
static byte NULL
           
static byte OCTETSTRING
           
static byte OID
           
static byte OPAQUE
           
static byte SEQUENCE
           
static byte TIMETICKS
           
 
Constructor Summary
BER()
           
 
Method Summary
static void checkSequenceLength(int expectedLength, BERSerializable sequence)
           
static void checkSequenceLength(int expectedLength, int actualLength, BERSerializable sequence)
           
static int decodeHeader(BERInputStream is, BER.MutableByte type)
          Decodes an ASN.1 header for an object with the ID and length specified.
static int decodeHeader(BERInputStream is, BER.MutableByte type, boolean checkLength)
          Decodes an ASN.1 header for an object with the ID and length specified.
static int decodeInteger(BERInputStream is, BER.MutableByte type)
           
static int decodeLength(BERInputStream is)
          Decodes a ASN.1 length.
static int decodeLength(BERInputStream is, boolean checkLength)
          Decodes a ASN.1 length.
static void decodeNull(BERInputStream is, BER.MutableByte type)
           
static int[] decodeOID(BERInputStream is, BER.MutableByte type)
           
static byte[] decodeString(BERInputStream is, BER.MutableByte type)
           
static long decodeUnsignedInt64(BERInputStream is, BER.MutableByte type)
           
static long decodeUnsignedInteger(BERInputStream is, BER.MutableByte type)
           
static void encodeHeader(java.io.OutputStream os, int type, int length)
          Encodes an ASN.1 header for an object with the ID and length specified.
static void encodeHeader(java.io.OutputStream os, int type, int length, int numBytesLength)
          Encodes an ASN.1 header for an object with the ID and length specified with a fixed length of the encoded length as supplied.
static void encodeInteger(java.io.OutputStream os, byte type, int value)
          Encode a signed integer.
static void encodeLength(java.io.OutputStream os, int length)
          Encodes the length of an ASN.1 object.
static void encodeLength(java.io.OutputStream os, int length, int numLengthBytes)
          Encodes the length of an ASN.1 object.
static void encodeOID(java.io.OutputStream os, byte type, int[] oid)
          Encode an ASN.1 oid filled with the supplied oid value.
static void encodeSequence(java.io.OutputStream os, byte type, int length)
          Encode an ASN.1 header for a sequence with the ID and length specified.
static void encodeString(java.io.OutputStream os, byte type, byte[] string)
          Encode an ASN.1 octet string filled with the supplied input string.
static void encodeUnsignedInt64(java.io.OutputStream os, byte type, long value)
           
static void encodeUnsignedInteger(java.io.OutputStream os, byte type, long value)
          Encode an unsigned integer.
static int getBERLengthOfLength(int length)
          Compute the space needed to encode the length.
static int getOIDLength(int[] value)
          Gets the payload length in bytes of the BER encoded OID value.
static boolean isCheckSequenceLength()
          Gets the SEQUENCE length checking mode.
 boolean isCheckValueLength()
           
static void setCheckSequenceLength(boolean checkSequenceLen)
          Sets the application wide SEQUENCE length checking mode.
 void setCheckValueLength(boolean checkValueLength)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASN_BOOLEAN

public static final byte ASN_BOOLEAN
See Also:
Constant Field Values

ASN_INTEGER

public static final byte ASN_INTEGER
See Also:
Constant Field Values

ASN_BIT_STR

public static final byte ASN_BIT_STR
See Also:
Constant Field Values

ASN_OCTET_STR

public static final byte ASN_OCTET_STR
See Also:
Constant Field Values

ASN_NULL

public static final byte ASN_NULL
See Also:
Constant Field Values

ASN_OBJECT_ID

public static final byte ASN_OBJECT_ID
See Also:
Constant Field Values

ASN_SEQUENCE

public static final byte ASN_SEQUENCE
See Also:
Constant Field Values

ASN_SET

public static final byte ASN_SET
See Also:
Constant Field Values

ASN_UNIVERSAL

public static final byte ASN_UNIVERSAL
See Also:
Constant Field Values

ASN_APPLICATION

public static final byte ASN_APPLICATION
See Also:
Constant Field Values

ASN_CONTEXT

public static final byte ASN_CONTEXT
See Also:
Constant Field Values

ASN_PRIVATE

public static final byte ASN_PRIVATE
See Also:
Constant Field Values

ASN_PRIMITIVE

public static final byte ASN_PRIMITIVE
See Also:
Constant Field Values

ASN_CONSTRUCTOR

public static final byte ASN_CONSTRUCTOR
See Also:
Constant Field Values

ASN_LONG_LEN

public static final byte ASN_LONG_LEN
See Also:
Constant Field Values

ASN_EXTENSION_ID

public static final byte ASN_EXTENSION_ID
See Also:
Constant Field Values

ASN_BIT8

public static final byte ASN_BIT8
See Also:
Constant Field Values

INTEGER

public static final byte INTEGER
See Also:
Constant Field Values

INTEGER32

public static final byte INTEGER32
See Also:
Constant Field Values

BITSTRING

public static final byte BITSTRING
See Also:
Constant Field Values

OCTETSTRING

public static final byte OCTETSTRING
See Also:
Constant Field Values

NULL

public static final byte NULL
See Also:
Constant Field Values

OID

public static final byte OID
See Also:
Constant Field Values

SEQUENCE

public static final byte SEQUENCE
See Also:
Constant Field Values

IPADDRESS

public static final byte IPADDRESS
See Also:
Constant Field Values

COUNTER

public static final byte COUNTER
See Also:
Constant Field Values

COUNTER32

public static final byte COUNTER32
See Also:
Constant Field Values

GAUGE

public static final byte GAUGE
See Also:
Constant Field Values

GAUGE32

public static final byte GAUGE32
See Also:
Constant Field Values

TIMETICKS

public static final byte TIMETICKS
See Also:
Constant Field Values

OPAQUE

public static final byte OPAQUE
See Also:
Constant Field Values

COUNTER64

public static final byte COUNTER64
See Also:
Constant Field Values

NOSUCHOBJECT

public static final int NOSUCHOBJECT
See Also:
Constant Field Values

NOSUCHINSTANCE

public static final int NOSUCHINSTANCE
See Also:
Constant Field Values

ENDOFMIBVIEW

public static final int ENDOFMIBVIEW
See Also:
Constant Field Values

MAX_OID_LENGTH

public static final int MAX_OID_LENGTH
See Also:
Constant Field Values
Constructor Detail

BER

public BER()
Method Detail

encodeHeader

public static final void encodeHeader(java.io.OutputStream os,
                                      int type,
                                      int length)
                               throws java.io.IOException
Encodes an ASN.1 header for an object with the ID and length specified.

Parameters:
os - an OutputStream to which the header is encoded.
type - the type of the ASN.1 object. Must be < 30, i.e. no extension octets.
length - the length of the object. The maximum length is 0xFFFFFFFF;
Throws:
java.io.IOException

encodeHeader

public static final void encodeHeader(java.io.OutputStream os,
                                      int type,
                                      int length,
                                      int numBytesLength)
                               throws java.io.IOException
Encodes an ASN.1 header for an object with the ID and length specified with a fixed length of the encoded length as supplied.

Parameters:
os - an OutputStream to which the header is encoded.
type - the type of the ASN.1 object. Must be < 30, i.e. no extension octets.
length - the length of the object. The maximum length is 0xFFFFFFFF;
numBytesLength - the number of bytes used to encode the length of the length.
Throws:
java.io.IOException

getBERLengthOfLength

public static final int getBERLengthOfLength(int length)
Compute the space needed to encode the length.

Parameters:
length - Length to encode
Returns:
the count of bytes needed to encode the value length

encodeLength

public static final void encodeLength(java.io.OutputStream os,
                                      int length)
                               throws java.io.IOException
Encodes the length of an ASN.1 object.

Parameters:
os - an OutputStream to which the length is encoded.
length - the length of the object. The maximum length is 0xFFFFFFFF;
Throws:
java.io.IOException

encodeLength

public static final void encodeLength(java.io.OutputStream os,
                                      int length,
                                      int numLengthBytes)
                               throws java.io.IOException
Encodes the length of an ASN.1 object.

Parameters:
os - an OutputStream to which the length is encoded.
length - the length of the object. The maximum length is 0xFFFFFFFF;
numLengthBytes - the number of bytes to be used to encode the length using the long form.
Throws:
java.io.IOException

encodeInteger

public static final void encodeInteger(java.io.OutputStream os,
                                       byte type,
                                       int value)
                                throws java.io.IOException
Encode a signed integer.

Parameters:
os - an OutputStream to which the length is encoded.
type - the tag type for the integer (typically 0x02)
value - the integer value to encode.
Throws:
java.io.IOException

encodeUnsignedInteger

public static final void encodeUnsignedInteger(java.io.OutputStream os,
                                               byte type,
                                               long value)
                                        throws java.io.IOException
Encode an unsigned integer. ASN.1 integer ::= 0x02 asnlength byte {byte}*

Parameters:
os - an OutputStream to which the length is encoded.
type - the tag type for the integer (typically 0x02)
value - the integer value to encode.
Throws:
java.io.IOException

encodeString

public static final void encodeString(java.io.OutputStream os,
                                      byte type,
                                      byte[] string)
                               throws java.io.IOException
Encode an ASN.1 octet string filled with the supplied input string.

Parameters:
os - an OutputStream to which the length is encoded.
type - the tag type for the integer (typically 0x02)
string - the byte array containing the octet string value.
Throws:
java.io.IOException

encodeSequence

public static final void encodeSequence(java.io.OutputStream os,
                                        byte type,
                                        int length)
                                 throws java.io.IOException
Encode an ASN.1 header for a sequence with the ID and length specified. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;

Parameters:
os - an OutputStream to which the length is encoded.
type - the tag type for the integer (typically 0x02)
length - the length of the sequence to encode.
Throws:
java.io.IOException

getOIDLength

public static final int getOIDLength(int[] value)
Gets the payload length in bytes of the BER encoded OID value.

Parameters:
value - an array of unsigned integer values representing an object identifier.
Returns:
the BER encoded length of the OID without header and length.

encodeOID

public static final void encodeOID(java.io.OutputStream os,
                                   byte type,
                                   int[] oid)
                            throws java.io.IOException
Encode an ASN.1 oid filled with the supplied oid value.

Parameters:
os - an OutputStream to which the length is encoded.
type - the tag type for the integer (typically 0x06)
oid - the int array containing the OID value.
Throws:
java.io.IOException

encodeUnsignedInt64

public static final void encodeUnsignedInt64(java.io.OutputStream os,
                                             byte type,
                                             long value)
                                      throws java.io.IOException
Throws:
java.io.IOException

decodeLength

public static final int decodeLength(BERInputStream is)
                              throws java.io.IOException
Decodes a ASN.1 length.

Parameters:
is - an InputStream
Returns:
the decoded length.
Throws:
java.io.IOException

decodeLength

public static final int decodeLength(BERInputStream is,
                                     boolean checkLength)
                              throws java.io.IOException
Decodes a ASN.1 length.

Parameters:
is - an InputStream
checkLength - if false length check is always suppressed.
Returns:
the decoded length.
Throws:
java.io.IOException

decodeHeader

public static final int decodeHeader(BERInputStream is,
                                     BER.MutableByte type,
                                     boolean checkLength)
                              throws java.io.IOException
Decodes an ASN.1 header for an object with the ID and length specified. On entry, datalength is input as the number of valid bytes following "data". On exit, it is returned as the number of valid bytes in this object following the id and length. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;

Parameters:
is - the BERInputStream to decode.
type - returns the type of the object at the current position in the input stream.
checkLength - if false length check is always suppressed.
Returns:
the decoded length of the object.
Throws:
java.io.IOException

decodeHeader

public static final int decodeHeader(BERInputStream is,
                                     BER.MutableByte type)
                              throws java.io.IOException
Decodes an ASN.1 header for an object with the ID and length specified. On entry, datalength is input as the number of valid bytes following "data". On exit, it is returned as the number of valid bytes in this object following the id and length. This only works on data types < 30, i.e. no extension octets. The maximum length is 0xFFFF;

Parameters:
is - the BERInputStream to decode.
type - returns the type of the object at the current position in the input stream.
Returns:
the decoded length of the object.
Throws:
java.io.IOException

decodeInteger

public static final int decodeInteger(BERInputStream is,
                                      BER.MutableByte type)
                               throws java.io.IOException
Throws:
java.io.IOException

decodeUnsignedInteger

public static final long decodeUnsignedInteger(BERInputStream is,
                                               BER.MutableByte type)
                                        throws java.io.IOException
Throws:
java.io.IOException

decodeString

public static final byte[] decodeString(BERInputStream is,
                                        BER.MutableByte type)
                                 throws java.io.IOException
Throws:
java.io.IOException

decodeOID

public static final int[] decodeOID(BERInputStream is,
                                    BER.MutableByte type)
                             throws java.io.IOException
Throws:
java.io.IOException

decodeNull

public static final void decodeNull(BERInputStream is,
                                    BER.MutableByte type)
                             throws java.io.IOException
Throws:
java.io.IOException

decodeUnsignedInt64

public static final long decodeUnsignedInt64(BERInputStream is,
                                             BER.MutableByte type)
                                      throws java.io.IOException
Throws:
java.io.IOException

isCheckSequenceLength

public static boolean isCheckSequenceLength()
Gets the SEQUENCE length checking mode.

Returns:
true if the length of a parsed SEQUENCE should be checked against the real length of the objects parsed.

setCheckSequenceLength

public static void setCheckSequenceLength(boolean checkSequenceLen)
Sets the application wide SEQUENCE length checking mode.

Parameters:
checkSequenceLen - specifies whether he length of a parsed SEQUENCE should be checked against the real length of the objects parsed.

checkSequenceLength

public static void checkSequenceLength(int expectedLength,
                                       BERSerializable sequence)
                                throws java.io.IOException
Throws:
java.io.IOException

checkSequenceLength

public static void checkSequenceLength(int expectedLength,
                                       int actualLength,
                                       BERSerializable sequence)
                                throws java.io.IOException
Throws:
java.io.IOException

isCheckValueLength

public boolean isCheckValueLength()

setCheckValueLength

public void setCheckValueLength(boolean checkValueLength)

Copyright 2005-2010 Frank Fock (SNMP4J.org)

Copyright © 2011 SNMP4J.org. All Rights Reserved.