Пример #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
  /**
   * Return the default BER or DER encoding for this object.
   *
   * @return BER/DER byte encoded object.
   * @throws java.io.IOException on encoding error.
   */
  public byte[] getEncoded() throws IOException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    aOut.writeObject(this);

    return bOut.toByteArray();
  }
  private byte[] getEncoded(DEREncodable obj) {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    try {
      aOut.writeObject(obj);
    } catch (IOException e) {
      throw new IllegalArgumentException("cannot encode object added to SET");
    }

    return bOut.toByteArray();
  }
Пример #4
0
  /**
   * A note on the implementation:
   *
   * <p>As DL requires the constructed, definite-length model to be used for structured types, this
   * varies slightly from the ASN.1 descriptions given. Rather than just outputting SEQUENCE, we
   * also have to specify CONSTRUCTED, and the objects length.
   */
  void encode(ASN1OutputStream out) throws IOException {
    ASN1OutputStream dOut = out.getDLSubStream();
    int length = getBodyLength();

    out.write(BERTags.SEQUENCE | BERTags.CONSTRUCTED);
    out.writeLength(length);

    for (Enumeration e = this.getObjects(); e.hasMoreElements(); ) {
      Object obj = e.nextElement();

      dOut.writeObject((ASN1Encodable) obj);
    }
  }
  public String getString() {
    StringBuffer buf = new StringBuffer("#");
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    try {
      aOut.writeObject(this);
    } catch (IOException e) {
      throw new RuntimeException("internal error encoding BitString");
    }

    byte[] string = bOut.toByteArray();

    for (int i = 0; i != string.length; i++) {
      buf.append(table[(string[i] >>> 4) & 0xf]);
      buf.append(table[string[i] & 0xf]);
    }

    return buf.toString();
  }