KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23
24
25 /**
26  *
27  *
28  * @author $author$
29  */

30 public class SimpleASNReader {
31     private byte[] data;
32     private int offset;
33
34     /**
35      * Creates a new SimpleASNReader object.
36      *
37      * @param data
38      */

39     public SimpleASNReader(byte[] data) {
40         this.data = data;
41         this.offset = 0;
42     }
43
44     /**
45      *
46      *
47      * @param b
48      *
49      * @throws IOException
50      */

51     public void assertByte(int b) throws IOException JavaDoc {
52         int x = getByte();
53
54         if (x != b) {
55             throw new IOException JavaDoc("Assertion failed, next byte value is " +
56                 Integer.toHexString(x) + " instead of asserted " +
57                 Integer.toHexString(b));
58         }
59     }
60
61     /**
62      *
63      *
64      * @return
65      */

66     public int getByte() {
67         return data[offset++] & 0xff;
68     }
69
70     /**
71      *
72      *
73      * @return
74      */

75     public byte[] getData() {
76         int length = getLength();
77
78         return getData(length);
79     }
80
81     /**
82      *
83      *
84      * @return
85      */

86     public int getLength() {
87         int b = data[offset++] & 0xff;
88
89         if ((b & 0x80) != 0) {
90             int length = 0;
91
92             for (int bytes = b & 0x7f; bytes > 0; bytes--) {
93                 length <<= 8;
94                 length |= (data[offset++] & 0xff);
95             }
96
97             return length;
98         }
99
100         return b;
101     }
102
103     private byte[] getData(int length) {
104         byte[] result = new byte[length];
105         System.arraycopy(data, offset, result, 0, length);
106         offset += length;
107
108         return result;
109     }
110
111     /**
112      *
113      *
114      * @return
115      */

116     public boolean hasMoreData() {
117         return offset < data.length;
118     }
119 }
120
Popular Tags