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