ASN1OctetString (Bouncy Castle Library 1.85 API Specification)
Date: 2026-07-12

org.bouncycastle.asn1

## Class ASN1OctetString

- java.lang.Object
- - org.bouncycastle.asn1.ASN1Object
  - - org.bouncycastle.asn1.ASN1Primitive
    - - org.bouncycastle.asn1.ASN1OctetString

- All Implemented Interfaces:
    ASN1Encodable, ASN1OctetStringParser, InMemoryRepresentable, Encodable
  Direct Known Subclasses:
    BEROctetString, DEROctetString

      public abstract class ASN1OctetString
      extends ASN1Primitive
      implements ASN1OctetStringParser

  Abstract base for the ASN.1 OCTET STRING data type

  This supports BER, and DER forms of the data.

  DER form is always primitive single OCTET STRING, while BER support includes the constructed forms.

  X.690

  8: Basic encoding rules

  8.7 Encoding of an octetstring value

  8.7.1 The encoding of an octetstring value shall be either primitive or constructed at the option of the sender.

  > NOTE — Where it is necessary to transfer part of an octet string before the entire OCTET STRING is available, the constructed encoding is used.

  8.7.2 The primitive encoding contains zero, one or more contents octets equal in value to the octets in the data value, in the order they appear in the data value, and with the most significant bit of an octet of the data value aligned with the most significant bit of an octet of the contents octets.

  8.7.3 The contents octets for the constructed encoding shall consist of zero, one, or more encodings.

  > NOTE — Each such encoding includes identifier, length, and contents octets, and may include end-of-contents octets if it is constructed.

  8.7.3.1 To encode an octetstring value in this way, it is segmented. Each segment shall consist of a series of consecutive octets of the value. There shall be no significance placed on the segment boundaries.

  > NOTE — A segment may be of size zero, i.e. contain no octets.

  8.7.3.2 Each encoding in the contents octets shall represent a segment of the overall octetstring, the encoding arising from a recursive application of this subclause. In this recursive application, each segment is treated as if it were a octetstring value. The encodings of the segments shall appear in the contents octets in the order in which their octets appear in the overall value.

  > NOTE 1 — As a consequence of this recursion, each encoding in the contents octets may itself be primitive or constructed. However, such encodings will usually be primitive.

  > NOTE 2 — In particular, the tags in the contents octets are always universal class, number 4.

  9: Canonical encoding rules

  9.1 Length forms

  If the encoding is constructed, it shall employ the indefinite-length form. If the encoding is primitive, it shall include the fewest length octets necessary. [Contrast with 8.1.3.2 b).]

  9.2 String encoding forms

  BIT STRING, OCTET STRING,and restricted character string values shall be encoded with a primitive encoding if they would require no more than 1000 contents octets, and as a constructed encoding otherwise. The string fragments contained in the constructed encoding shall be encoded with a primitive encoding. The encoding of each fragment, except possibly the last, shall have 1000 contents octets. (Contrast with 8.21.6.)

  10: Distinguished encoding rules

  10.1 Length forms The definite form of length encoding shall be used, encoded in the minimum number of octets. [Contrast with 8.1.3.2 b).]

  10.2 String encoding forms For BIT STRING, OCTET STRING and restricted character string types, the constructed form of encoding shall not be used. (Contrast with 8.21.6.)

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ASN1OctetString(byte[] string)` Base constructor.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static ASN1OctetString` | `getInstance(ASN1TaggedObject taggedObject, boolean declaredExplicit)` return an Octet String from a tagged object.
    `static ASN1OctetString` | `getInstance(java.lang.Object obj)` return an Octet String from the given object.
    `ASN1Primitive` | `getLoadedObject()` Get the in-memory representation of the ASN.1 object.
    `byte[]` | `getOctets()` Return the content of the OCTET STRING as a byte array.
    `int` | `getOctetsLength()`
    `java.io.InputStream` | `getOctetStream()` Return the content of the OCTET STRING as an InputStream.
    `static ASN1OctetString` | `getTagged(ASN1TaggedObject taggedObject, boolean declaredExplicit)`
    `int` | `hashCode()`
    `ASN1OctetStringParser` | `parser()` Return the parser associated with this object.
    `java.lang.String` | `toString()`

    - ### Methods inherited from class org.bouncycastle.asn1.ASN1Primitive

      `encodeTo, encodeTo, equals, equals, equals, fromByteArray, fromStream, toASN1Primitive`

    - ### Methods inherited from class org.bouncycastle.asn1.ASN1Object

      `getEncoded, getEncoded, hasEncodedTagValue`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.bouncycastle.asn1.ASN1Encodable

      `toASN1Primitive`

- - ### Constructor Detail

    - #### ASN1OctetString

          public ASN1OctetString(byte[] string)

      Base constructor.
      Parameters:
        `string` - the octets making up the octet string.

  - ### Method Detail

    - #### getInstance

          public static ASN1OctetString getInstance(ASN1TaggedObject taggedObject,
                                                    boolean declaredExplicit)

      return an Octet String from a tagged object.
      Parameters:
        `taggedObject` - the tagged object holding the object we want.
        `declaredExplicit` - true if the object is meant to be explicitly tagged false otherwise.
      Throws:
        `java.lang.IllegalArgumentException` - if the tagged object cannot be converted.

    - #### getTagged

          public static ASN1OctetString getTagged(ASN1TaggedObject taggedObject,
                                                  boolean declaredExplicit)

    - #### getInstance

          public static ASN1OctetString getInstance(java.lang.Object obj)

      return an Octet String from the given object.
      Parameters:
        `obj` - the object we want converted.
      Throws:
        `java.lang.IllegalArgumentException` - if the object cannot be converted.

    - #### getOctetStream

          public java.io.InputStream getOctetStream()

      Return the content of the OCTET STRING as an InputStream.
      Specified by:
        `getOctetStream` in interface `ASN1OctetStringParser`
      Returns:
        an InputStream representing the OCTET STRING's content.

    - #### parser

          public ASN1OctetStringParser parser()

      Return the parser associated with this object.
      Returns:
        a parser based on this OCTET STRING

    - #### getOctets

          public byte[] getOctets()

      Return the content of the OCTET STRING as a byte array.
      Returns:
        the byte[] representing the OCTET STRING's content.

    - #### getOctetsLength

          public int getOctetsLength()

    - #### hashCode

          public int hashCode()

      Specified by:
        `hashCode` in class `ASN1Primitive`

    - #### getLoadedObject

          public ASN1Primitive getLoadedObject()

      Description copied from interface: `InMemoryRepresentable`
      Get the in-memory representation of the ASN.1 object.
      Specified by:
        `getLoadedObject` in interface `InMemoryRepresentable`
      Returns:
        an ASN1Primitive representing the loaded object.

    - #### toString

          public java.lang.String toString()

      Overrides:
        `toString` in class `java.lang.Object`