deroutputstream.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 525 行 · 第 1/2 页

JAVA
525
字号
     * @param octets the octet string     */    public void putOctetString(byte[] octets) throws IOException {        write(DerValue.tag_OctetString, octets);    }    /**     * Marshals a DER "null" value on the output stream.  These are     * often used to indicate optional values which have been omitted.     */    public void putNull() throws IOException {        write(DerValue.tag_Null);        putLength(0);    }    /**     * Marshals an object identifier (OID) on the output stream.     * Corresponds to the ASN.1 "OBJECT IDENTIFIER" construct.     */    public void putOID(ObjectIdentifier oid) throws IOException {        oid.encode(this);    }    /**     * Marshals a sequence on the output stream.  This supports both     * the ASN.1 "SEQUENCE" (zero to N values) and "SEQUENCE OF"     * (one to N values) constructs.     */    public void putSequence(DerValue[] seq) throws IOException {        DerOutputStream bytes = new DerOutputStream();        int i;        for (i = 0; i < seq.length; i++)            seq[i].encode(bytes);        write(DerValue.tag_Sequence, bytes);    }    /**     * Marshals the contents of a set on the output stream without     * ordering the elements.  Ok for BER encoding, but not for DER     * encoding.     *     * For DER encoding, use orderedPutSet() or orderedPutSetOf().     */    public void putSet(DerValue[] set) throws IOException {        DerOutputStream bytes = new DerOutputStream();        int i;        for (i = 0; i < set.length; i++)            set[i].encode(bytes);        write(DerValue.tag_Set, bytes);    }    /**     * Marshals a string as a DER encoded UTF8String.     */    public void putUTF8String(String s) throws IOException {        writeString(s, DerValue.tag_UTF8String, "UTF-8");    }    /**     * Marshals a string as a DER encoded PrintableString.     */    public void putPrintableString(String s) throws IOException {        writeString(s, DerValue.tag_PrintableString, "US_ASCII");    }    /**     * Marshals a string as a DER encoded T61String.     */    public void putT61String(String s) throws IOException {        /*         * Works for characters that are defined in both ASCII and         * T61.         */        writeString(s, DerValue.tag_T61String, "ISO-8859-1");    }    /**     * Marshals a string as a DER encoded IA5String.     */    public void putIA5String(String s) throws IOException {        writeString(s, DerValue.tag_IA5String, "US_ASCII");    }    /**     * Marshals a string as a DER encoded BMPString.     */    public void putBMPString(String s) throws IOException {        writeString(s, DerValue.tag_BMPString, "UnicodeBigUnmarked");    }    /**     * Marshals a string as a DER encoded GeneralString.     */    public void putGeneralString(String s) throws IOException {        writeString(s, DerValue.tag_GeneralString, "US_ASCII");    }    /**     * Private helper routine for writing DER encoded string values.     * @param s the string to write     * @param stringTag one of the DER string tags that indicate which     * encoding should be used to write the string out.     * @param enc the name of the encoder that should be used corresponding     * to the above tag.     */    private void writeString(String s, byte stringTag, String enc)        throws IOException {        byte[] data = s.getBytes(enc);        write(stringTag);        putLength(data.length);        write(data);    }    /**     * Marshals a DER UTC time/date value.     *     * <P>YYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time     * and with seconds (even if seconds=0) as per RFC 3280.     */    public void putUTCTime(Date d) throws IOException {        putTime(d, DerValue.tag_UtcTime);    }    /**     * Marshals a DER Generalized Time/date value.     *     * <P>YYYYMMDDhhmmss{Z|+hhmm|-hhmm} ... emits only using Zulu time     * and with seconds (even if seconds=0) as per RFC 3280.     */    public void putGeneralizedTime(Date d) throws IOException {        putTime(d, DerValue.tag_GeneralizedTime);    }    /**     * Private helper routine for marshalling a DER UTC/Generalized     * time/date value. If the tag specified is not that for UTC Time     * then it defaults to Generalized Time.     * @param d the date to be marshalled     * @param tag the tag for UTC Time or Generalized Time     */    private void putTime(Date d, byte tag) throws IOException {        /*         * Format the date.         */        TimeZone tz = TimeZone.getTimeZone("GMT");        Calendar c = Calendar.getInstance(tz);        c.setTime(d);        String strYear = Integer.toString(c.get(Calendar.YEAR));        StringBuffer timeStr = new StringBuffer();        if (tag == DerValue.tag_UtcTime) {            // "yy"            int len = strYear.length();            if (len <= 2) {                timeStr.append(strYear);            } else {                timeStr.append(strYear.substring(len - 2, len));            }        } else {            // "yyyy"            tag = DerValue.tag_GeneralizedTime;            timeStr.append(strYear);        }        // adding "MMddHHmmss'Z'"        int[] n = {            Calendar.MONTH, Calendar.DAY_OF_MONTH,            Calendar.HOUR, Calendar.SECOND        };        for (int i = 0; i < n.length; i++) {            String tmp = Integer.toString(c.get(n[i]));            if (tmp.length() < 2) {                timeStr.append("0");            }            timeStr.append(tmp);        }        timeStr.append("'+0000'"); // GMT        byte[] time = timeStr.toString().getBytes("ISO-8859-1");        /*         * Write the formatted date.         */        write(tag);        putLength(time.length);        write(time);    }    /**     * Put the encoding of the length in the stream.     *     * @params len the length of the attribute.     * @exception IOException on writing errors.     */    public void putLength(int len) throws IOException {        if (len < 128) {            write((byte)len);        } else if (len < (1 << 8)) {            write((byte)0x081);            write((byte)len);        } else if (len < (1 << 16)) {            write((byte)0x082);            write((byte)(len >> 8));            write((byte)len);        } else if (len < (1 << 24)) {            write((byte)0x083);            write((byte)(len >> 16));            write((byte)(len >> 8));            write((byte)len);        } else {            write((byte)0x084);            write((byte)(len >> 24));            write((byte)(len >> 16));            write((byte)(len >> 8));            write((byte)len);        }    }    /**     * Put the tag of the attribute in the stream.     *     * @params class the tag class type, one of UNIVERSAL, CONTEXT,     *                            APPLICATION or PRIVATE     * @params form if true, the value is constructed, otherwise it is     * primitive.     * @params val the tag value     */    public void putTag(byte tagClass, boolean form, byte val) {        byte tag = (byte)(tagClass | val);        if (form) {            tag |= (byte)0x20;        }        write(tag);    }    /**     *  Write the current contents of this <code>DerOutputStream</code>     *  to an <code>OutputStream</code>.     *     *  @exception IOException on output error.     */    public void derEncode(OutputStream out) throws IOException {        out.write(toByteArray());    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?