KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > 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.maverick.util;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24 /**
25  *
26  *
27  * @author $author$
28  */

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

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

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

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

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

98   public byte[] toByteArray() {
99     return data.toByteArray();
100   }
101 }
102
Popular Tags