jdk/src/share/classes/com/sun/tools/hat/internal/model/JavaLazyReadObject.java
changeset 2 90ce3da70b43
child 468 642c8c0be52e
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 
       
    27 /*
       
    28  * The contents of this file are subject to the Sun Public License
       
    29  * Version 1.0 (the "License"); you may not use this file except in
       
    30  * compliance with the License. A copy of the License is available at
       
    31  * http://www.sun.com/, and in the file LICENSE.html in the
       
    32  * doc directory.
       
    33  *
       
    34  * The Original Code is HAT. The Initial Developer of the
       
    35  * Original Code is Bill Foote, with contributions from others
       
    36  * at JavaSoft/Sun. Portions created by Bill Foote and others
       
    37  * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
       
    38  *
       
    39  * In addition to the formal license, I ask that you don't
       
    40  * change the history or donations files without permission.
       
    41  *
       
    42  */
       
    43 
       
    44 package com.sun.tools.hat.internal.model;
       
    45 
       
    46 import java.io.IOException;
       
    47 import com.sun.tools.hat.internal.parser.ReadBuffer;
       
    48 
       
    49 /*
       
    50  * Base class for lazily read Java heap objects.
       
    51  */
       
    52 public abstract class JavaLazyReadObject extends JavaHeapObject {
       
    53 
       
    54     // file offset from which this object data starts
       
    55     private final long offset;
       
    56 
       
    57     protected JavaLazyReadObject(long offset) {
       
    58         this.offset = offset;
       
    59     }
       
    60 
       
    61     public final int getSize() {
       
    62         return getValueLength() + getClazz().getMinimumObjectSize();
       
    63     }
       
    64 
       
    65     protected final long getOffset() {
       
    66         return offset;
       
    67     }
       
    68 
       
    69     // return the length of the data for this object
       
    70     protected final int getValueLength() {
       
    71         try {
       
    72             return readValueLength();
       
    73         } catch (IOException exp) {
       
    74             System.err.println("lazy read failed at offset " + offset);
       
    75             exp.printStackTrace();
       
    76             return 0;
       
    77         }
       
    78     }
       
    79 
       
    80     // get this object's content as byte array
       
    81     protected final byte[] getValue() {
       
    82         try {
       
    83             return readValue();
       
    84         } catch (IOException exp) {
       
    85             System.err.println("lazy read failed at offset " + offset);
       
    86             exp.printStackTrace();
       
    87             return Snapshot.EMPTY_BYTE_ARRAY;
       
    88         }
       
    89     }
       
    90 
       
    91     // get ID of this object
       
    92     public final long getId() {
       
    93         try {
       
    94             ReadBuffer buf = getClazz().getReadBuffer();
       
    95             int idSize = getClazz().getIdentifierSize();
       
    96             if (idSize == 4) {
       
    97                 return ((long)buf.getInt(offset)) & Snapshot.SMALL_ID_MASK;
       
    98             } else {
       
    99                 return buf.getLong(offset);
       
   100             }
       
   101         } catch (IOException exp) {
       
   102             System.err.println("lazy read failed at offset " + offset);
       
   103             exp.printStackTrace();
       
   104             return -1;
       
   105         }
       
   106     }
       
   107 
       
   108     protected abstract int readValueLength() throws IOException;
       
   109     protected abstract byte[] readValue() throws IOException;
       
   110 
       
   111     // make Integer or Long for given object ID
       
   112     protected static Number makeId(long id) {
       
   113         if ((id & ~Snapshot.SMALL_ID_MASK) == 0) {
       
   114             return new Integer((int)id);
       
   115         } else {
       
   116             return new Long(id);
       
   117         }
       
   118     }
       
   119 
       
   120     // get ID as long value from Number
       
   121     protected static long getIdValue(Number num) {
       
   122         long id = num.longValue();
       
   123         if (num instanceof Integer) {
       
   124             id &= Snapshot.SMALL_ID_MASK;
       
   125         }
       
   126         return id;
       
   127     }
       
   128 
       
   129     // read object ID from given index from given byte array
       
   130     protected final long objectIdAt(int index, byte[] data) {
       
   131         int idSize = getClazz().getIdentifierSize();
       
   132         if (idSize == 4) {
       
   133             return ((long)intAt(index, data)) & Snapshot.SMALL_ID_MASK;
       
   134         } else {
       
   135             return longAt(index, data);
       
   136         }
       
   137     }
       
   138 
       
   139     // utility methods to read primitive types from byte array
       
   140     protected static byte byteAt(int index, byte[] value) {
       
   141         return value[index];
       
   142     }
       
   143 
       
   144     protected static boolean booleanAt(int index, byte[] value) {
       
   145         return (value[index] & 0xff) == 0? false: true;
       
   146     }
       
   147 
       
   148     protected static char charAt(int index, byte[] value) {
       
   149         int b1 = ((int) value[index++] & 0xff);
       
   150         int b2 = ((int) value[index++] & 0xff);
       
   151         return (char) ((b1 << 8) + b2);
       
   152     }
       
   153 
       
   154     protected static short shortAt(int index, byte[] value) {
       
   155         int b1 = ((int) value[index++] & 0xff);
       
   156         int b2 = ((int) value[index++] & 0xff);
       
   157         return (short) ((b1 << 8) + b2);
       
   158     }
       
   159 
       
   160     protected static int intAt(int index, byte[] value) {
       
   161         int b1 = ((int) value[index++] & 0xff);
       
   162         int b2 = ((int) value[index++] & 0xff);
       
   163         int b3 = ((int) value[index++] & 0xff);
       
   164         int b4 = ((int) value[index++] & 0xff);
       
   165         return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
       
   166     }
       
   167 
       
   168     protected static long longAt(int index, byte[] value) {
       
   169         long val = 0;
       
   170         for (int j = 0; j < 8; j++) {
       
   171             val = val << 8;
       
   172             int b = ((int)value[index++]) & 0xff;
       
   173             val |= b;
       
   174         }
       
   175         return val;
       
   176     }
       
   177 
       
   178     protected static float floatAt(int index, byte[] value) {
       
   179         int val = intAt(index, value);
       
   180         return Float.intBitsToFloat(val);
       
   181     }
       
   182 
       
   183     protected static double doubleAt(int index, byte[] value) {
       
   184         long val = longAt(index, value);
       
   185         return Double.longBitsToDouble(val);
       
   186     }
       
   187 }