KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > util > event > generator > ByteUtility


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.util.event.generator;
57
58 /**
59  * Byte handling utilities
60  *
61  * 5 April 1999 - functions to append standard types to byte arrays
62  * functions to produce standard types from byte arrays
63  *
64  * @author Richard F. Boehme
65  *
66  */

67 public class ByteUtility
68 {
69   public static byte[] addBytes(byte[] array,byte[] value)
70   {
71     if( null != array )
72     {
73       byte newarray[] = new byte[array.length + value.length];
74       System.arraycopy(array,0,newarray,0,array.length);
75       System.arraycopy(value,0,newarray,array.length,value.length);
76       array = newarray;
77     }
78     else
79     {
80       array = value;
81     }
82     return array;
83   }
84   public static byte[] addBytes(byte[] array, byte value)
85   {
86     if( null != array )
87     {
88       byte newarray[] = new byte[array.length + 1];
89       System.arraycopy(array,0,newarray,0,array.length);
90       newarray[newarray.length-1] = value;
91       array = newarray;
92     }
93     else
94     {
95       array = new byte[1];
96       array[0] = value;
97     }
98     return array;
99   }
100   public static byte[] addBytes(byte[] array, int value)
101   {
102     if( null != array )
103     {
104       byte newarray[] = new byte[array.length + 3];
105       System.arraycopy(array,0,newarray,0,array.length);
106       newarray[newarray.length-3] = (byte) (( value >> 16 ) & 0xFF);
107       newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF);
108       newarray[newarray.length-1] = (byte) ( value & 0xFF);
109       array = newarray;
110     }
111     else
112     {
113       array = new byte[3];
114       array[0] = (byte) (( value >> 16 ) & 0xFF);
115       array[1] = (byte) (( value >> 8 ) & 0xFF);
116       array[2] = (byte) ( value & 0xFF);
117     }
118     return array;
119   }
120   public static byte[] addBytes(byte[] array, long value)
121   {
122     if( null != array )
123     {
124       byte newarray[] = new byte[array.length + 4];
125       System.arraycopy(array,0,newarray,0,array.length);
126       newarray[newarray.length-4] = (byte) (( value >> 24 ) & 0xFF);
127       newarray[newarray.length-3] = (byte) (( value >> 16 ) & 0xFF);
128       newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF);
129       newarray[newarray.length-1] = (byte) ( value & 0xFF);
130       array = newarray;
131     }
132     else
133     {
134       array = new byte[4];
135       array[0] = (byte) (( value >> 24 ) & 0xFF);
136       array[1] = (byte) (( value >> 16 ) & 0xFF);
137       array[2] = (byte) (( value >> 8 ) & 0xFF);
138       array[3] = (byte) (value & 0xFF);
139     }
140     return array;
141   }
142   public static byte[] addBytes(byte[] array,String JavaDoc value)
143   {
144     if( null != value )
145     {
146       if( null != array)
147       {
148         byte newarray[] = new byte[array.length + value.length()];
149         System.arraycopy(array,0,newarray,0,array.length);
150         System.arraycopy(value.getBytes(),0,newarray,array.length,value.length());
151         array = newarray;
152       }
153       else
154       {
155         array = value.getBytes();
156       }
157     }
158     return array;
159   }
160   public static byte[] addBytes(byte[] array, short value)
161   {
162     if( null != array)
163     {
164       byte newarray[] = new byte[array.length + 2];
165       System.arraycopy(array,0,newarray,0,array.length);
166       newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF);
167       newarray[newarray.length-1] = (byte) ( value & 0xFF);
168       array = newarray;
169     }
170     else
171     {
172       array = new byte[2];
173       array[0] = (byte) (( value >> 8 ) & 0xFF);
174       array[1] = (byte) ( value & 0xFF);
175     }
176     return array;
177   }
178   public static double byteArrayToDouble(byte high[], byte low[])
179   {
180     double temp = 0;
181     // high bytes
182
temp += (((long)high[0]) & 0xFF) << 56;
183     temp += (((long)high[1]) & 0xFF) << 48;
184     temp += (((long)high[2]) & 0xFF) << 40;
185     temp += (((long)high[3]) & 0xFF) << 32;
186     // low bytes
187
temp += (((long)low[0]) & 0xFF) << 24;
188     temp += (((long)low[1]) & 0xFF) << 16;
189     temp += (((long)low[2]) & 0xFF) << 8;
190     temp += (((long)low[3]) & 0xFF);
191     return temp;
192   }
193   public static double byteArrayToDounle(byte value[])
194   {
195     byte high[] = new byte[4];
196     byte low[] = new byte[4];
197     high[0] = value[0];
198     high[1] = value[1];
199     high[2] = value[2];
200     high[3] = value[3];
201     low[0] = value[4];
202     low[1] = value[5];
203     low[2] = value[6];
204     low[3] = value[7];
205     return byteArrayToDouble(high,low);
206   }
207   public static float byteArrayToFloat(byte value[])
208   {
209     float temp = 0;
210     temp += (((int)value[0]) & 0xFF) << 24;
211     temp += (((int)value[1]) & 0xFF) << 16;
212     temp += (((int)value[2]) & 0xFF) << 8;
213     temp += (((int)value[3]) & 0xFF);
214     return temp;
215   }
216   public static int byteArrayToInt(byte value[])
217   {
218     int temp = 0;
219     temp += (((int)value[0]) & 0xFF) << 24;
220     temp += (((int)value[1]) & 0xFF) << 16;
221     temp += (((int)value[2]) & 0xFF) << 8;
222     temp += (((int)value[3]) & 0xFF);
223     return temp;
224   }
225   public static long byteArrayToLong(byte value[])
226   {
227     byte high[] = new byte[4];
228     byte low[] = new byte[4];
229     high[0] = value[0];
230     high[1] = value[1];
231     high[2] = value[2];
232     high[3] = value[3];
233     low[0] = value[4];
234     low[1] = value[5];
235     low[2] = value[6];
236     low[3] = value[7];
237     return byteArrayToLong(high,low);
238   }
239   public static long byteArrayToLong(byte high[], byte low[])
240   {
241     long temp = 0;
242     // high bytes
243
temp += (((long)high[0]) & 0xFF) << 56;
244     temp += (((long)high[1]) & 0xFF) << 48;
245     temp += (((long)high[2]) & 0xFF) << 40;
246     temp += (((long)high[3]) & 0xFF) << 32;
247     // low bytes
248
temp += (((long)low[0]) & 0xFF) << 24;
249     temp += (((long)low[1]) & 0xFF) << 16;
250     temp += (((long)low[2]) & 0xFF) << 8;
251     temp += (((long)low[3]) & 0xFF);
252     return temp;
253   }
254   // make the following loops with check on array length *****************
255
public static short byteArrayToShort(byte value[])
256   {
257     short temp = 0;
258     temp += (((int)value[0]) & 0xFF) << 8;
259     temp += (((int)value[1]) & 0xFF);
260     return temp;
261   }
262   public static String JavaDoc byteToHexString(byte value)
263   {
264     String JavaDoc temp = null;
265
266     switch( (value & 0xF0) >> 4 )
267     {
268       case 0:
269         temp = "0";
270         break;
271       case 1:
272         temp = "1";
273         break;
274       case 2:
275         temp = "2";
276         break;
277       case 3:
278         temp = "3";
279         break;
280       case 4:
281         temp = "4";
282         break;
283       case 5:
284         temp = "5";
285         break;
286       case 6:
287         temp = "6";
288         break;
289       case 7:
290         temp = "7";
291         break;
292       case 8:
293         temp = "8";
294         break;
295       case 9:
296         temp = "9";
297         break;
298       case 10:
299         temp = "A";
300         break;
301       case 11:
302         temp = "B";
303         break;
304       case 12:
305         temp = "C";
306         break;
307       case 13:
308         temp = "D";
309         break;
310       case 14:
311         temp = "E";
312         break;
313       case 15:
314         temp = "F";
315         break;
316     }
317     switch( (value & 0x0F) )
318     {
319       case 0:
320         temp += "0";
321         break;
322       case 1:
323         temp += "1";
324         break;
325       case 2:
326         temp += "2";
327         break;
328       case 3:
329         temp += "3";
330         break;
331       case 4:
332         temp += "4";
333         break;
334       case 5:
335         temp += "5";
336         break;
337       case 6:
338         temp += "6";
339         break;
340       case 7:
341         temp += "7";
342         break;
343       case 8:
344         temp += "8";
345         break;
346       case 9:
347         temp += "9";
348         break;
349       case 10:
350         temp += "A";
351         break;
352       case 11:
353         temp += "B";
354         break;
355       case 12:
356         temp += "C";
357         break;
358       case 13:
359         temp += "D";
360         break;
361       case 14:
362         temp += "E";
363         break;
364       case 15:
365         temp += "F";
366         break;
367     }
368     return temp;
369   }
370 }
371
Popular Tags