KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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