Source for gnu.gcj.tools.gc_analyze.BytePtr

   1: /* BytePtr.java -- Container for bytes from a memory image.
   2:    Copyright (C) 2007  Free Software Foundation
   3: 
   4:    This file is part of libgcj.
   5: 
   6: This software is copyrighted work licensed under the terms of the
   7: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   8: details.  */
   9: 
  10: package gnu.gcj.tools.gc_analyze;
  11: 
  12: import java.nio.ByteBuffer;
  13: 
  14: public class BytePtr
  15: {
  16:   ByteBuffer content;
  17:   int wordSize;
  18: 
  19:   BytePtr(ByteBuffer b, int ws)
  20:   {
  21:     content = b;
  22:     wordSize = ws;
  23:   }
  24:   
  25:   public int getsize()
  26:   {
  27:     return content.limit();
  28:   }
  29: 
  30:   public int getByte(int offset)
  31:   {
  32:     return content.get(offset);
  33:   }
  34: 
  35:   public int getInt(int n)
  36:   {
  37:     return content.getInt(n * 4);
  38:   }
  39: 
  40:   public int getShort(int n)
  41:   {
  42:     return content.getShort(n * 2);
  43:   }
  44:   
  45:   public long getWord(int n)
  46:   {
  47:     if (4 == wordSize)
  48:       return 0xffffffffL & content.getInt(n * 4);
  49:     else
  50:       return content.getLong(n * 8);
  51:   }
  52:   
  53:   public int intsPerWord()
  54:   {
  55:     return (4 == wordSize) ? 1 : 2;
  56:   }
  57: 
  58:   public BytePtr getRegion(int offset, int size)
  59:   {
  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);
  66:     
  67:     return new BytePtr(n, wordSize);
  68:   }
  69: 
  70:   public void setInt(int a, int n)
  71:   {
  72:     content.putInt(a * 4, n);
  73:   }
  74: 
  75:   public void dump()
  76:   {
  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');
  83: 
  84:     i = 0;
  85:     do
  86:       {
  87:         for (int j = 0; j < 16; j++)
  88:           {
  89:             int k = i + j;
  90: 
  91:             if (k < content.limit())
  92:               {
  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: '.'));
 103:               }
 104:             else
 105:               {
 106:                 b.setCharAt(j * 3 + 0, ' ');
 107:                 b.setCharAt(j * 3 + 1, ' ');
 108:                 b.setCharAt(j + 50, ' ');
 109:               }
 110:           }
 111:         i += 16;
 112:         System.out.print(b);
 113:       } while (i < content.limit());
 114:   }
 115: }