1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.FilterOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 public class DEROutputStream 27 extends FilterOutputStream implements DERTags 28 { 29 public DEROutputStream( 30 OutputStream os) 31 { 32 super(os); 33 } 34 35 private void writeLength( 36 int length) 37 throws IOException 38 { 39 if (length > 127) 40 { 41 int size = 1; 42 int val = length; 43 44 while ((val >>>= 8) != 0) 45 { 46 size++; 47 } 48 49 write((byte)(size | 0x80)); 50 51 for (int i = (size - 1) * 8; i >= 0; i -= 8) 52 { 53 write((byte)(length >> i)); 54 } 55 } 56 else 57 { 58 write((byte)length); 59 } 60 } 61 62 void writeEncoded( 63 int tag, 64 byte[] bytes) 65 throws IOException 66 { 67 write(tag); 68 writeLength(bytes.length); 69 write(bytes); 70 } 71 72 protected void writeNull() 73 throws IOException 74 { 75 write(NULL); 76 write(0x00); 77 } 78 79 public void writeObject( 80 Object obj) 81 throws IOException 82 { 83 if (obj == null) 84 { 85 writeNull(); 86 } 87 else if (obj instanceof DERObject) 88 { 89 ((DERObject)obj).encode(this); 90 } 91 else if (obj instanceof DEREncodable) 92 { 93 ((DEREncodable)obj).getDERObject().encode(this); 94 } 95 else 96 { 97 throw new IOException ("object not DEREncodable"); 98 } 99 } 100 } 101 | Popular Tags |