KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > ByteArrayReader


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.ByteArrayInputStream JavaDoc;
23 import java.io.EOFException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26
27 import java.math.BigInteger JavaDoc;
28
29 /**
30  *
31  * <p>Utiltiy class to read common parameter types from a byte array.</p>
32  *
33  * @author Lee David Painter
34  */

35 public class ByteArrayReader
36     extends ByteArrayInputStream JavaDoc {
37
38   private static String JavaDoc CHARSET_ENCODING = "UTF8";
39
40   public static boolean encode;
41
42   static {
43       setCharsetEncoding(CHARSET_ENCODING);
44   }
45   /**
46    * Construct a reader.
47    * @param buffer
48    * @param start
49    * @param len
50    */

51   public ByteArrayReader(byte[] buffer, int start, int len) {
52     super(buffer, start, len);
53   }
54
55   public ByteArrayReader(byte[] buffer) {
56     super(buffer, 0, buffer.length);
57   }
58
59   /**
60    * Provides access to the underlying array
61    * @return byte[]
62    */

63   public byte[] array() {
64       return this.buf;
65   }
66
67   /**
68    * Allows the default encoding to be overriden for String variables processed
69    * by the class. This currently defaults to UTF-8.
70    *
71    * @param charset
72    * @throws UnsupportedEncodingException
73    */

74   public static void setCharsetEncoding(String JavaDoc charset) {
75     try {
76
77       String JavaDoc test = "123456890";
78       test.getBytes(charset);
79       CHARSET_ENCODING = charset;
80       encode = true;
81     }
82     catch (UnsupportedEncodingException JavaDoc ex) {
83       // Reset the encoding to default
84
CHARSET_ENCODING = "";
85       encode = false;
86     }
87   }
88
89   /**
90    * Get the current encoding being used for Strings variables.
91    * @return
92    */

93   public static String JavaDoc getCharsetEncoding() {
94     return CHARSET_ENCODING;
95   }
96
97   /**
98    * Read until the buffer supplied is full.
99    * @param b
100    * @param off
101    * @param len
102    * @throws IOException
103    */

104   public void readFully(byte b[], int off, int len) throws IOException JavaDoc {
105    if (len < 0)
106        throw new IndexOutOfBoundsException JavaDoc();
107    int n = 0;
108    while (n < len) {
109        int count = read(b, off + n, len - n);
110        if (count < 0)
111         throw new EOFException JavaDoc();
112        n += count;
113    }
114   }
115   
116   /**
117    * Write a boolean value to the array.
118    * @param b
119    * @throws IOException
120    */

121   public boolean readBoolean() throws IOException JavaDoc {
122     return read()==1;
123   }
124
125   public void readFully(byte[] b) throws IOException JavaDoc {
126     readFully(b,0,b.length);
127   }
128
129   /**
130    * Read a BigInteger from the array.
131    * @return the BigInteger value.
132    * @throws IOException
133    */

134   public BigInteger JavaDoc readBigInteger() throws IOException JavaDoc {
135     int len = (int) readInt();
136     byte[] raw = new byte[len];
137     readFully(raw);
138     return new BigInteger JavaDoc(raw);
139   }
140
141   public UnsignedInteger64 readUINT64() throws IOException JavaDoc {
142     byte[] raw = new byte[8];
143     readFully(raw);
144     return new UnsignedInteger64(raw);
145   }
146
147   public UnsignedInteger32 readUINT32() throws IOException JavaDoc {
148     return new UnsignedInteger32(readInt());
149   }
150
151   /**
152    * Read an integer (4 bytes) from the array. This is returned as a long
153    * as we deal with unsigned ints so the value may be higher than the
154    * standard java int.
155    * @param data
156    * @param start
157    * @return the value represent by a long.
158    */

159   public static long readInt(byte[] data, int start) {
160     long ret = ( ( (long) (data[start] & 0xFF) << 24) & 0xFFFFFFFFL)
161         | ( (data[start + 1] & 0xFF) << 16)
162         | ( (data[start + 2] & 0xFF) << 8) | ( (data[start + 3] & 0xFF) << 0);
163
164     return ret;
165   }
166   
167   
168   public static short readShort(byte[] data, int start) {
169       short ret = (short)((data[start+1] & 0xFF) << 8 | (data[start] & 0xFF) << 0);
170
171       return ret;
172   }
173 /*public static int readInt(InputStream in) throws IOException {
174    int ch1 = in.read();
175     int ch2 = in.read();
176     int ch3 = in.read();
177     int ch4 = in.read();
178     if((ch1 | ch2 | ch3 | ch4) < 0)
179      throw new EOFException();
180     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
181      }*/

182
183   /*public static void readFully(InputStream in, byte[] buf, int off, int len) throws IOException {
184    if(len < 0)
185     throw new IndexOutOfBoundsException();
186    int count = 0;
187    int read;
188    while(count < len) {
189     read = in.read(buf, off + count, len - count);
190     if(read < 0)
191      throw new EOFException();
192     count += read;
193    }
194      }*/

195
196   /**
197    * Read a binary string from the array.
198    * @return the byte array.
199    * @throws IOException
200    */

201   public byte[] readBinaryString() throws IOException JavaDoc {
202     int len = (int) readInt();
203     byte[] buf = new byte[len];
204     readFully(buf);
205     return buf;
206   }
207
208   /**
209    * Read an integer (4 bytes) from the array. This is returned as a long
210    * as we deal with unsigned ints so the value may be higher than the
211    * standard java int.
212    * @return the integer value as a long.
213    * @throws IOException
214    */

215   public long readInt() throws IOException JavaDoc {
216     int ch1 = read();
217     int ch2 = read();
218     int ch3 = read();
219     int ch4 = read();
220     if ((ch1 | ch2 | ch3 | ch4) < 0)
221         throw new EOFException JavaDoc();
222     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)) & 0xFFFFFFFFL;
223
224   }
225
226   /**
227    * Read a String from the array.
228    * @return the String value.
229    * @throws IOException
230    */

