KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
21  * Represent a signed integer rendering.
22  */

23 public class SignedIntegerRendering extends AbstractIntegerRendering {
24
25     private int fColSize;
26     private BigInteger JavaDoc fMax;
27     private BigInteger JavaDoc fMin;
28     
29     /**
30      * @param memBlock
31      * @param renderingId
32      */

33     public SignedIntegerRendering(String JavaDoc renderingId) {
34         super(renderingId);
35     }
36
37     private String JavaDoc convertToString(byte[] byteArray, int columnSize, int endianess)
38     {
39         String JavaDoc ret;
40         long result = 0;
41         
42         if (columnSize == 1)
43         {
44             result = byteArray[0];
45         }
46         else if (columnSize == 2)
47         {
48             result = RenderingsUtil.convertByteArrayToShort(byteArray, endianess);
49         }
50         else if (columnSize == 4)
51         {
52             result = RenderingsUtil.convertByteArrayToInt(byteArray, endianess);
53         }
54         else if (columnSize == 8)
55         {
56             result = RenderingsUtil.convertByteArrayToLong(byteArray, endianess);
57         }
58         else if (columnSize == 16)
59         {
60             BigInteger JavaDoc bigRet = RenderingsUtil.convertByteArrayToSignedBigInt(byteArray, endianess);
61             return bigRet.toString();
62         }
63         else
64         {
65             BigInteger JavaDoc bigRet = RenderingsUtil.convertByteArrayToSignedBigInt(byteArray, endianess, columnSize);
66             return bigRet.toString();
67         }
68
69         ret = new Long JavaDoc(result).toString();
70         
71         return ret;
72     }
73     
74     private byte[] convertToBytes(int colSize, String JavaDoc newValue, int endianess)
75     {
76         try {
77             byte[] bytes;
78             if (colSize == 1)
79             {
80                 byte x = Byte.parseByte(newValue);
81                 bytes = new byte[1];
82                 bytes[0] = x;
83             }
84             else if (colSize == 2)
85             {
86                 short i = Short.parseShort(newValue);
87                 bytes = RenderingsUtil.convertShortToByteArray(i, endianess);
88             }
89             else if (colSize == 4)
90             {
91                 int i = Integer.parseInt(newValue);
92                 bytes = RenderingsUtil.convertIntToByteArray(i, endianess);
93             }
94             else if (colSize == 8)
95             {
96                 long i = Long.parseLong(newValue);
97                 bytes = RenderingsUtil.convertLongToByteArray(i, endianess);
98             }
99             else if (colSize == 16)
100             {
101                 // special case for colSize == 16
102
// need to represent number in Big Integer
103
BigInteger JavaDoc i = new BigInteger JavaDoc(newValue);
104                 bytes = RenderingsUtil.convertBigIntegerToByteArray(i, endianess);
105             
106                 return bytes;
107             }
108             else
109             {
110                 BigInteger JavaDoc i = new BigInteger JavaDoc(newValue);
111                 
112                 // avoid calculating max and min over and over again
113
// for the same column size
114
if (fColSize != colSize)
115                 {
116                     fColSize = colSize;
117                     fMax = BigInteger.valueOf(2);
118                     fMax = fMax.pow(colSize*8-1);
119                     fMin = fMax.multiply(BigInteger.valueOf(-1));
120                     fMax = fMax.subtract(BigInteger.valueOf(1));
121                 }
122                 
123                 if (i.compareTo(fMax) > 0 || i.compareTo(fMin) < 0)
124                     throw new NumberFormatException JavaDoc();
125                 
126                 bytes = RenderingsUtil.convertSignedBigIntToByteArray(i, endianess, colSize);
127                 return bytes;
128             }
129             
130             return bytes;
131         } catch (NumberFormatException JavaDoc e) {
132             throw e;
133         }
134     }
135
136     /* (non-Javadoc)
137      * @see com.ibm.debug.extended.ui.AbstractMemoryRenderer#getString(java.lang.String, java.math.BigInteger, byte[])
138      */

139     public String JavaDoc getString(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] data) {
140         
141         boolean invalid = false;
142         String JavaDoc paddedStr = DebugUIPlugin.getDefault().getPreferenceStore().getString(IDebugUIConstants.PREF_PADDED_STR);
143         for (int i=0; i<data.length; i++)
144         {
145             if (!data[i].isReadable())
146             {
147                 invalid = true;
148                 break;
149             }
150         }
151         
152         if (invalid)
153         {
154             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
155             for (int i=0; i<data.length; i++)
156             {
157                 strBuf.append(paddedStr);
158             }
159             return strBuf.toString();
160         }
161         
162         int columnSize = getBytesPerColumn();
163         
164         // if the user has not set an endianess to the rendering
165
// take default endianess from bytes
166
int endianess = getDisplayEndianess();
167         if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
168             endianess = getBytesEndianess(data);
169         
170         byte[] byteArray = new byte[data.length];
171         for (int i=0; i<byteArray.length;i ++)
172         {
173             byteArray[i] = data[i].getValue();
174         }
175         
176         // if endianess is unknown, do not render, just return padded string
177
if (RenderingsUtil.ENDIANESS_UNKNOWN == endianess)
178         {
179             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
180             for (int i=0; i<byteArray.length; i++)
181             {
182                 strBuf.append(paddedStr);
183             }
184             return strBuf.toString();
185         }
186         return convertToString(byteArray, columnSize, endianess);
187     }
188
189     /* (non-Javadoc)
190      * @see com.ibm.debug.extended.ui.AbstractMemoryRenderer#getBytes(java.lang.String, java.math.BigInteger, java.lang.String)
191      */

192     public byte[] getBytes(String JavaDoc dataType, BigInteger JavaDoc address, MemoryByte[] currentValues, String JavaDoc data) {
193         
194         int columnSize = getBytesPerColumn();
195         
196         // if the user has not set an endianess to the rendering
197
// take default
198
int endianess = getDisplayEndianess();
199         if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
200             endianess = getBytesEndianess(currentValues);
201         
202         // if endianess is unknown, do not try to render new data to bytes
203
if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN)
204         {
205             byte[] retBytes = new byte[currentValues.length];
206             for (int i=0 ;i<currentValues.length; i++)
207                 retBytes[i] = currentValues[i].getValue();
208             return retBytes;
209         }
210         
211         return convertToBytes(columnSize, data, endianess);
212     }
213 }
214
Popular Tags