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.BufferedInputStream;
044: import java.io.BufferedOutputStream;
045: import java.io.DataInputStream;
046: import java.io.DataOutputStream;
047: import java.io.EOFException;
048: import java.io.File;
049: import java.io.FileInputStream;
050: import java.io.FileNotFoundException;
051: import java.io.FileOutputStream;
052: import java.io.IOException;
053:
054: /**
055: * LongBuffer is a special kind of buffer for storing longs. It uses array of longs if there is only few longs
056: * stored, otherwise longs are saved to backing temporary file.
057: * @author Tomas Hurka
058: */
059: class LongBuffer {
060: //~ Instance fields ----------------------------------------------------------------------------------------------------------
061:
062: private DataInputStream readStream;
063: private DataOutputStream writeStream;
064: private File backedFile;
065: private long[] buffer;
066: private boolean hasData;
067: private boolean useBackingFile;
068: private int bufferSize;
069: private int readOffset;
070:
071: //~ Constructors -------------------------------------------------------------------------------------------------------------
072:
073: LongBuffer(int size) {
074: buffer = new long[size];
075: }
076:
077: //~ Methods ------------------------------------------------------------------------------------------------------------------
078:
079: void delete() {
080: if (backedFile != null) {
081: backedFile.delete();
082: }
083: }
084:
085: boolean hasData() {
086: return hasData;
087: }
088:
089: long readLong() throws IOException {
090: if (!useBackingFile) {
091: if (readOffset < bufferSize) {
092: return buffer[readOffset++];
093: } else {
094: return 0;
095: }
096: }
097:
098: try {
099: return readStream.readLong();
100: } catch (EOFException ex) {
101: return 0L;
102: }
103: }
104:
105: void reset() {
106: bufferSize = 0;
107: writeStream = null;
108: readStream = null;
109: hasData = false;
110: useBackingFile = false;
111: readOffset = 0;
112: }
113:
114: void startReading() {
115: if (useBackingFile) {
116: try {
117: writeStream.close();
118: } catch (IOException ex) {
119: ex.printStackTrace();
120: }
121: }
122:
123: writeStream = null;
124: readOffset = 0;
125:
126: if (useBackingFile) {
127: try {
128: readStream = new DataInputStream(
129: new BufferedInputStream(new FileInputStream(
130: backedFile), buffer.length * 8));
131: } catch (FileNotFoundException ex) {
132: ex.printStackTrace();
133: }
134: }
135: }
136:
137: void writeLong(long data) throws IOException {
138: hasData = true;
139:
140: if (bufferSize < buffer.length) {
141: buffer[bufferSize++] = data;
142:
143: return;
144: }
145:
146: if (backedFile == null) {
147: backedFile = File.createTempFile("NBProfiler", ".gc"); // NOI18N
148: }
149:
150: if (writeStream == null) {
151: writeStream = new DataOutputStream(
152: new BufferedOutputStream(new FileOutputStream(
153: backedFile), buffer.length * 8));
154:
155: for (int i = 0; i < buffer.length; i++) {
156: writeStream.writeLong(buffer[i]);
157: }
158:
159: useBackingFile = true;
160: }
161:
162: writeStream.writeLong(data);
163: }
164: }
|