231   public String JavaDoc readString() throws IOException JavaDoc {
232     return readString(CHARSET_ENCODING);
233   }
234
235   /**
236    * Read a String from the array converting using the given character set.
237    * @param charset
238    * @return
239    * @throws IOException
240    */

241   public String JavaDoc readString(String JavaDoc charset) throws IOException JavaDoc {
242     long len = readInt();
243
244     if(len > available())
245         throw new IOException JavaDoc("Cannot read string of length " + len
246                               + " bytes when only " + available()
247                               + " bytes are available");
248
249     byte[] raw = new byte[ (int) len];
250     readFully(raw);
251     if (encode) {
252       return new String JavaDoc(raw, charset);
253     }
254     else {
255       return new String JavaDoc(raw);
256     }
257
258   }
259
260
261
262
263   public short readShort() throws IOException JavaDoc {
264     int ch1 = read();
265     int ch2 = read();
266
267     if ( (ch1 | ch2) < 0) {
268       throw new EOFException JavaDoc();
269     }
270
271     return (short) ( (ch1 << 8) + (ch2 << 0));
272   }
273
274   /**
275    * Reads an MPINT using the first 32 bits as the length prefix
276    * @return
277    * @throws IOException
278    */

279   public BigInteger JavaDoc readMPINT32() throws IOException JavaDoc {
280     int bits = (int)readInt();
281
282     byte[] raw = new byte[ (bits + 7) / 8 + 1];
283
284     raw[0] = 0;
285     readFully(raw, 1, raw.length - 1);
286
287     return new BigInteger JavaDoc(raw);
288
289   }
290
291   /**
292    * Reads a standard SSH1 MPINT using the first 16 bits as the length prefix
293    * @return
294    * @throws IOException
295    */

296   public BigInteger JavaDoc readMPINT() throws IOException JavaDoc {
297     short bits = readShort();
298
299     byte[] raw = new byte[ (bits + 7) / 8 + 1];
300
301     raw[0] = 0;
302     readFully(raw, 1, raw.length - 1);
303
304     return new BigInteger JavaDoc(raw);
305   }
306
307   /**
308    * Get the current position within the array.
309    * @return the current position within the array
310    */

311   public int getPosition() {
312     return this.pos;
313   }
314 }
315
Popular Tags