001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.heap;
042:
043: import java.io.UnsupportedEncodingException;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: /**
048: *
049: * @author Tomas Hurka
050: */
051: class StringSegment extends TagBounds {
052: //~ Instance fields ----------------------------------------------------------------------------------------------------------
053:
054: final int UTF8CharsOffset;
055: final int lengthOffset;
056: final int stringIDOffset;
057: final int timeOffset;
058: Map stringIDMap;
059: private HprofHeap hprofHeap;
060:
061: //~ Constructors -------------------------------------------------------------------------------------------------------------
062:
063: StringSegment(HprofHeap heap, long start, long end) {
064: super (HprofHeap.STRING, start, end);
065:
066: int idSize = heap.dumpBuffer.getIDSize();
067: hprofHeap = heap;
068: timeOffset = 1;
069: lengthOffset = timeOffset + 4;
070: stringIDOffset = lengthOffset + 4;
071: UTF8CharsOffset = stringIDOffset + idSize;
072: }
073:
074: //~ Methods ------------------------------------------------------------------------------------------------------------------
075:
076: String getString(long start) {
077: HprofByteBuffer dumpBuffer = getDumpBuffer();
078:
079: if (start == -1) {
080: return "<unknown string>"; // NOI18N
081: }
082:
083: int len = dumpBuffer.getInt(start + lengthOffset);
084: byte[] chars = new byte[len - dumpBuffer.getIDSize()];
085: dumpBuffer.get(start + UTF8CharsOffset, chars);
086:
087: String s = "Error"; // NOI18N
088:
089: try {
090: s = new String(chars, "UTF-8"); // NOI18N
091: } catch (UnsupportedEncodingException ex) {
092: ex.printStackTrace();
093: }
094:
095: return s;
096: }
097:
098: String getStringByID(long stringID) {
099: return getString(getStringOffsetByID(stringID));
100: }
101:
102: long getStringOffsetByID(long stringID) {
103: Long startLong;
104:
105: if (stringIDMap == null) {
106: stringIDMap = new HashMap(8000);
107:
108: long[] offset = new long[] { startOffset };
109:
110: while (offset[0] < endOffset) {
111: long start = offset[0];
112: long sID = readStringTag(offset);
113: stringIDMap.put(new Long(sID), new Long(start));
114: }
115: }
116:
117: startLong = (Long) stringIDMap.get(new Long(stringID));
118:
119: if (startLong == null) {
120: return -1;
121: }
122:
123: return startLong.longValue();
124: }
125:
126: private HprofByteBuffer getDumpBuffer() {
127: HprofByteBuffer dumpBuffer = hprofHeap.dumpBuffer;
128:
129: return dumpBuffer;
130: }
131:
132: private long readStringTag(long[] offset) {
133: long start = offset[0];
134:
135: if (hprofHeap.readTag(offset) != HprofHeap.STRING) {
136: return 0;
137: }
138:
139: return getDumpBuffer().getID(start + stringIDOffset);
140: }
141: }
|