Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.IOException;
      4 import java.io.OutputStream;
      5 
      6 /**
      7  * Base class for generators for indefinite-length structures.
      8  */
      9 public class BERGenerator
     10     extends ASN1Generator
     11 {
     12     private boolean      _tagged = false;
     13     private boolean      _isExplicit;
     14     private int          _tagNo;
     15 
     16     protected BERGenerator(
     17         OutputStream out)
     18     {
     19         super(out);
     20     }
     21 
     22     protected BERGenerator(
     23         OutputStream out,
     24         int tagNo,
     25         boolean isExplicit)
     26     {
     27         super(out);
     28 
     29         _tagged = true;
     30         _isExplicit = isExplicit;
     31         _tagNo = tagNo;
     32     }
     33 
     34     public OutputStream getRawOutputStream()
     35     {
     36         return _out;
     37     }
     38 
     39     private void writeHdr(
     40         int tag)
     41         throws IOException
     42     {
     43         _out.write(tag);
     44         _out.write(0x80);
     45     }
     46 
     47     protected void writeBERHeader(
     48         int tag)
     49         throws IOException
     50     {
     51         if (_tagged)
     52         {
     53             int tagNum = _tagNo | BERTags.TAGGED;
     54 
     55             if (_isExplicit)
     56             {
     57                 writeHdr(tagNum | BERTags.CONSTRUCTED);
     58                 writeHdr(tag);
     59             }
     60             else
     61             {
     62                 if ((tag & BERTags.CONSTRUCTED) != 0)
     63                 {
     64                     writeHdr(tagNum | BERTags.CONSTRUCTED);
     65                 }
     66                 else
     67                 {
     68                     writeHdr(tagNum);
     69                 }
     70             }
     71         }
     72         else
     73         {
     74             writeHdr(tag);
     75         }
     76     }
     77 
     78     protected void writeBEREnd()
     79         throws IOException
     80     {
     81         _out.write(0x00);
     82         _out.write(0x00);
     83 
     84         if (_tagged && _isExplicit)  // write extra end for tag header
     85         {
     86             _out.write(0x00);
     87             _out.write(0x00);
     88         }
     89     }
     90 }
     91