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.File;
044: import java.io.IOException;
045: import java.util.Date;
046: import java.util.ResourceBundle;
047:
048: /**
049: *
050: * @author Tomas Hurka
051: */
052: abstract class HprofByteBuffer {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: // Magic header
056: static final String magic1 = "JAVA PROFILE 1.0.1"; // NOI18N
057: static final String magic2 = "JAVA PROFILE 1.0.2"; // NOI18N
058: static final int JAVA_PROFILE_1_0_1 = 1;
059: static final int JAVA_PROFILE_1_0_2 = 2;
060: static final int MINIMAL_SIZE = 30;
061: static final boolean DEBUG = false;
062:
063: //~ Instance fields ----------------------------------------------------------------------------------------------------------
064:
065: int idSize;
066: int version;
067: long headerSize;
068: long length;
069: long time;
070:
071: //~ Methods ------------------------------------------------------------------------------------------------------------------
072:
073: static HprofByteBuffer createHprofByteBuffer(File dumpFile)
074: throws IOException {
075: long fileLen = dumpFile.length();
076:
077: if (fileLen < MINIMAL_SIZE) {
078: String errText = ResourceBundle.getBundle(
079: "org/netbeans/lib/profiler/heap/Bundle").getString(
080: "HprofByteBuffer_ShortFile"); // NOI18N
081: throw new IOException(errText);
082: }
083:
084: try {
085: if (fileLen < Integer.MAX_VALUE) {
086: return new HprofMappedByteBuffer(dumpFile);
087: } else {
088: return new HprofLongMappedByteBuffer(dumpFile);
089: }
090: } catch (IOException ex) {
091: if (ex.getCause() instanceof OutOfMemoryError) { // can happen on 32bit Windows, since there is only 2G for memory mapped data for whole java process.
092:
093: return new HprofFileBuffer(dumpFile);
094: }
095:
096: throw ex;
097: }
098: }
099:
100: abstract char getChar(long index);
101:
102: abstract double getDouble(long index);
103:
104: abstract float getFloat(long index);
105:
106: long getHeaderSize() {
107: return headerSize;
108: }
109:
110: long getID(long offset) {
111: if (idSize == 4) {
112: return getInt(offset);
113: } else if (idSize == 8) {
114: return getLong(offset);
115: }
116: assert false;
117:
118: return -1;
119: }
120:
121: int getIDSize() {
122: return idSize;
123: }
124:
125: int getFoffsetSize() {
126: return length < Integer.MAX_VALUE ? 4 : 8;
127: }
128:
129: abstract int getInt(long index);
130:
131: abstract long getLong(long index);
132:
133: abstract short getShort(long index);
134:
135: long getTime() {
136: return time;
137: }
138:
139: long capacity() {
140: return length;
141: }
142:
143: abstract byte get(long index);
144:
145: abstract void get(long position, byte[] chars);
146:
147: void readHeader() throws IOException {
148: long[] offset = new long[1];
149: String magic = readStringNull(offset, MINIMAL_SIZE);
150:
151: if (DEBUG) {
152: System.out.println("Magic " + magic); // NOI18N
153: }
154:
155: if (magic1.equals(magic)) {
156: version = JAVA_PROFILE_1_0_1;
157: } else if (magic2.equals(magic)) {
158: version = JAVA_PROFILE_1_0_2;
159: } else {
160: if (DEBUG) {
161: System.out.println("Invalid version"); // NOI18N
162: }
163:
164: String errText = ResourceBundle.getBundle(
165: "org/netbeans/lib/profiler/heap/Bundle").getString(
166: "HprofByteBuffer_InvalidFormat");
167: throw new IOException(errText);
168: }
169:
170: idSize = getInt(offset[0]);
171: offset[0] += 4;
172: time = getLong(offset[0]);
173: offset[0] += 8;
174:
175: if (DEBUG) {
176: System.out.println("ID " + idSize); // NOI18N
177: }
178:
179: if (DEBUG) {
180: System.out.println("Date " + new Date(time).toString()); // NOI18N
181: }
182:
183: headerSize = offset[0];
184: }
185:
186: private String readStringNull(long[] offset, int len) {
187: StringBuffer s = new StringBuffer(20);
188: byte b = get(offset[0]++);
189:
190: for (; (b > 0) && (s.length() < len); b = get(offset[0]++)) {
191: s.append((char) b);
192: }
193:
194: return s.toString();
195: }
196: }
|