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.EOFException;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.RandomAccessFile;
047:
048: /**
049: *
050: * @author Tomas Hurka
051: */
052: class HprofFileBuffer extends HprofByteBuffer {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: private static final int MAX_bufferSizeBits = 17;
056: private static final int MIN_bufferSizeBits = 7;
057: private static final int MIN_bufferSize = 1 << MIN_bufferSizeBits;
058: private static final int MIN_bufferSizeMask = MIN_bufferSize - 1;
059: private static final int BUFFER_EXT = 8;
060: private static int bufferSizeBits;
061: private static int bufferSize;
062:
063: //~ Instance fields ----------------------------------------------------------------------------------------------------------
064:
065: RandomAccessFile fis;
066: private byte[] dumpBuffer;
067: private long bufferStartOffset;
068:
069: //~ Constructors -------------------------------------------------------------------------------------------------------------
070:
071: HprofFileBuffer(File dumpFile) throws IOException {
072: fis = new RandomAccessFile(dumpFile, "r");
073: length = fis.length();
074: bufferStartOffset = Long.MAX_VALUE;
075: readHeader();
076: }
077:
078: //~ Methods ------------------------------------------------------------------------------------------------------------------
079:
080: synchronized char getChar(long index) {
081: int i = loadBufferIfNeeded(index);
082: int ch1 = ((int) dumpBuffer[i++]) & 0xFF;
083: int ch2 = ((int) dumpBuffer[i]) & 0xFF;
084:
085: return (char) ((ch1 << 8) + (ch2 << 0));
086: }
087:
088: synchronized double getDouble(long index) {
089: int i = loadBufferIfNeeded(index);
090:
091: return Double.longBitsToDouble(getLong(i));
092: }
093:
094: synchronized float getFloat(long index) {
095: int i = loadBufferIfNeeded(index);
096:
097: return Float.intBitsToFloat(getInt(i));
098: }
099:
100: synchronized int getInt(long index) {
101: int i = loadBufferIfNeeded(index);
102: int ch1 = ((int) dumpBuffer[i++]) & 0xFF;
103: int ch2 = ((int) dumpBuffer[i++]) & 0xFF;
104: int ch3 = ((int) dumpBuffer[i++]) & 0xFF;
105: int ch4 = ((int) dumpBuffer[i]) & 0xFF;
106:
107: return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
108: }
109:
110: synchronized long getLong(long index) {
111: return ((long) (getInt(index)) << 32)
112: + (getInt(index + 4) & 0xFFFFFFFFL);
113: }
114:
115: synchronized short getShort(long index) {
116: int i = loadBufferIfNeeded(index);
117: int ch1 = ((int) dumpBuffer[i++]) & 0xFF;
118: int ch2 = ((int) dumpBuffer[i]) & 0xFF;
119:
120: return (short) ((ch1 << 8) + (ch2 << 0));
121: }
122:
123: // delegate to MappedByteBuffer
124: synchronized byte get(long index) {
125: int i = loadBufferIfNeeded(index);
126:
127: return dumpBuffer[i];
128: }
129:
130: synchronized void get(long position, byte[] chars) {
131: int i = loadBufferIfNeeded(position);
132:
133: if ((i + chars.length) < dumpBuffer.length) {
134: System.arraycopy(dumpBuffer, i, chars, 0, chars.length);
135: } else {
136: try {
137: fis.seek(position);
138: fis.readFully(chars);
139: } catch (IOException ex) {
140: ex.printStackTrace();
141: }
142: }
143: }
144:
145: private void setBufferSize(long newBufferStart) {
146: if ((newBufferStart > bufferStartOffset)
147: && (newBufferStart < (bufferStartOffset + (2 * bufferSize)))) { // sequential read -> increase buffer size
148:
149: if (bufferSizeBits < MAX_bufferSizeBits) {
150: setBufferSize(bufferSizeBits + 1);
151: }
152: } else { // reset buffer size
153: setBufferSize(MIN_bufferSizeBits);
154: }
155: }
156:
157: private void setBufferSize(int newBufferSizeBits) {
158: bufferSizeBits = newBufferSizeBits;
159: bufferSize = 1 << bufferSizeBits;
160: dumpBuffer = new byte[bufferSize + BUFFER_EXT];
161: }
162:
163: private int loadBufferIfNeeded(long index) {
164: if ((index >= bufferStartOffset)
165: && (index < (bufferStartOffset + bufferSize))) {
166: return (int) (index - bufferStartOffset);
167: }
168:
169: long newBufferStart = index & ~MIN_bufferSizeMask;
170: setBufferSize(newBufferStart);
171:
172: try {
173: fis.seek(newBufferStart);
174: fis.readFully(dumpBuffer);
175:
176: //System.out.println("Reading at "+newBufferStart+" size "+dumpBuffer.length+" thread "+Thread.currentThread().getName());
177: } catch (EOFException ex) {
178: // ignore
179: } catch (IOException ex) {
180: ex.printStackTrace();
181: }
182:
183: bufferStartOffset = newBufferStart;
184:
185: return (int) (index - bufferStartOffset);
186: }
187: }
|