Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.IOException;
      4 import java.util.Enumeration;
      5 
      6 /**
      7  * Note: this class is for processing DER/DL encoded sequences only.
      8  */
      9 class LazyEncodedSequence
     10     extends ASN1Sequence
     11 {
     12     private byte[] encoded;
     13 
     14     LazyEncodedSequence(
     15         byte[] encoded)
     16         throws IOException
     17     {
     18         this.encoded = encoded;
     19     }
     20 
     21     private void parse()
     22     {
     23         Enumeration en = new LazyConstructionEnumeration(encoded);
     24 
     25         while (en.hasMoreElements())
     26         {
     27             seq.addElement(en.nextElement());
     28         }
     29 
     30         encoded = null;
     31     }
     32 
     33     public synchronized ASN1Encodable getObjectAt(int index)
     34     {
     35         if (encoded != null)
     36         {
     37             parse();
     38         }
     39 
     40         return super.getObjectAt(index);
     41     }
     42 
     43     public synchronized Enumeration getObjects()
     44     {
     45         if (encoded == null)
     46         {
     47             return super.getObjects();
     48         }
     49 
     50         return new LazyConstructionEnumeration(encoded);
     51     }
     52 
     53     public synchronized int size()
     54     {
     55         if (encoded != null)
     56         {
     57             parse();
     58         }
     59 
     60         return super.size();
     61     }
     62 
     63     ASN1Primitive toDERObject()
     64     {
     65         if (encoded != null)
     66         {
     67             parse();
     68         }
     69 
     70         return super.toDERObject();
     71     }
     72 
     73     ASN1Primitive toDLObject()
     74     {
     75         if (encoded != null)
     76         {
     77             parse();
     78         }
     79 
     80         return super.toDLObject();
     81     }
     82 
     83     int encodedLength()
     84         throws IOException
     85     {
     86         if (encoded != null)
     87         {
     88             return 1 + StreamUtil.calculateBodyLength(encoded.length) + encoded.length;
     89         }
     90         else
     91         {
     92             return super.toDLObject().encodedLength();
     93         }
     94     }
     95 
     96     void encode(
     97         ASN1OutputStream out)
     98         throws IOException
     99     {
    100         if (encoded != null)
    101         {
    102             out.writeEncoded(BERTags.SEQUENCE | BERTags.CONSTRUCTED, encoded);
    103         }
    104         else
    105         {
    106             super.toDLObject().encode(out);
    107         }
    108     }
    109 }
    110