예제 #1
0
  void encode(ASN1OutputStream out) throws IOException {
    if (!empty) {
      ASN1Primitive primitive = obj.toASN1Primitive().toDERObject();

      if (explicit) {
        out.writeTag(BERTags.CONSTRUCTED | BERTags.TAGGED, tagNo);
        out.writeLength(primitive.encodedLength());
        out.writeObject(primitive);
      } else {
        //
        // need to mark constructed types...
        //
        int flags;
        if (primitive.isConstructed()) {
          flags = BERTags.CONSTRUCTED | BERTags.TAGGED;
        } else {
          flags = BERTags.TAGGED;
        }

        out.writeTag(flags, tagNo);
        out.writeImplicitObject(primitive);
      }
    } else {
      out.writeEncoded(BERTags.CONSTRUCTED | BERTags.TAGGED, tagNo, ZERO_BYTES);
    }
  }
예제 #2
0
  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;
    }
  }