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