KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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 import org.eclipse.debug.core.model.MemoryByte;
16 import org.eclipse.debug.internal.ui.DebugUIPlugin;
17 import org.eclipse.debug.ui.IDebugUIConstants;
18
19 /**
20  * Represent unsigned integer rendering
21  */

22 public class UnsignedIntegerRendering extends AbstractIntegerRendering {
23
24     /**
25      * @param memBlock
26      * @param renderingId
27      */

28     public UnsignedIntegerRendering(String JavaDoc renderingId) {
29         super(renderingId);
30     }
31
32     private String JavaDoc convertToString(byte[] byteArray, int columnSize, int endianess)
33     {
34         String JavaDoc ret;
35         long result = 0;
36         
37         if (columnSize == 1)
38         {
39             result = byteArray[0];
40             result &= 0xff;
41         }
42         else if (columnSize == 2)
43         {
44             result = RenderingsUtil.convertByteArrayToInt(byteArray, endianess);
45         }
46         else if (columnSize == 4)
47         {
48             result = RenderingsUtil.convertByteArrayToLong(byteArray, endianess);
49         }
50         else if (columnSize == 8)
51         {
52             BigInteger JavaDoc value = RenderingsUtil.convertByteArrayToUnsignedLong(byteArray, endianess);
53             return value.toString();
54         }
55         else if (columnSize == 16)
56         {
57             BigInteger JavaDoc bigRet = RenderingsUtil.convertByteArrayToUnsignedBigInt(byteArray, endianess);
58             return bigRet.toString();
59         }
60         else
61         {
62             BigInteger JavaDoc bigRet = RenderingsUtil.convertByteArrayToUnsignedBigInt(byteArray, endianess, columnSize);
63             return bigRet.toString();
64         }
65
66         ret = new Long JavaDoc(result).toString();
67         
68         return ret;
69     }
70     
71     private byte[] convertToBytes(int colSize, String JavaDoc newValue, int endianess)
72     {
73         try {
74             byte[] bytes;
75             if (colSize == 1)
76             {
77                 short i = Short.parseShort(newValue);
78                 bytes = RenderingsUtil.convertShortToByteArray(i, endianess);
79                 bytes = extractBytes(bytes, endianess, colSize);
80             }
81             // unsigned integer
82
else if (colSize == 2)
83             {
84                 int i = Integer.parseInt(newValue);
85                 bytes = RenderingsUtil.convertIntToByteArray(i, endianess);
86                 bytes = extractBytes(bytes, endianess, colSize);
87             }
88             else if (colSize == 4)
89             {
90                 long i = Long.parseLong(newValue);
91                 bytes = RenderingsUtil.convertLongToByteArray(i, endianess);
92                 bytes = extractBytes(bytes, endianess, colSize);
93             }
94             else if (colSize == 8)
95             {
96                 BigInteger JavaDoc i = new BigInteger JavaDoc(newValue);
97                 bytes = RenderingsUtil.convertBigIntegerToByteArray(i, endianess);
98                 bytes = extractBytes(bytes, endianess, colSize);
99             }
100             else if (colSize == 16)
101             {
102                 BigInteger JavaDoc i = new BigInteger JavaDoc(newValue);
103                 bytes = RenderingsUtil.convertUnsignedBigIntegerToByteArray(i, endianess);
104                 bytes = extractBytes(bytes, endianess, colSize);
105
106                 return bytes;
107             }
108             else
109             {
110                 BigInteger JavaDoc i = new BigInteger JavaDoc(newValue);
111                 bytes = RenderingsUtil.convertUnsignedBigIntToByteArray(i, endianess, colSize);
112                 bytes = extractBytes(bytes, endianess, colSize);
113                 return bytes;
114             }
115             
116             return bytes;
117         } catch (NumberFormatException JavaDoc e) {
118             throw e;
119         }
120     }
121
122     /* (non-Javadoc)
123      * @see com.ibm.debug.extended.ui.AbstractMemoryRenderer#getString(java.lang.String, java.math.BigInteger, byte[])
124      */

125     public String JavaDoc getString(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] data) {
126         
127         String JavaDoc paddedStr = DebugUIPlugin.getDefault().getPreferenceStore().getString(IDebugUIConstants.PREF_PADDED_STR);
128         boolean invalid = false;
129         for (int i=0; i<data.length; i++)
130         {
131             if (!data[i].isReadable())
132             {
133                 invalid = true;
134                 break;
135             }
136         }
137         
138         if (invalid)
139         {
140             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
141             for (int i=0; i<data.length; i++)
142             {
143                 strBuf.append(paddedStr);
144             }
145             return strBuf.toString();
146         }
147         
148         int columnSize = getBytesPerColumn();
149         int endianess = getDisplayEndianess();
150         if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
151             endianess = getBytesEndianess(data);
152         
153         byte[] byteArray = new byte[data.length];
154         for (int i=0; i<byteArray.length;i ++)
155         {
156             byteArray[i] = data[i].getValue();
157         }
158         
159         // if endianess is unknown, do not render, just return padded string
160
if (RenderingsUtil.ENDIANESS_UNKNOWN == endianess)
161         {
162             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
163             for (int i=0; i<byteArray.length; i++)
164             {
165                 strBuf.append(paddedStr);
166             }
167             return strBuf.toString();
168         }
169         
170         return convertToString(byteArray, columnSize, endianess);
171     }
172
173     /* (non-Javadoc)
174      * @see com.ibm.debug.extended.ui.AbstractMemoryRenderer#getBytes(java.lang.String, java.math.BigInteger, java.lang.String)
175      */

