Class Oid


  • public final class Oid
    extends Object
    An immutable representation of an object identifier that provides conversion between their String, and encoded byte[] representations.

    The encoding of OID values is performed according to itu X.690 section 8.19. Specifically:

    8.19.2 The contents octets shall be an (ordered) list of encodings of subidentifiers (see 8.19.3 and 8.19.4) concatenated together. Each subidentifier is represented as a series of (one or more) octets. Bit 8 of each octet indicates whether it is the last in the series: bit 8 of the last octet is zero; bit 8 of each preceding octet is one. Bits 7 to 1 of the octets in the series collectively encode the subidentifier. Conceptually, these groups of bits are concatenated to form an unsigned binary number whose most significant bit is bit 7 of the first octet and whose least significant bit is bit 1 of the last octet. The subidentifier shall be encoded in the fewest possible octets, that is, the leading octet of the subidentifier shall not have the value 0x80.

    8.19.3 The number of subidentifiers (N) shall be one less than the number of object identifier components in the object identifier value being encoded.

    8.19.4 The numerical value of the first subidentifier is derived from the values of the first two object identifier components in the object identifier value being encoded, using the formula:
    (X*40) + Y
    where X is the value of the first object identifier component and Y is the value of the second object identifier component. NOTE – This packing of the first two object identifier components recognizes that only three values are allocated from the root node, and at most 39 subsequent values from nodes reached by X = 0 and X = 1.

    For example, the OID "2.12.3456.7" would be turned into a list of 3 values: [((2*40)+12), 3456, 7]. The first of which, 92, would be encoded as the bytes 0x5C, the second would be [0x9B, 0x00], and the third as 0x07 giving the final encoding [0x5C, 0x9B, 0x00, 0x07].

    Author:
    Apache Directory Project
    • Method Detail

      • fromBytes

        public static Oid fromBytes​(byte[] oidBytes)
                             throws DecoderException
        Decodes an OID from a byte[].
        Parameters:
        oidBytes - The encodedbyte[]
        Returns:
        A new Oid
        Throws:
        DecoderException - When the OID is not valid
      • fromString

        public static Oid fromString​(String oidString)
                              throws DecoderException
        Returns an OID object representing oidString.
        Parameters:
        oidString - The string representation of the OID
        Returns:
        A new Oid
        Throws:
        DecoderException - When the OID is not valid
      • getEncodedLength

        public int getEncodedLength()
        Returns the length of the encoded byte[] representation.
        Returns:
        The length of the byte[]
      • isOid

        public static boolean isOid​(String oidString)
        Returns true if oidString is a valid string representation of an OID. This method simply calls fromString(String) and returns true if no exception was thrown. As such, it should not be used in an attempt to check if a string is a valid OID before calling fromString(String).
        Parameters:
        oidString - The string to test
        Returns:
        True, if oidString is valid
      • toBytes

        public byte[] toBytes()
        Returns the byte[] representation of the OID. The byte[] that is returned is copied from the internal value so as to preserve the immutability of an OID object. If the output of a call to this method is intended to be written to a stream, the writeBytesTo(OutputStream) should be used instead as it will avoid creating this copy.
        Returns:
        The encoded byte[] representation of the OID.
      • toString

        public String toString()
        Returns the string representation of the OID.
        Overrides:
        toString in class Object
        Returns:
        The string representation of the OID
      • writeBytesTo

        public void writeBytesTo​(ByteBuffer buffer)
        Writes the bytes respresenting this OID to the provided buffer. This should be used in preference to the toBytes() method in order to prevent the creation of copies of the actual byte[].
        Parameters:
        buffer - The buffer to write the bytes into
      • writeBytesTo

        public void writeBytesTo​(OutputStream outputStream)
                          throws IOException
        Writes the bytes respresenting this OID to the provided stream. This should be used in preference to the toBytes() method in order to prevent the creation of copies of the actual byte[].
        Parameters:
        outputStream - The stream to write the bytes to
        Throws:
        IOException - When we can't write the OID into a Stream