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
    • Field Detail

      • oidBytes

        private byte[] oidBytes
        A byte[] representation of an OID
      • oidString

        private String oidString
        The OID as a String
      • JOINT_ISO_ITU_T

        private static final BigInteger JOINT_ISO_ITU_T
    • Constructor Detail

      • Oid

        private Oid​(String oidString,
                    byte[] oidBytes)
        Creates a new instance of Oid.
        Parameters:
        oidString - The OID as a String
        oidBytes - The OID as a byte[]
    • 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
      • processStateStart

        private static Oid.OidFSAState processStateStart​(String oid,
                                                         byte[] buffer,
                                                         int pos)
                                                  throws DecoderException
        Process state A
         (Start) --['0','1']--> (A)
         (start) --['2']--> (F)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateA

        private static Oid.OidFSAState processStateA​(String oid,
                                                     int pos)
                                              throws DecoderException
        Process state B
         (A) --['.']--> (B)
         
        Parameters:
        oid - The OID to check
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateB

        private static Oid.OidFSAState processStateB​(String oid,
                                                     byte[] buffer,
                                                     int pos)
                                              throws DecoderException
        Process state B
         (B) --['0']--> (D)
         (B) --['1'..'3']--> (C)
         (B) --['4'..'9']--> (E)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateC

        private static Oid.OidFSAState processStateC​(String oid,
                                                     byte[] buffer,
                                                     int pos)
                                              throws DecoderException
        Process state C
         (C) --['.']--> (K)
         (C) --['0'..'9']--> (E)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateDE

        private static Oid.OidFSAState processStateDE​(String oid,
                                                      byte[] buffer,
                                                      int pos)
                                               throws DecoderException
        Process state D and E
         (D) --['.']--> (K)
         (E) --['.']--> (K)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateF

        private static Oid.OidFSAState processStateF​(String oid,
                                                     int pos)
                                              throws DecoderException
        Process state F
         (F) --['.']--> (G)
         
        Parameters:
        oid - The OID to check
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateG

        private static Oid.OidFSAState processStateG​(String oid,
                                                     byte[] buffer,
                                                     int pos)
                                              throws DecoderException
        Process state G
         (G) --['0']--> (I)
         (G) --['1'..'9']--> (H)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateH

        private static Oid.OidFSAState processStateH​(String oid,
                                                     byte[] buffer,
                                                     int pos)
                                              throws DecoderException
        Process state H
         (H) --['.']--> (K)
         (H) --['0'..'9']--> (J)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateI

        private static Oid.OidFSAState processStateI​(String oid,
                                                     byte[] buffer,
                                                     int pos)
                                              throws DecoderException
        Process state I
         (I) --['.']--> (K)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateJ

        private static Oid.OidFSAState processStateJ​(String oid,
                                                     byte[] buffer,
                                                     int bufferPos,
                                                     int pos)
                                              throws DecoderException
        Process state J
         (J) --['.']--> (K)
         (J) --['0'..'9']--> (J)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        bufferPos - The current position in the buffer
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateK

        private static Oid.OidFSAState processStateK​(String oid,
                                                     byte[] buffer,
                                                     int bufferPos,
                                                     int pos)
                                              throws DecoderException
        Process state J
         (K) --['0']--> (M)
         (K) --['1'..'9']--> (L)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        bufferPos - The current position in the buffer
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateL

        private static Oid.OidFSAState processStateL​(String oid,
                                                     byte[] buffer,
                                                     int bufferPos,
                                                     int pos)
                                              throws DecoderException
        Process state J
         (L) --['.']--> (K)
         (L) --['0'..'9']--> (L)
         
        Parameters:
        oid - The OID to check
        buffer - The buffer gathering the binary OID
        bufferPos - The current position in the buffer
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • processStateM

        private static Oid.OidFSAState processStateM​(String oid,
                                                     int pos)
                                              throws DecoderException
        Process state J
         (M) --['.']--> (K)
         
        Parameters:
        oid - The OID to check
        pos - The position in the OID string
        Returns:
        The next OID decoding state
        Throws:
        DecoderException - If the OID is invalid
      • convert

        private static int convert​(String oid,
                                   byte[] buffer,
                                   int start,
                                   int nbDigits,
                                   int posBuffer,
                                   boolean isJointIsoItuT)
        Convert a list of digits to a list of 7 bits bytes. We must start by the end, and we don't know how many bytes we will need, except when we will be done with the conversion.
        Parameters:
        oid - The OID to convert
        buffer - The buffer gathering the binary OID
        start - The starting position in the OID string
        nbDigits - Teh number of digits the OID has
        posBuffer - The position in the buffer
        isJointIsoItuT - A flag set if we know the OID is a JointIsoItuT OID
        Returns:
        The number of bytes required to store the OID in a PDU
      • 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[]
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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