176     public byte[] getBytes(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] currentValues, String JavaDoc data) {
177         
178         int columnSize = getBytesPerColumn();
179         int endianess = getDisplayEndianess();
180         if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
181             endianess = getBytesEndianess(currentValues);
182         
183         // if endianess is unknown, do not try to render new data to bytes
184
if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
185         {
186             byte[] retBytes = new byte[currentValues.length];
187             for (int i=0 ;i<currentValues.length; i++)
188                 retBytes[i] = currentValues[i].getValue();
189             return retBytes;
190         }
191         
192         return convertToBytes(columnSize, data, endianess);
193     }
194     
195     private byte[] extractBytes(byte[] bytes, int endianess, int colSize) {
196         
197         if (colSize > bytes.length)
198             throw new NumberFormatException JavaDoc();
199         
200         // take the least significant 'colSize' bytes out of the bytes array
201
// if it's big endian, it's the last 'colSize' bytes
202
if (endianess == RenderingsUtil.BIG_ENDIAN)
203         {
204             // check most significan bytes... if data has to be represented
205
// using more than 'colSize' number of bytes, this
206
// number is invalid, throw number format exception
207
for (int i=0; i<colSize; i++)
208             {
209                 if (bytes[i] != 0)
210                     throw new NumberFormatException JavaDoc();
211             }
212             
213             byte[] copy = new byte[colSize];
214             for (int j=0, k=bytes.length-colSize; j<copy.length && k<bytes.length; j++, k++)
215             {
216                 copy[j] = bytes[k];
217             }
218             bytes = copy;
219         }
220         // if it's little endian, it's the first 'colSize' bytes
221
else
222         {
223             // check most significan bytes... if data has to be represented
224
// using more than 'colSize' number of bytes, this
225
// number is invalid, throw number format exception
226
for (int i=colSize; i<bytes.length; i++)
227             {
228                 if (bytes[i] != 0)
229                     throw new NumberFormatException JavaDoc();
230             }
231             
232             byte[] copy = new byte[colSize];
233             for (int j=0; j<copy.length; j++)
234             {
235                 copy[j] = bytes[j];
236             }
237             bytes = copy;
238         }
239         return bytes;
240     }
241 }
242
Popular Tags