jdk/src/share/classes/com/sun/tools/hat/internal/model/JavaValueArray.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 com.sun.tools.hat.internal.parser.ReadBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An array of values, that is, an array of ints, boolean, floats or the like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class JavaValueArray extends JavaLazyReadObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                /*imports*/ implements ArrayTypeCodes {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static String arrayTypeName(byte sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        switch (sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                return "byte[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                return "boolean[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                return "char[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                return "short[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                return "int[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                return "float[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                return "long[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                return "double[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new RuntimeException("invalid array element sig: " + sig);
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
    private static int elementSize(byte type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            case T_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            case T_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            case T_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            case T_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            case T_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            case T_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                return 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            case T_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            case T_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                throw new RuntimeException("invalid array element type: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Java primitive array record (HPROF_GC_PRIM_ARRAY_DUMP) looks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * as below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *    object ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *    stack trace serial number (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *    length of the instance data (int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *    element type (byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *    array data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected final int readValueLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JavaClass cl = getClazz();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ReadBuffer buf = cl.getReadBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int idSize = cl.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        long offset = getOffset() + idSize + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // length of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int len = buf.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // typecode of array element type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte type = buf.getByte(offset + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return len * elementSize(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected final byte[] readValue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        JavaClass cl = getClazz();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ReadBuffer buf = cl.getReadBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int idSize = cl.getIdentifierSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        long offset = getOffset() + idSize + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // length of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int length = buf.getInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // typecode of array element type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        byte type = buf.getByte(offset + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return Snapshot.EMPTY_BYTE_ARRAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            length *= elementSize(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            byte[] res = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            buf.get(offset + 5, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // JavaClass set only after resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private JavaClass clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // This field contains elementSignature byte and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // divider to be used to calculate length. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // length of content byte[] is not same as array length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Actual array length is (byte[].length / divider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private int data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // First 8 bits of data is used for element signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static final int SIGNATURE_MASK = 0x0FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Next 8 bits of data is used for length divider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int LENGTH_DIVIDER_MASK = 0x0FF00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // Number of bits to shift to get length divider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final int LENGTH_DIVIDER_SHIFT = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public JavaValueArray(byte elementSignature, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.data = (elementSignature & SIGNATURE_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public JavaClass getClazz() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void visitReferencedObjects(JavaHeapObjectVisitor v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        super.visitReferencedObjects(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void resolve(Snapshot snapshot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (clazz instanceof JavaClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        byte elementSig = getElementType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        clazz = snapshot.findClass(arrayTypeName(elementSig));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (clazz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            clazz = snapshot.getArrayClass("" + ((char) elementSig));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        getClazz().addInstance(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        super.resolve(snapshot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int divider = (data & LENGTH_DIVIDER_MASK) >>> LENGTH_DIVIDER_SHIFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (divider == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            byte elementSignature = getElementType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            switch (elementSignature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                divider = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                divider = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                divider = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                divider = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                throw new RuntimeException("unknown primitive type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                elementSignature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            data |= (divider << LENGTH_DIVIDER_SHIFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return (getValueLength() / divider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public Object getElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final int len = getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        final byte et = getElementType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] data = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        switch (et) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            case 'Z': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                boolean[] res = new boolean[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    res[i] = booleanAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            case 'B': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                byte[] res = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    res[i] = byteAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            case 'C': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                char[] res = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    res[i] = charAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            case 'S': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                short[] res = new short[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    res[i] = shortAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            case 'I': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                int[] res = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    res[i] = intAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    index += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            case 'J': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                long[] res = new long[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    res[i] = longAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    index += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            case 'F': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                float[] res = new float[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    res[i] = floatAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    index += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            case 'D': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                double[] res = new double[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    res[i] = doubleAt(index, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    index += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            default: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new RuntimeException("unknown primitive type?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public byte getElementType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return (byte) (data & SIGNATURE_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private void checkIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (index < 0 || index >= getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private void requireType(char type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (getElementType() != type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new RuntimeException("not of type : " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean getBooleanAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        requireType('Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return booleanAt(index, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public byte getByteAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        requireType('B');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return byteAt(index, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public char getCharAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        requireType('C');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return charAt(index << 1, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public short getShortAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        requireType('S');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return shortAt(index << 1, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public int getIntAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        requireType('I');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return intAt(index << 2, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public long getLongAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        requireType('J');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return longAt(index << 3, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public float getFloatAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        requireType('F');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return floatAt(index << 2, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public double getDoubleAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        checkIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        requireType('D');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return doubleAt(index << 3, getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public String valueString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return valueString(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public String valueString(boolean bigLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // Char arrays deserve special treatment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        StringBuffer result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        byte[] value = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int max = value.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        byte elementSignature = getElementType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (elementSignature == 'C')  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            for (int i = 0; i < value.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                char val = charAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                result.append(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            int limit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (bigLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                limit = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            result = new StringBuffer("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            for (int i = 0; i < value.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (num > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    result.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                if (num >= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    result.append("... ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                num++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                switch (elementSignature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    case 'Z': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        boolean val = booleanAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        if (val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            result.append("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                            result.append("false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    case 'B': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        int val = 0xFF & byteAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        result.append("0x" + Integer.toString(val, 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    case 'S': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        short val = shortAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        result.append("" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    case 'I': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        int val = intAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        i += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        result.append("" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    case 'J': {         // long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                        long val = longAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        result.append("" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        i += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    case 'F': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        float val = floatAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        result.append("" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        i += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    case 'D': {         // double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        double val = doubleAt(i, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        result.append("" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        i += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    default: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        throw new RuntimeException("unknown primitive type?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            result.append("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}