int encodedLength() throws IOException {
    if (!empty) {
      ASN1Primitive primitive = obj.toASN1Primitive().toDERObject();
      int length = primitive.encodedLength();

      if (explicit) {
        return StreamUtil.calculateTagLength(tagNo)
            + StreamUtil.calculateBodyLength(length)
            + length;
      } else {
        // header length already in calculation
        length = length - 1;

        return StreamUtil.calculateTagLength(tagNo) + length;
      }
    } else {
      return StreamUtil.calculateTagLength(tagNo) + 1;
    }
  }