1 /*
   2  * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 /*
  28  * The Original Code is HAT. The Initial Developer of the
  29  * Original Code is Bill Foote, with contributions from others
  30  * at JavaSoft/Sun.
  31  */
  32 
  33 package com.sun.tools.hat.internal.model;
  34 
  35 import java.io.IOException;
  36 import com.sun.tools.hat.internal.parser.ReadBuffer;
  37 
  38 /*
  39  * Base class for lazily read Java heap objects.
  40  */
  41 public abstract class JavaLazyReadObject extends JavaHeapObject {
  42 
  43     // file offset from which this object data starts
  44     private final long offset;
  45 
  46     protected JavaLazyReadObject(long offset) {
  47         this.offset = offset;
  48     }
  49 
  50     public final int getSize() {
  51         return getValueLength() + getClazz().getMinimumObjectSize();
  52     }
  53 
  54     protected final long getOffset() {
  55         return offset;
  56     }
  57 
  58     // return the length of the data for this object
  59     protected final int getValueLength() {
  60         try {
  61             return readValueLength();
  62         } catch (IOException exp) {
  63             System.err.println("lazy read failed at offset " + offset);
  64             exp.printStackTrace();
  65             return 0;
  66         }
  67     }
  68 
  69     // get this object's content as byte array
  70     protected final byte[] getValue() {
  71         try {
  72             return readValue();
  73         } catch (IOException exp) {
  74             System.err.println("lazy read failed at offset " + offset);
  75             exp.printStackTrace();
  76             return Snapshot.EMPTY_BYTE_ARRAY;
  77         }
  78     }
  79 
  80     // get ID of this object
  81     public final long getId() {
  82         try {
  83             ReadBuffer buf = getClazz().getReadBuffer();
  84             int idSize = getClazz().getIdentifierSize();
  85             if (idSize == 4) {
  86                 return ((long)buf.getInt(offset)) & Snapshot.SMALL_ID_MASK;
  87             } else {
  88                 return buf.getLong(offset);
  89             }
  90         } catch (IOException exp) {
  91             System.err.println("lazy read failed at offset " + offset);
  92             exp.printStackTrace();
  93             return -1;
  94         }
  95     }
  96 
  97     protected abstract int readValueLength() throws IOException;
  98     protected abstract byte[] readValue() throws IOException;
  99 
 100     // make Integer or Long for given object ID
 101     protected static Number makeId(long id) {
 102         if ((id & ~Snapshot.SMALL_ID_MASK) == 0) {
 103             return new Integer((int)id);
 104         } else {
 105             return id;
 106         }
 107     }
 108 
 109     // get ID as long value from Number
 110     protected static long getIdValue(Number num) {
 111         long id = num.longValue();
 112         if (num instanceof Integer) {
 113             id &= Snapshot.SMALL_ID_MASK;
 114         }
 115         return id;
 116     }
 117 
 118     // read object ID from given index from given byte array
 119     protected final long objectIdAt(int index, byte[] data) {
 120         int idSize = getClazz().getIdentifierSize();
 121         if (idSize == 4) {
 122             return ((long)intAt(index, data)) & Snapshot.SMALL_ID_MASK;
 123         } else {
 124             return longAt(index, data);
 125         }
 126     }
 127 
 128     // utility methods to read primitive types from byte array
 129     protected static byte byteAt(int index, byte[] value) {
 130         return value[index];
 131     }
 132 
 133     protected static boolean booleanAt(int index, byte[] value) {
 134         return (value[index] & 0xff) == 0? false: true;
 135     }
 136 
 137     protected static char charAt(int index, byte[] value) {
 138         int b1 = ((int) value[index++] & 0xff);
 139         int b2 = ((int) value[index++] & 0xff);
 140         return (char) ((b1 << 8) + b2);
 141     }
 142 
 143     protected static short shortAt(int index, byte[] value) {
 144         int b1 = ((int) value[index++] & 0xff);
 145         int b2 = ((int) value[index++] & 0xff);
 146         return (short) ((b1 << 8) + b2);
 147     }
 148 
 149     protected static int intAt(int index, byte[] value) {
 150         int b1 = ((int) value[index++] & 0xff);
 151         int b2 = ((int) value[index++] & 0xff);
 152         int b3 = ((int) value[index++] & 0xff);
 153         int b4 = ((int) value[index++] & 0xff);
 154         return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
 155     }
 156 
 157     protected static long longAt(int index, byte[] value) {
 158         long val = 0;
 159         for (int j = 0; j < 8; j++) {
 160             val = val << 8;
 161             int b = ((int)value[index++]) & 0xff;
 162             val |= b;
 163         }
 164         return val;
 165     }
 166 
 167     protected static float floatAt(int index, byte[] value) {
 168         int val = intAt(index, value);
 169         return Float.intBitsToFloat(val);
 170     }
 171 
 172     protected static double doubleAt(int index, byte[] value) {
 173         long val = longAt(index, value);
 174         return Double.longBitsToDouble(val);
 175     }
 176 }