BERTaggedObject.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:3k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1;
  2. import java.io.*;
  3. import java.util.*;
  4. /**
  5.  * BER TaggedObject - in ASN.1 nottation this is any object proceeded by
  6.  * a [n] where n is some number - these are assume to follow the construction
  7.  * rules (as with sequences).
  8.  */
  9. public class BERTaggedObject
  10.     extends DERTaggedObject
  11. {
  12.     /**
  13.      * @param tagNo the tag number for this object.
  14.      * @param obj the tagged object.
  15.      */
  16.     public BERTaggedObject(
  17.         int             tagNo,
  18.         DEREncodable    obj)
  19.     {
  20. super(tagNo, obj);
  21.     }
  22.     /**
  23.      * @param explicit true if an explicitly tagged object.
  24.      * @param tagNo the tag number for this object.
  25.      * @param obj the tagged object.
  26.      */
  27.     public BERTaggedObject(
  28.         boolean         explicit,
  29.         int             tagNo,
  30.         DEREncodable    obj)
  31.     {
  32. super(explicit, tagNo, obj);
  33.     }
  34.     /**
  35.      * create an implicitly tagged object that contains a zero
  36.      * length sequence.
  37.      */
  38.     public BERTaggedObject(
  39.         int             tagNo)
  40.     {
  41.         super(false, tagNo, new BERConstructedSequence());
  42.     }
  43.     void encode(
  44.         DEROutputStream  out)
  45.         throws IOException
  46.     {
  47.         if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
  48.         {
  49.             out.write(CONSTRUCTED | TAGGED | tagNo);
  50.             out.write(0x80);
  51.             if (!empty)
  52.             {
  53.                 if (!explicit)
  54.                 {
  55.                     if (obj instanceof ASN1OctetString)
  56.                     {
  57.                         Enumeration  e;
  58.                         if (obj instanceof BERConstructedOctetString)
  59.                         {
  60.                             e = ((BERConstructedOctetString)obj).getObjects();
  61.                         }
  62.                         else
  63.                         {
  64.                             ASN1OctetString             octs = (ASN1OctetString)obj;
  65.                             BERConstructedOctetString   berO = new BERConstructedOctetString(octs.getOctets());
  66.                             e = berO.getObjects();
  67.                         }
  68.                         while (e.hasMoreElements())
  69.                         {
  70.                             out.writeObject(e.nextElement());
  71.                         }
  72.                     }
  73.                     else if (obj instanceof ASN1Sequence)
  74.                     {
  75.                         Enumeration  e = ((ASN1Sequence)obj).getObjects();
  76.                         while (e.hasMoreElements())
  77.                         {
  78.                             out.writeObject(e.nextElement());
  79.                         }
  80.                     }
  81.                     else if (obj instanceof ASN1Set)
  82.                     {
  83.                         Enumeration  e = ((ASN1Set)obj).getObjects();
  84.                         while (e.hasMoreElements())
  85.                         {
  86.                             out.writeObject(e.nextElement());
  87.                         }
  88.                     }
  89.                     else
  90.                     {
  91.                         throw new RuntimeException("not implemented: " + obj.getClass().getName());
  92.                     }
  93.                 }
  94.                 else
  95.                 {
  96.                     out.writeObject(obj);
  97.                 }
  98.             }
  99.             out.write(0x00);
  100.             out.write(0x00);
  101.         }
  102.         else
  103.         {
  104.             super.encode(out);
  105.         }
  106.     }
  107. }