summaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/tools/gc_analyze/BytePtr.java
blob: 4afceeeec8a4d0217ccfe9808295cc6e341c33e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* BytePtr.java -- Container for bytes from a memory image.
   Copyright (C) 2007  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package gnu.gcj.tools.gc_analyze;

import java.nio.ByteBuffer;

public class BytePtr
{
  ByteBuffer content;
  int wordSize;

  BytePtr(ByteBuffer b, int ws)
  {
    content = b;
    wordSize = ws;
  }
  
  public int getsize()
  {
    return content.limit();
  }

  public int getByte(int offset)
  {
    return content.get(offset);
  }

  public int getInt(int n)
  {
    return content.getInt(n * 4);
  }

  public int getShort(int n)
  {
    return content.getShort(n * 2);
  }
  
  public long getWord(int n)
  {
    if (4 == wordSize)
      return 0xffffffffL & content.getInt(n * 4);
    else
      return content.getLong(n * 8);
  }
  
  public int intsPerWord()
  {
    return (4 == wordSize) ? 1 : 2;
  }

  public BytePtr getRegion(int offset, int size)
  {
    int oldLimit = content.limit();
    content.position(offset);
    content.limit(offset + size);
    ByteBuffer n = content.slice();
    content.position(0);
    content.limit(oldLimit);
    
    return new BytePtr(n, wordSize);
  }

  public void setInt(int a, int n)
  {
    content.putInt(a * 4, n);
  }

  public void dump()
  {
    // 38 5a f4 2a 50 bd 04 10 10 00 00 00 0e 00 00 00   8Z.*P...........
    int i;
    StringBuilder b = new StringBuilder(67);
    for (i = 0; i < 66; i++)
      b.append(' ');
    b.append('\n');

    i = 0;
    do
      {
        for (int j = 0; j < 16; j++)
          {
            int k = i + j;

            if (k < content.limit())
              {
                int v = 0xff & getByte(k);
                // hex
                int v1 = v/16;
                b.setCharAt(j * 3 + 0,
                            (char)(v1 >= 10 ? 'a' - 10 + v1 : v1 + '0'));
                v1 = v % 16;
                b.setCharAt(j * 3 + 1,
                            (char)(v1 >= 10 ? 'a' - 10 + v1 : v1 + '0'));
                // ascii
                b.setCharAt(j + 50, (char)((v >= 32 && v <= 127) ? v: '.'));
              }
            else
              {
                b.setCharAt(j * 3 + 0, ' ');
                b.setCharAt(j * 3 + 1, ' ');
                b.setCharAt(j + 50, ' ');
              }
          }
        i += 16;
        System.out.print(b);
      } while (i < content.limit());
  }
}