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