1 11 package org.eclipse.debug.internal.ui.views.memory.renderings; 12 13 import java.math.BigInteger ; 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 22 public class UnsignedIntegerRendering extends AbstractIntegerRendering { 23 24 28 public UnsignedIntegerRendering(String renderingId) { 29 super(renderingId); 30 } 31 32 private String convertToString(byte[] byteArray, int columnSize, int endianess) 33 { 34 String 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 value = RenderingsUtil.convertByteArrayToUnsignedLong(byteArray, endianess); 53 return value.toString(); 54 } 55 else if (columnSize == 16) 56 { 57 BigInteger bigRet = RenderingsUtil.convertByteArrayToUnsignedBigInt(byteArray, endianess); 58 return bigRet.toString(); 59 } 60 else 61 { 62 BigInteger bigRet = RenderingsUtil.convertByteArrayToUnsignedBigInt(byteArray, endianess, columnSize); 63 return bigRet.toString(); 64 } 65 66 ret = new Long (result).toString(); 67 68 return ret; 69 } 70 71 private byte[] convertToBytes(int colSize, String 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 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 i = new BigInteger (newValue); 97 bytes = RenderingsUtil.convertBigIntegerToByteArray(i, endianess); 98 bytes = extractBytes(bytes, endianess, colSize); 99 } 100 else if (colSize == 16) 101 { 102 BigInteger i = new BigInteger (newValue); 103 bytes = RenderingsUtil.convertUnsignedBigIntegerToByteArray(i, endianess); 104 bytes = extractBytes(bytes, endianess, colSize); 105 106 return bytes; 107 } 108 else 109 { 110 BigInteger i = new BigInteger (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 e) { 118 throw e; 119 } 120 } 121 122 125 public String getString(String dataType, BigInteger address, MemoryByte[] data) { 126 127 String 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 strBuf = new StringBuffer (); 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 (RenderingsUtil.ENDIANESS_UNKNOWN == endianess) 161 { 162 StringBuffer strBuf = new StringBuffer (); 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 176 public byte[] getBytes(String dataType, BigInteger address, MemoryByte[] currentValues, String data) { 177 178 int columnSize = getBytesPerColumn(); 179 int endianess = getDisplayEndianess(); 180 if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN) 181 endianess = getBytesEndianess(currentValues); 182 183 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 (); 199 200 if (endianess == RenderingsUtil.BIG_ENDIAN) 203 { 204 for (int i=0; i<colSize; i++) 208 { 209 if (bytes[i] != 0) 210 throw new NumberFormatException (); 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 else 222 { 223 for (int i=colSize; i<bytes.length; i++) 227 { 228 if (bytes[i] != 0) 229 throw new NumberFormatException (); 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 |