KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > SimpleASNWriter


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24
25 /**
26  *
27  *
28  * @author $author$
29  */

30 public class SimpleASNWriter {
31     private ByteArrayOutputStream JavaDoc data;
32
33     /**
34      * Creates a new SimpleASNWriter object.
35      */

36     public SimpleASNWriter() {
37         this.data = new ByteArrayOutputStream JavaDoc();
38     }
39
40     /**
41      *
42      *
43      * @param b
44      */

45     public void writeByte(int b) {
46         data.write(b);
47     }
48
49     /**
50      *
51      *
52      * @param b
53      */

54     public void writeData(byte[] b) {
55         writeLength(b.length);
56         this.data.write(b, 0, b.length);
57     }
58
59     /**
60      *
61      *
62      * @param length
63      */

64     public void writeLength(int length) {
65         if (length < 0x80) {
66             data.write(length);
67         } else {
68             if (length < 0x100) {
69                 data.write(0x81);
70                 data.write(length);
71             } else if (length < 0x10000) {
72                 data.write(0x82);
73                 data.write(length >>> 8);
74                 data.write(length);
75             } else if (length < 0x1000000) {
76                 data.write(0x83);
77                 data.write(length >>> 16);
78                 data.write(length >>> 8);
79                 data.write(length);
80             } else {
81                 data.write(0x84);
82                 data.write(length >>> 24);
83                 data.write(length >>> 16);
84                 data.write(length >>> 8);
85                 data.write(length);
86             }
87         }
88     }
89
90     /**
91      *
92      *
93      * @return
94      */

95     public byte[] toByteArray() {
96         return data.toByteArray();
97     }
98 }
99
Popular Tags