gcc/java:
[official-gcc.git] / libjava / gnu / gcj / tools / gc_analyze / BytePtr.java
blob4afceeeec8a4d0217ccfe9808295cc6e341c33e8
1 /* BytePtr.java -- Container for bytes from a memory image.
2 Copyright (C) 2007 Free Software Foundation
4 This file is part of libgcj.
6 This software is copyrighted work licensed under the terms of the
7 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
8 details. */
10 package gnu.gcj.tools.gc_analyze;
12 import java.nio.ByteBuffer;
14 public class BytePtr
16 ByteBuffer content;
17 int wordSize;
19 BytePtr(ByteBuffer b, int ws)
21 content = b;
22 wordSize = ws;
25 public int getsize()
27 return content.limit();
30 public int getByte(int offset)
32 return content.get(offset);
35 public int getInt(int n)
37 return content.getInt(n * 4);
40 public int getShort(int n)
42 return content.getShort(n * 2);
45 public long getWord(int n)
47 if (4 == wordSize)
48 return 0xffffffffL & content.getInt(n * 4);
49 else
50 return content.getLong(n * 8);
53 public int intsPerWord()
55 return (4 == wordSize) ? 1 : 2;
58 public BytePtr getRegion(int offset, int size)
60 int oldLimit = content.limit();
61 content.position(offset);
62 content.limit(offset + size);
63 ByteBuffer n = content.slice();
64 content.position(0);
65 content.limit(oldLimit);
67 return new BytePtr(n, wordSize);
70 public void setInt(int a, int n)
72 content.putInt(a * 4, n);
75 public void dump()
77 // 38 5a f4 2a 50 bd 04 10 10 00 00 00 0e 00 00 00 8Z.*P...........
78 int i;
79 StringBuilder b = new StringBuilder(67);
80 for (i = 0; i < 66; i++)
81 b.append(' ');
82 b.append('\n');
84 i = 0;
87 for (int j = 0; j < 16; j++)
89 int k = i + j;
91 if (k < content.limit())
93 int v = 0xff & getByte(k);
94 // hex
95 int v1 = v/16;
96 b.setCharAt(j * 3 + 0,
97 (char)(v1 >= 10 ? 'a' - 10 + v1 : v1 + '0'));
98 v1 = v % 16;
99 b.setCharAt(j * 3 + 1,
100 (char)(v1 >= 10 ? 'a' - 10 + v1 : v1 + '0'));
101 // ascii
102 b.setCharAt(j + 50, (char)((v >= 32 && v <= 127) ? v: '.'));
104 else
106 b.setCharAt(j * 3 + 0, ' ');
107 b.setCharAt(j * 3 + 1, ' ');
108 b.setCharAt(j + 50, ' ');
111 i += 16;
112 System.out.print(b);
113 } while (i < content.limit());