1 /* |
|
2 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 |
|
27 /* |
|
28 * The Original Code is HAT. The Initial Developer of the |
|
29 * Original Code is Bill Foote, with contributions from others |
|
30 * at JavaSoft/Sun. |
|
31 */ |
|
32 |
|
33 package com.sun.tools.hat.internal.parser; |
|
34 |
|
35 import java.io.*; |
|
36 import com.sun.tools.hat.internal.model.*; |
|
37 |
|
38 /** |
|
39 * Abstract base class for reading object dump files. A reader need not be |
|
40 * thread-safe. |
|
41 * |
|
42 * @author Bill Foote |
|
43 */ |
|
44 |
|
45 |
|
46 public abstract class Reader { |
|
47 protected PositionDataInputStream in; |
|
48 |
|
49 protected Reader(PositionDataInputStream in) { |
|
50 this.in = in; |
|
51 } |
|
52 |
|
53 /** |
|
54 * Read a snapshot from a data input stream. It is assumed that the magic |
|
55 * number has already been read. |
|
56 */ |
|
57 abstract public Snapshot read() throws IOException; |
|
58 |
|
59 /** |
|
60 * Read a snapshot from a file. |
|
61 * |
|
62 * @param heapFile The name of a file containing a heap dump |
|
63 * @param callStack If true, read the call stack of allocaation sites |
|
64 */ |
|
65 public static Snapshot readFile(String heapFile, boolean callStack, |
|
66 int debugLevel) |
|
67 throws IOException { |
|
68 int dumpNumber = 1; |
|
69 int pos = heapFile.lastIndexOf('#'); |
|
70 if (pos > -1) { |
|
71 String num = heapFile.substring(pos+1, heapFile.length()); |
|
72 try { |
|
73 dumpNumber = Integer.parseInt(num, 10); |
|
74 } catch (java.lang.NumberFormatException ex) { |
|
75 String msg = "In file name \"" + heapFile |
|
76 + "\", a dump number was " |
|
77 + "expected after the :, but \"" |
|
78 + num + "\" was found instead."; |
|
79 System.err.println(msg); |
|
80 throw new IOException(msg); |
|
81 } |
|
82 heapFile = heapFile.substring(0, pos); |
|
83 } |
|
84 PositionDataInputStream in = new PositionDataInputStream( |
|
85 new BufferedInputStream(new FileInputStream(heapFile))); |
|
86 try { |
|
87 int i = in.readInt(); |
|
88 if (i == HprofReader.MAGIC_NUMBER) { |
|
89 Reader r |
|
90 = new HprofReader(heapFile, in, dumpNumber, |
|
91 callStack, debugLevel); |
|
92 return r.read(); |
|
93 } else { |
|
94 throw new IOException("Unrecognized magic number: " + i); |
|
95 } |
|
96 } finally { |
|
97 in.close(); |
|
98 } |
|
99 } |
|
100 } |
|