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

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1;
  2. import java.io.FilterOutputStream;
  3. import java.io.OutputStream;
  4. import java.io.IOException;
  5. import java.io.EOFException;
  6. public class DEROutputStream
  7.     extends FilterOutputStream implements DERTags
  8. {
  9.     public DEROutputStream(
  10.         OutputStream    os)
  11.     {
  12.         super(os);
  13.     }
  14.     private void writeLength(
  15.         int length)
  16.         throws IOException
  17.     {
  18.         if (length > 127)
  19.         {
  20.             int size = 1;
  21.             int val = length;
  22.             while ((val >>>= 8) != 0)
  23.             {
  24.                 size++;
  25.             }
  26.             write((byte)(size | 0x80));
  27.             for (int i = (size - 1) * 8; i >= 0; i -= 8)
  28.             {
  29.                 write((byte)(length >> i));
  30.             }
  31.         }
  32.         else
  33.         {
  34.             write((byte)length);
  35.         }
  36.     }
  37.     void writeEncoded(
  38.         int     tag,
  39.         byte[]  bytes)
  40.         throws IOException
  41.     {
  42.         write(tag);
  43.         writeLength(bytes.length);
  44.         write(bytes);
  45.     }
  46.     protected void writeNull()
  47.         throws IOException
  48.     {
  49.         write(NULL);
  50.         write(0x00);
  51.     }
  52.     public void writeObject(
  53.         Object    obj)
  54.         throws IOException
  55.     {
  56.         if (obj == null)
  57.         {
  58.             writeNull();
  59.         }
  60.         else if (obj instanceof DERObject)
  61.         {
  62.             ((DERObject)obj).encode(this);
  63.         }
  64.         else if (obj instanceof DEREncodable)
  65.         {
  66.             ((DEREncodable)obj).getDERObject().encode(this);
  67.         }
  68.         else 
  69.         {
  70.             throw new IOException("object not DEREncodable");
  71.         }
  72.     }
  73. }