0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/A/*
0N/A * The Original Code is HAT. The Initial Developer of the
0N/A * Original Code is Bill Foote, with contributions from others
245N/A * at JavaSoft/Sun.
0N/A */
0N/A
0N/Apackage com.sun.tools.hat.internal.model;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport com.sun.tools.hat.internal.parser.ReadBuffer;
0N/A
0N/A/*
0N/A * Base class for lazily read Java heap objects.
0N/A */
0N/Apublic abstract class JavaLazyReadObject extends JavaHeapObject {
0N/A
0N/A // file offset from which this object data starts
0N/A private final long offset;
0N/A
0N/A protected JavaLazyReadObject(long offset) {
0N/A this.offset = offset;
0N/A }
0N/A
0N/A public final int getSize() {
0N/A return getValueLength() + getClazz().getMinimumObjectSize();
0N/A }
0N/A
0N/A protected final long getOffset() {
0N/A return offset;
0N/A }
0N/A
0N/A // return the length of the data for this object
0N/A protected final int getValueLength() {
0N/A try {
0N/A return readValueLength();
0N/A } catch (IOException exp) {
0N/A System.err.println("lazy read failed at offset " + offset);
0N/A exp.printStackTrace();
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A // get this object's content as byte array
0N/A protected final byte[] getValue() {
0N/A try {
0N/A return readValue();
0N/A } catch (IOException exp) {
0N/A System.err.println("lazy read failed at offset " + offset);
0N/A exp.printStackTrace();
0N/A return Snapshot.EMPTY_BYTE_ARRAY;
0N/A }
0N/A }
0N/A
0N/A // get ID of this object
0N/A public final long getId() {
0N/A try {
0N/A ReadBuffer buf = getClazz().getReadBuffer();
0N/A int idSize = getClazz().getIdentifierSize();
0N/A if (idSize == 4) {
0N/A return ((long)buf.getInt(offset)) & Snapshot.SMALL_ID_MASK;
0N/A } else {
0N/A return buf.getLong(offset);
0N/A }
0N/A } catch (IOException exp) {
0N/A System.err.println("lazy read failed at offset " + offset);
0N/A exp.printStackTrace();
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A protected abstract int readValueLength() throws IOException;
0N/A protected abstract byte[] readValue() throws IOException;
0N/A
0N/A // make Integer or Long for given object ID
0N/A protected static Number makeId(long id) {
0N/A if ((id & ~Snapshot.SMALL_ID_MASK) == 0) {
0N/A return new Integer((int)id);
0N/A } else {
0N/A return new Long(id);
0N/A }
0N/A }
0N/A
0N/A // get ID as long value from Number
0N/A protected static long getIdValue(Number num) {
0N/A long id = num.longValue();
0N/A if (num instanceof Integer) {
0N/A id &= Snapshot.SMALL_ID_MASK;
0N/A }
0N/A return id;
0N/A }
0N/A
0N/A // read object ID from given index from given byte array
0N/A protected final long objectIdAt(int index, byte[] data) {
0N/A int idSize = getClazz().getIdentifierSize();
0N/A if (idSize == 4) {
0N/A return ((long)intAt(index, data)) & Snapshot.SMALL_ID_MASK;
0N/A } else {
0N/A return longAt(index, data);
0N/A }
0N/A }
0N/A
0N/A // utility methods to read primitive types from byte array
0N/A protected static byte byteAt(int index, byte[] value) {
0N/A return value[index];
0N/A }
0N/A
0N/A protected static boolean booleanAt(int index, byte[] value) {
0N/A return (value[index] & 0xff) == 0? false: true;
0N/A }
0N/A
0N/A protected static char charAt(int index, byte[] value) {
0N/A int b1 = ((int) value[index++] & 0xff);
0N/A int b2 = ((int) value[index++] & 0xff);
0N/A return (char) ((b1 << 8) + b2);
0N/A }
0N/A
0N/A protected static short shortAt(int index, byte[] value) {
0N/A int b1 = ((int) value[index++] & 0xff);
0N/A int b2 = ((int) value[index++] & 0xff);
0N/A return (short) ((b1 << 8) + b2);
0N/A }
0N/A
0N/A protected static int intAt(int index, byte[] value) {
0N/A int b1 = ((int) value[index++] & 0xff);
0N/A int b2 = ((int) value[index++] & 0xff);
0N/A int b3 = ((int) value[index++] & 0xff);
0N/A int b4 = ((int) value[index++] & 0xff);
0N/A return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
0N/A }
0N/A
0N/A protected static long longAt(int index, byte[] value) {
0N/A long val = 0;
0N/A for (int j = 0; j < 8; j++) {
0N/A val = val << 8;
0N/A int b = ((int)value[index++]) & 0xff;
0N/A val |= b;
0N/A }
0N/A return val;
0N/A }
0N/A
0N/A protected static float floatAt(int index, byte[] value) {
0N/A int val = intAt(index, value);
0N/A return Float.intBitsToFloat(val);
0N/A }
0N/A
0N/A protected static double doubleAt(int index, byte[] value) {
0N/A long val = longAt(index, value);
0N/A return Double.longBitsToDouble(val);
0N/A }
0N/A}