KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > renderings > RenderingsUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.memory.renderings;
12
13 import java.math.BigInteger JavaDoc;
14
15
16 /**
17  * Util functions for data conversions
18  */

19 public class RenderingsUtil {
20     
21     public static final int LITTLE_ENDIAN = 0;
22     public static final int BIG_ENDIAN = 1;
23     public static final int ENDIANESS_UNKNOWN = 2;
24
25     /**
26      * Pad byte array with zero's with the byte array's length
27      * is shorter that what's expected the conversion functions.
28      * @param array
29      * @param size
30      * @param endianess
31      * @return an array of bytes
32      */

33     protected static byte[] fillArray(byte[] array, int size, int endianess)
34     {
35         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
36         {
37             byte[] temp = new byte[size];
38             
39             for (int i=0; i<array.length; i++)
40             {
41                 temp[i] = array[i];
42             }
43             
44             // fill up the rest of the array
45
for (int i=array.length; i<size; i++)
46             {
47                 temp[i] = 0;
48             }
49             
50             array = temp;
51             return array;
52         }
53         byte[] temp = new byte[size];
54         
55         for (int i=0; i<size - array.length; i++)
56         {
57             temp[i] = 0;
58         }
59         
60         int j=0;
61         // fill up the rest of the array
62
for (int i=size - array.length; i<size; i++)
63         {
64             temp[i] = array[j];
65             j++;
66         }
67         
68         array = temp;
69         return array;
70     }
71     
72     static public BigInteger JavaDoc convertByteArrayToUnsignedLong(byte[] array, int endianess)
73     {
74         if (array.length < 8)
75         {
76             array = fillArray(array, 8, endianess);
77         }
78         
79         BigInteger JavaDoc value = new BigInteger JavaDoc("0"); //$NON-NLS-1$
80
if (endianess == RenderingsUtil.LITTLE_ENDIAN)
81         {
82             for (int i=0; i< 8; i++)
83             {
84                 byte[] temp = new byte[1];
85                 temp[0] = array[i];
86                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
87                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
88
b = b.shiftLeft(i*8);
89                 value = value.or(b);
90             }
91         }
92         else
93         {
94             for (int i=0; i< 8; i++)
95             {
96                 byte[] temp = new byte[1];
97                 temp[0] = array[i];
98                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
99                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
100
b = b.shiftLeft((7-i)*8);
101                 value = value.or(b);
102             }
103         }
104         return value;
105     }
106     
107     /**
108      * Convert byte array to long.
109      * @param array
110      * @param endianess
111      * @return result of the conversion in long
112      */

113     static public long convertByteArrayToLong(byte[] array, int endianess)
114     {
115         if (array.length < 8)
116         {
117             array = fillArray(array, 8, endianess);
118         }
119         
120         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
121         {
122             long value = 0;
123             for (int i = 0; i < 8; i++) {
124                 long b = array[i];
125                 b &= 0xff;
126                 value |= (b << (i * 8));
127             }
128             return value;
129         }
130         long value = 0;
131         for (int i=0; i< 8; i++)
132         {
133             long b = array[i];
134             b &= 0xff;
135             value |= (b<<((7-i)*8));
136         }
137         
138         return value;
139     }
140     
141     static public BigInteger JavaDoc convertByteArrayToSignedBigInt(byte[] array, int endianess)
142     {
143         if (array.length < 16)
144         {
145             array = fillArray(array, 16, endianess);
146         }
147         
148         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
149         {
150             // reverse bytes
151
byte[] holder = new byte[16];
152             int j=15;
153             for (int i=0; i<16; i++, j--)
154             {
155                 holder[i] = array[j];
156             }
157             
158             // create BigInteger
159
BigInteger JavaDoc value = new BigInteger JavaDoc(holder);
160             return value;
161         }
162         BigInteger JavaDoc value = new BigInteger JavaDoc(array);
163         return value;
164     }
165     
166     static public BigInteger JavaDoc convertByteArrayToSignedBigInt(byte[] array, int endianess, int arraySize)
167     {
168         if (array.length < arraySize)
169         {
170             array = fillArray(array, arraySize, endianess);
171         }
172         
173         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
174         {
175             // reverse bytes
176
byte[] holder = new byte[arraySize];
177             int j=arraySize-1;
178             for (int i=0; i<arraySize; i++, j--)
179             {
180                 holder[i] = array[j];
181             }
182             
183             // create BigInteger
184
BigInteger JavaDoc value = new BigInteger JavaDoc(holder);
185             return value;
186         }
187         BigInteger JavaDoc value = new BigInteger JavaDoc(array);
188         return value;
189     }
190     
191     static public BigInteger JavaDoc convertByteArrayToUnsignedBigInt(byte[] array, int endianess)
192     {
193         if (array.length < 16)
194         {
195             array = fillArray(array, 16, endianess);
196         }
197         
198         BigInteger JavaDoc value = new BigInteger JavaDoc("0"); //$NON-NLS-1$
199
if (endianess == RenderingsUtil.LITTLE_ENDIAN)
200         {
201             for (int i=0; i< 16; i++)
202             {
203                 byte[] temp = new byte[1];
204                 temp[0] = array[i];
205                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
206                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
207
b = b.shiftLeft(i*8);
208                 value = value.or(b);
209             }
210         }
211         else
212         {
213             for (int i=0; i< 16; i++)
214             {
215                 byte[] temp = new byte[1];
216                 temp[0] = array[i];
217                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
218                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
219
b = b.shiftLeft((15-i)*8);
220                 value = value.or(b);
221             }
222         }
223         return value;
224     }
225     
226     static public BigInteger JavaDoc convertByteArrayToUnsignedBigInt(byte[] array, int endianess, int arraySize)
227     {
228         if (array.length < arraySize)
229         {
230             array = fillArray(array, arraySize, endianess);
231         }
232         
233         BigInteger JavaDoc value = new BigInteger JavaDoc("0"); //$NON-NLS-1$
234
if (endianess == RenderingsUtil.LITTLE_ENDIAN)
235         {
236             for (int i=0; i< arraySize; i++)
237             {
238                 byte[] temp = new byte[1];
239                 temp[0] = array[i];
240                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
241                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
242
b = b.shiftLeft(i*8);
243                 value = value.or(b);
244             }
245         }
246         else
247         {
248             for (int i=0; i< arraySize; i++)
249             {
250                 byte[] temp = new byte[1];
251                 temp[0] = array[i];
252                 BigInteger JavaDoc b = new BigInteger JavaDoc(temp);
253                 b = b.and(new BigInteger JavaDoc("ff", 16)); //$NON-NLS-1$
254
b = b.shiftLeft((arraySize-1-i)*8);
255                 value = value.or(b);
256             }
257         }
258         return value;
259     }
260     
261     /**
262      * Convert byte array to integer.
263      * @param array
264      * @param endianess
265      * @return result of the conversion in int
266      */

267     static public int convertByteArrayToInt(byte[] array, int endianess)
268     {
269         if (array.length < 4)
270         {
271             array = fillArray(array, 4, endianess);
272         }
273         
274         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
275         {
276             int value = 0;
277             for (int i = 0; i < 4; i++) {
278                 int b = array[i];
279                 b &= 0xff;
280                 value |= (b << (i * 8));
281             }
282             return value;
283         }
284         int value = 0;
285         for (int i=0; i< 4; i++)
286         {
287             int b = array[i];
288             b &= 0xff;
289             value |= (b<<((3-i)*8));
290         }
291         
292         return value;
293     }
294     
295     /**
296      * Convert byte array to short.
297      * @param array
298      * @param endianess
299      * @return result of teh conversion in short
300      */

301     static public short convertByteArrayToShort(byte[] array, int endianess)
302     {
303         if (array.length < 2)
304         {
305             array = fillArray(array, 2, endianess);
306         }
307         
308         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
309         {
310             short value = 0;
311             for (int i = 0; i < 2; i++) {
312                 short b = array[i];
313                 b &= 0xff;
314                 value |= (b << (i * 8));
315             }
316             return value;
317         }
318         short value = 0;
319         for (int i=0; i< 2; i++)
320         {
321             short b = array[i];
322             b &= 0xff;
323             value |= (b<<((1-i)*8));
324         }
325         return value;
326     }
327     
328     /**
329      * Convert big integer to byte array.
330      * @param i
331      * @param endianess
332      * @return result of the conversion in raw byte array
333      */

334     static public byte[] convertBigIntegerToByteArray(BigInteger JavaDoc i, int endianess)
335     {
336         byte buf[]=new byte[16];
337
338         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
339         {
340             for (int j=0; j<16; j++)
341             {
342                 BigInteger JavaDoc x = i.shiftRight(j*8);
343                 buf[j] = x.byteValue();
344             }
345             return buf;
346         }
347         for (int j=15; j>=0; j--)
348         {
349             BigInteger JavaDoc x = i.shiftRight((15-j)*8);
350             buf[j] = x.byteValue();
351         }
352         return buf;
353     }
354     
355     static public byte[] convertSignedBigIntToByteArray(BigInteger JavaDoc i, int endianess, int arraySize)
356     {
357         byte buf[]=new byte[arraySize];
358
359         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
360         {
361             for (int j=0; j<arraySize; j++)
362             {
363                 BigInteger JavaDoc x = i.shiftRight(j*8);
364                 buf[j] = x.byteValue();
365             }
366             return buf;
367         }
368         for (int j=arraySize-1; j>=0; j--)
369         {
370             BigInteger JavaDoc x = i.shiftRight((arraySize-1-j)*8);
371             buf[j] = x.byteValue();
372         }
373         return buf;
374     }
375     
376     /**
377      * Convert big integer to byte array.
378      * @param i
379      * @param endianess
380      * @return result of the conversion in raw byte array
381      */

382     static public byte[] convertUnsignedBigIntegerToByteArray(BigInteger JavaDoc i, int endianess)
383     {
384         byte buf[]=new byte[32];
385
386         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
387         {
388             for (int j=0; j<32; j++)
389             {
390                 BigInteger JavaDoc x = i.shiftRight(j*8);
391                 buf[j] = x.byteValue();
392             }
393             return buf;
394         }
395         for (int j=31; j>=0; j--)
396         {
397             BigInteger JavaDoc x = i.shiftRight((31-j)*8);
398             buf[j] = x.byteValue();
399         }
400         return buf;
401     }
402     
403     static public byte[] convertUnsignedBigIntToByteArray(BigInteger JavaDoc i, int endianess, int arraySize)
404     {
405         byte buf[]=new byte[arraySize*2];
406
407         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
408         {
409             for (int j=0; j<arraySize*2; j++)
410             {
411                 BigInteger JavaDoc x = i.shiftRight(j*8);
412                 buf[j] = x.byteValue();
413             }
414             return buf;
415         }
416         for (int j=(arraySize*2)-1; j>=0; j--)
417         {
418             BigInteger JavaDoc x = i.shiftRight(((arraySize*2)-1-j)*8);
419             buf[j] = x.byteValue();
420         }
421         return buf;
422     }
423     
424     /**
425      * Convert long to byte array.
426      * @param i
427      * @param endianess
428      * @return result of the conversion in raw byte array
429      */

430     static public byte[] convertLongToByteArray(long i, int endianess)
431     {
432         byte buf[]=new byte[8];
433
434         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
435         {
436             for (int j=0; j<8; j++)
437             {
438                 buf[j] = new Long JavaDoc(i>>j*8).byteValue();
439             }
440             return buf;
441         }
442         for (int j=7; j>=0; j--)
443         {
444             buf[j] = new Long JavaDoc(i>>(7-j)*8).byteValue();
445         }
446         return buf;
447     }
448     
449     /**
450      * Convert integer to byte array.
451      * @param i
452      * @param endianess
453      * @return result of the conversion in raw byte array
454      */

455     static public byte[] convertIntToByteArray(int i, int endianess)
456     {
457         byte buf[]=new byte[4];
458
459         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
460         {
461             for (int j=0; j<4; j++)
462             {
463                 buf[j] = new Integer JavaDoc(i>>j*8).byteValue();
464             }
465             return buf;
466         }
467         for (int j=3; j>=0; j--)
468         {
469             buf[j] = new Integer JavaDoc(i>>(3-j)*8).byteValue();
470         }
471         return buf;
472     }
473     
474     /**
475      * Convert short to byte array.
476      * @param i
477      * @param endianess
478      * @return result of the conversion in raw byte array
479      */

480     static public byte[] convertShortToByteArray(short i, int endianess)
481     {
482         byte buf[]=new byte[2];
483
484         if (endianess == RenderingsUtil.LITTLE_ENDIAN)
485         {
486             for (short j=0; j<2; j++)
487             {
488                 buf[j] = new Integer JavaDoc(i>>j*8).byteValue();
489             }
490             return buf;
491         }
492         for (short j=1; j>=0; j--)
493         {
494             buf[j] = new Integer JavaDoc(i>>(1-j)*8).byteValue();
495         }
496         return buf;
497     }
498
499     /**
500      * byte array to Hex string helper
501      * replaces the Integer.toHexString() which can't convert byte values properly
502      * (always pads with FFFFFF)
503      */

504     static public String JavaDoc convertByteArrayToHexString(byte[] byteArray)
505     {
506         StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc();
507         char charArray[];
508         
509         for (int i=0; i<byteArray.length;i ++)
510         {
511             charArray = RenderingsUtil.convertByteToCharArray(byteArray[i]);
512             strBuffer.append(charArray);
513         }
514         
515         return strBuffer.toString();
516     }
517
518     static public char[] convertByteToCharArray(byte aByte)
519     {
520         char charArray[] = new char[2];
521         int val = aByte;
522         if (val<0) val += 256;
523         charArray[0] = Character.forDigit(val/16, 16);
524         charArray[1] = Character.forDigit(val%16, 16);
525         
526         return charArray;
527     }
528
529     /**
530      * Convert raw memory data to byte array
531      * @param str
532      * @param numBytes
533      * @param numCharsPerByte - number of characters per byte of data
534      * @return an array of byte, converted from a hex string
535      * @throws NumberFormatException
536      */

537     public static byte[] convertHexStringToByteArray(String JavaDoc str, int numBytes, int numCharsPerByte) throws NumberFormatException JavaDoc
538     {
539         if (str.length() == 0)
540             return null;
541         
542         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str);
543         
544         // pad string with zeros
545
int requiredPadding = numBytes * numCharsPerByte - str.length();
546         while (requiredPadding > 0) {
547             buf.insert(0, "0"); //$NON-NLS-1$
548
requiredPadding--;
549         }
550         
551         byte[] bytes = new byte[numBytes];
552         str = buf.toString();
553     
554         // set data in memory
555
for (int i=0; i<bytes.length; i++)
556         {
557             // convert string to byte
558
String JavaDoc oneByte = str.substring(i*2, i*2+2);
559             
560             Integer JavaDoc number = Integer.valueOf(oneByte, 16);
561             if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0)
562             {
563                 int temp = number.intValue();
564                 temp = temp - 256;
565     
566                 String JavaDoc tempStr = Integer.toString(temp);
567         
568                 Byte JavaDoc myByte = Byte.valueOf(tempStr);
569                 bytes[i] = myByte.byteValue();
570             }
571             else
572             {
573                 Byte JavaDoc myByte = Byte.valueOf(oneByte, 16);
574                 bytes[i] = myByte.byteValue();
575             }
576         }
577         
578         return bytes;
579     }
580 }
581
Popular Tags