KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
24
25 import java.math.BigInteger JavaDoc;
26
27 /**
28  *
29  * <p>Utility class to write common parameter types to a byte array.</p>
30  * @author Lee David Painter
31  */

32 public class ByteArrayWriter
33     extends ByteArrayOutputStream JavaDoc {
34
35   /**
36    * Contruct an empty writer.
37    */

38   public ByteArrayWriter() {
39
40   }
41
42   /**
43    * Construct a writer with an array size of the length supplied.
44    * @param length
45    */

46   public ByteArrayWriter(int length) {
47     super(length);
48   }
49
50   /**
51    * Get the underlying byte array
52    * @return the underlying byte array.
53    */

54   public byte[] array() {
55     return buf;
56   }
57
58   /**
59    * Move the position of the next byte to be written.
60    * @param numBytes
61    */

62   public void move(int numBytes) {
63     count += numBytes;
64   }
65
66   /**
67    * Write a BigInteger to the array.
68    * @param bi
69    * @throws IOException
70    */

71   public void writeBigInteger(BigInteger JavaDoc bi) throws IOException JavaDoc {
72     byte[] raw = bi.toByteArray();
73
74     writeInt(raw.length);
75     write(raw);
76   }
77   
78   /**
79    * Write a boolean value to the array.
80    * @param b
81    * @throws IOException
82    */

83   public void writeBoolean(boolean b) {
84     write(b ? 1 : 0);
85   }
86
87   /**
88    * Write a binary string to the array.
89    * @param data
90    * @throws IOException
91    */

92   public void writeBinaryString(byte[] data) throws IOException JavaDoc {
93       if(data==null)
94           writeInt(0);
95       else
96           writeBinaryString(data, 0, data.length);
97   }
98
99   /**
100    * Write a binary string to the array.
101    * @param data
102    * @param offset
103    * @param len
104    * @throws IOException
105    */

106   public void writeBinaryString(byte[] data, int offset, int len) throws
107       IOException JavaDoc {
108     if(data==null)
109         writeInt(0);
110     else {
111         writeInt(len);
112         write(data, offset, len);
113     }
114   }
115
116   public void writeMPINT(BigInteger JavaDoc b) {
117     short bytes = (short) ( (b.bitLength() + 7) / 8);
118     byte[] raw = b.toByteArray();
119     writeShort( (short) b.bitLength());
120     if (raw[0] == 0) {
121       write(raw, 1, bytes);
122     }
123     else {
124       write(raw, 0, bytes);
125     }
126   }
127
128   public void writeShort(short s) {
129     write( (s >>> 8) & 0xFF);
130     write( (s >>> 0) & 0xFF);
131   }
132
133   /**
134    * Write an integer to the array
135    * @param i
136    * @throws IOException
137    */

138   public void writeInt(long i) throws IOException JavaDoc {
139     byte[] raw = new byte[4];
140
141     raw[0] = (byte) (i >> 24);
142     raw[1] = (byte) (i >> 16);
143     raw[2] = (byte) (i >> 8);
144     raw[3] = (byte) (i);
145
146     write(raw);
147   }
148
149   /**
150    * Write an integer to the array.
151    * @param i
152    * @throws IOException
153    */

154   public void writeInt(int i) throws IOException JavaDoc {
155     byte[] raw = new byte[4];
156
157     raw[0] = (byte) (i >> 24);
158     raw[1] = (byte) (i >> 16);
159     raw[2] = (byte) (i >> 8);
160     raw[3] = (byte) (i);
161
162     write(raw);
163   }
164
165   /**
166    * Encode an integer into a 4 byte array.
167    * @param i
168    * @return a byte[4] containing the encoded integer.
169    */

170   public static byte[] encodeInt(int i) {
171     byte[] raw = new byte[4];
172     raw[0] = (byte) (i >> 24);
173     raw[1] = (byte) (i >> 16);
174     raw[2] = (byte) (i >> 8);
175     raw[3] = (byte) (i);
176     return raw;
177   }
178
179   public static void encodeInt(byte[] buf, int off, int i) {
180     buf[off++] = (byte) (i >> 24);
181     buf[off++] = (byte) (i >> 16);
182     buf[off++] = (byte) (i >> 8);
183     buf[off] = (byte) (i);
184   }
185
186   public void writeUINT32(UnsignedInteger32 value) throws IOException JavaDoc {
187     writeInt(value.longValue());
188   }
189
190   public void writeUINT64(UnsignedInteger64 value) throws IOException JavaDoc {
191     byte[] raw = new byte[8];
192     byte[] bi = value.bigIntValue().toByteArray();
193     System.arraycopy(bi, 0, raw, raw.length - bi.length, bi.length);
194     // Pad the raw data
195
write(raw);
196   }
197
198   public void writeUINT64(long value) throws IOException JavaDoc {
199     writeUINT64(new UnsignedInteger64(value));
200   }
201
202   /*public static void writeIntToArray(byte[] array, int pos, int value) throws
203       IOException {
204     if ( (array.length - pos) < 4) {
205       throw new IOException(
206           "Not enough data in array to write integer at position "
207           + String.valueOf(pos));
208     }
209     array[pos] = (byte) (value >> 24);
210     array[pos + 1] = (byte) (value >> 16);
211     array[pos + 2] = (byte) (value >> 8);
212     array[pos + 3] = (byte) (value);
213    }*/

214
215   /**
216    * Write a string to the byte array.
217    * @param str
218    * @throws IOException
219    */

220     public void writeString(String JavaDoc str) throws IOException JavaDoc {
221       writeString(str, ByteArrayReader.getCharsetEncoding());
222     }
223
224     /**
225      * Write a String to the byte array converting the bytes using the
226      * given character set.
227      * @param str
228      * @param charset
229      * @throws IOException
230      */

231   public void writeString(String JavaDoc str, String JavaDoc charset) throws IOException JavaDoc {
232
233     if (str == null) {
234       writeInt(0);
235     }
236     else {
237       byte[] tmp;
238
239       if(ByteArrayReader.encode)
240         tmp = str.getBytes(charset);
241       else
242         tmp = str.getBytes();
243
244       writeInt(tmp.length);
245       write(tmp);
246     }
247   }
248
249 }
250
Popular Tags