KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import org.eclipse.debug.internal.core.memory.MemoryByte;
15
16 /**
17  * @since 3.0
18  */

19
20 public class MemoryViewLine extends Object JavaDoc {
21     private String JavaDoc fAddress;
22     private String JavaDoc fStrRep;
23     private MemoryByte[] fBytes;
24     private byte[] fByteArray;
25     private int fTableIndex = -1;
26     private String JavaDoc fPaddedString;
27     public boolean isMonitored;
28
29     public static final String JavaDoc P_ADDRESS = "address"; //$NON-NLS-1$
30

31     // for raw hex data, it's 2 characters per byte
32
private static final int numCharPerByteForHex = 2;
33
34     public MemoryViewLine(String JavaDoc address, MemoryByte[] bytes, int tableIndex, String JavaDoc paddedString) {
35         fAddress = address;
36         fBytes = bytes;
37         fTableIndex = tableIndex;
38         fPaddedString = paddedString;
39     }
40
41     public String JavaDoc getAddress() {
42         return fAddress;
43     }
44
45     public void setAddress(String JavaDoc address) {
46         fAddress = address;
47     }
48     
49     public MemoryByte[] getBytes()
50     {
51         return fBytes;
52     }
53     
54     public MemoryByte getByte(int offset)
55     {
56         if (fBytes == null)
57             return null;
58         
59         if (offset < fBytes.length)
60             return fBytes[offset];
61         else
62             return null;
63             
64     }
65     
66     public MemoryByte[] getBytes(int start, int end)
67     {
68         ArrayList JavaDoc ret = new ArrayList JavaDoc();
69         
70         for (int i=start; i<end; i++)
71         {
72             ret.add(fBytes[i]);
73         }
74         return (MemoryByte[]) ret.toArray(new MemoryByte[ret.size()]);
75     }
76     
77     public String JavaDoc getRawMemoryString()
78     {
79         if (fStrRep == null)
80         {
81             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82             fStrRep = HexRenderer.convertByteArrayToHexString(getByteArray());
83             fStrRep = fStrRep.toUpperCase();
84             
85             buffer = buffer.append(fStrRep);
86             
87             // pad unavailable bytes with padded string from memory block
88
String JavaDoc paddedString = null;
89             int bufferCounter = 0;
90             for (int i=0; i<fBytes.length; i++)
91             {
92                 // if byte is valid
93
if ((fBytes[i].flags & MemoryByte.VALID) != MemoryByte.VALID)
94                 {
95                     if (paddedString == null)
96                     {
97                         paddedString = fPaddedString;
98                         
99                         if (paddedString.length() > MemoryViewLine.numCharPerByteForHex)
100                             paddedString = paddedString.substring(0, MemoryViewLine.numCharPerByteForHex);
101                     }
102                     buffer.replace(bufferCounter, bufferCounter+MemoryViewLine.numCharPerByteForHex, paddedString);
103                     bufferCounter += MemoryViewLine.numCharPerByteForHex;
104                 }
105             }
106             
107             fStrRep = buffer.toString();
108         }
109         
110         return fStrRep;
111     }
112     
113     /**
114      * @param start
115      * @param end
116      * @return
117      */

118     public String JavaDoc getPaddedString(int start, int end) {
119         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
120         
121         for (int i=start; i<end; i++)
122         {
123             buf.append(fPaddedString);
124         }
125         return buf.toString();
126     }
127     
128     public String JavaDoc getPaddedString()
129     {
130         return fPaddedString;
131     }
132
133     /**
134      * @param start
135      * @param end
136      * @return
137      */

138     public boolean isAvailable(int start, int end) {
139         boolean available = true;
140         for (int i=start; i<end; i++)
141         {
142             if ((fBytes[i].flags & MemoryByte.VALID) != MemoryByte.VALID)
143             {
144                 available = false;
145                 break;
146             }
147         }
148         return available;
149     }
150
151
152     public byte[] getByteArray()
153     {
154         if (fByteArray == null)
155         {
156             fByteArray = new byte[fBytes.length];
157             for (int i=0; i<fBytes.length; i++)
158             {
159                 fByteArray[i] = fBytes[i].value;
160             }
161         }
162         
163         return fByteArray;
164     }
165     
166     public byte[] getByteArray(int start, int end)
167     {
168         byte[] ret = new byte[end-start];
169         int j=0;
170         
171         for (int i=start; i<end; i++)
172         {
173             ret[j] = fBytes[i].value;
174             j++;
175         }
176         return ret;
177     }
178     
179     public void markDeltas(MemoryViewLine oldData)
180     {
181         if (oldData == null)
182             return;
183         
184         // if address is not the same, no need to compare
185
if (!oldData.getAddress().equals(this.getAddress()))
186             return;
187         
188         // if the string representation is the same, no need to compare
189
if (oldData.getRawMemoryString().equals(getRawMemoryString()))
190             return;
191         
192         MemoryByte[] oldMemory = oldData.getBytes();
193         
194         if (oldMemory.length != fBytes.length)
195             return;
196             
197         for (int i=0; i<fBytes.length; i++)
198         {
199             if ((fBytes[i].flags & MemoryByte.VALID) != (oldMemory[i].flags & MemoryByte.VALID))
200             {
201                 fBytes[i].flags |= MemoryByte.CHANGED;
202                 continue;
203             }
204                 
205             if (((fBytes[i].flags & MemoryByte.VALID) == MemoryByte.VALID) &&
206                     ((oldMemory[i].flags & MemoryByte.VALID) == MemoryByte.VALID))
207             {
208                 if (fBytes[i].value != oldMemory[i].value)
209                 {
210                     fBytes[i].flags |= MemoryByte.CHANGED;
211                 }
212             }
213         }
214     }
215     
216     public void copyDeltas(MemoryViewLine oldData)
217     {
218         if (oldData == null)
219             return;
220         
221         // if address is not the same, do not copy
222
if (!oldData.getAddress().equals(this.getAddress()))
223             return;
224         
225         // reuse delta information from old data
226
MemoryByte[] oldMemory = oldData.getBytes();
227         
228         if (oldMemory.length != fBytes.length)
229             return;
230             
231         for (int i=0; i<fBytes.length; i++)
232         {
233             fBytes[i].flags = oldMemory[i].flags;
234         }
235     }
236     
237     public boolean isLineChanged(MemoryViewLine oldData)
238     {
239         if (oldData == null)
240             return false;
241         
242         // if address is not the same, no need to compare
243
if (!oldData.getAddress().equals(this.getAddress()))
244             return false;
245         
246         // if the string representation is not the same, this line has changed
247
if (oldData.getRawMemoryString().equals(getRawMemoryString()))
248             return false;
249         else
250             return true;
251     }
252     
253     /**
254      * @param offset
255      * @param endOffset
256      * @return true if the specified range of memory has changed, false otherwise
257      * */

258     
259     public boolean isRangeChange(int offset, int endOffset)
260     {
261         byte ret = fBytes[offset].flags;
262         for (int i=offset; i<=endOffset; i++)
263         {
264             ret |= fBytes[i].flags;
265         }
266         
267         if ((ret&MemoryByte.CHANGED) == MemoryByte.CHANGED)
268             return true;
269         else
270             return false;
271     }
272     
273     public void unmarkDeltas()
274     {
275         for (int i=0; i<fBytes.length; i++)
276         {
277             // unset the change bit
278
if ((fBytes[i].flags & MemoryByte.CHANGED) == MemoryByte.CHANGED)
279                 fBytes[i].flags ^= MemoryByte.CHANGED;
280         }
281     }
282     
283     /* (non-Javadoc)
284      * @see java.lang.Object#toString()
285      */

286     public String JavaDoc toString()
287     {
288         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
289         buf.append(getAddress());
290         
291         buf.append(": "); //$NON-NLS-1$
292

293         buf.append(getRawMemoryString());
294         
295         return buf.toString();
296     }
297     
298     public int getTableIndex()
299     {
300         return fTableIndex;
301     }
302
303 }
304
305
Popular Tags