KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.debug.internal.ui.views.memory;
12
13 import java.math.BigInteger JavaDoc;
14 import org.eclipse.debug.internal.core.memory.MemoryByte;
15
16
17 /**
18  * Converts bytes into raw hex data and vice versa.
19  *
20  * @since 3.0
21  */

22 public class HexRenderer extends AbstractMemoryRenderer implements IFixedLengthOutputRenderer {
23
24     /* (non-Javadoc)
25      * @see org.eclipse.debug.internal.ui.views.memory.AbstractMemoryRenderer#getString(java.lang.String, java.math.BigInteger, org.eclipse.debug.internal.core.memory.MemoryByte[], java.lang.String)
26      */

27     public String JavaDoc getString(
28         String JavaDoc dataType,
29         BigInteger JavaDoc address,
30         MemoryByte[] data, String JavaDoc paddedStr) {
31
32         StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc();
33         
34         for (int i=0; i<data.length; i++)
35         {
36             if ((data[i].flags & MemoryByte.VALID) != 0)
37             {
38                 strBuffer.append(new String JavaDoc(convertByteToCharArray(data[i].value)));
39             }
40             else
41             {
42                 // pad with padded string
43
strBuffer.append(paddedStr);
44             }
45         }
46         
47         return strBuffer.toString().toUpperCase();
48
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.debug.internal.ui.views.memory.AbstractMemoryRenderer#getBytes(java.lang.String, java.math.BigInteger, org.eclipse.debug.internal.core.memory.MemoryByte[], java.lang.String)
53      */

54     public byte[] getBytes(
55         String JavaDoc dataType,
56         BigInteger JavaDoc address,
57         MemoryByte[] currentValues, String JavaDoc data) {
58
59         byte[] bytes = convertHexStringToByteArray(data);
60         
61         return bytes;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.debug.internal.ui.views.memory.IFixedLengthOutputRenderer#getNumCharPerByte()
66      */

67     public int getNumCharPerByte() {
68         return 2;
69     }
70
71     /**
72      * byte array to Hex string helper
73      * replaces the Integer.toHexString() which can't convert byte values properly
74      * (always pads with FFFFFF)
75      */

76     static public String JavaDoc convertByteArrayToHexString(byte[] byteArray)
77     {
78         StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc();
79         char charArray[];
80         
81         for (int i=0; i<byteArray.length;i ++)
82         {
83             charArray = convertByteToCharArray(byteArray[i]);
84             strBuffer.append(charArray);
85         }
86         
87         return strBuffer.toString();
88     }
89     
90     static private char[] convertByteToCharArray(byte aByte)
91     {
92         char charArray[] = new char[2];
93         int val = aByte;
94         if (val<0) val += 256;
95         charArray[0] = Character.forDigit(val/16, 16);
96         charArray[1] = Character.forDigit(val%16, 16);
97         
98         return charArray;
99     }
100
101     /**
102      * Convert raw memory datat to byte array
103      * @param str
104      * @return
105      * @throws NumberFormatException
106      */

107     public byte[] convertHexStringToByteArray(String JavaDoc str) throws NumberFormatException JavaDoc
108     {
109         if (str.length() < getNumCharPerByte())
110             return null;
111         
112         byte[] bytes = new byte[str.length()/getNumCharPerByte()];
113     
114         // set data in memory
115
for (int i=0; i<bytes.length; i++)
116         {
117             // convert string to byte
118
String JavaDoc oneByte = str.substring(i*2, i*2+2);
119             
120             Integer JavaDoc number = Integer.valueOf(oneByte, 16);
121             if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0)
122             {
123                 int temp = number.intValue();
124                 temp = temp - 256;
125     
126                 String JavaDoc tempStr = Integer.toString(temp);
127         
128                 Byte JavaDoc myByte = Byte.valueOf(tempStr);
129                 bytes[i] = myByte.byteValue();
130             }
131             else
132             {
133                 Byte JavaDoc myByte = Byte.valueOf(oneByte, 16);
134                 bytes[i] = myByte.byteValue();
135             }
136         }
137         
138         return bytes;
139     }
140
141 }
142
Popular Tags