< prev index next >

test/lib/jdk/test/lib/hprof/parser/Reader.java

Print this page
rev 58388 : 8237354: Add option to jcmd to write a gzipped heap dump
Reviewed-by:
   1 /*
   2  * Copyright (c) 1997, 2017, 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


  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         try (PositionDataInputStream in = new PositionDataInputStream(
  85                 new BufferedInputStream(new FileInputStream(heapFile)))) {
  86             int i = in.readInt();
  87             if (i == HprofReader.MAGIC_NUMBER) {
  88                 Reader r
  89                     = new HprofReader(heapFile, in, dumpNumber,
  90                                       callStack, debugLevel);
  91                 return r.read();














  92             } else {
  93                 throw new IOException("Unrecognized magic number: " + i);
  94             }
  95         }
  96     }
  97 
  98     /**
  99      * Get Stack Traces from a Hprof file.
 100      *
 101      * @param heapFile The name of a file containing a heap dump
 102      */
 103     public static String getStack(String heapFile, int debugLevel)
 104             throws IOException {
 105         int dumpNumber = 1;
 106         int pos = heapFile.lastIndexOf('#');
 107         if (pos > -1) {
 108             String num = heapFile.substring(pos+1, heapFile.length());
 109             try {
 110                 dumpNumber = Integer.parseInt(num, 10);
 111             } catch (java.lang.NumberFormatException ex) {


   1 /*
   2  * Copyright (c) 1997, 2020, 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


  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         GzipRandomAccess access = null;
  85         try (PositionDataInputStream in = new PositionDataInputStream(
  86                 new BufferedInputStream(new FileInputStream(heapFile)))) {
  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 if ((access = GzipRandomAccess.getAccess(heapFile, 16)) != null) {
  94                 in.close();
  95                 try (PositionDataInputStream in2 = new PositionDataInputStream(
  96                         new BufferedInputStream(access.asStream(0)))) {
  97                     i = in2.readInt();
  98                     if (i == HprofReader.MAGIC_NUMBER) {
  99                         Reader r
 100                             = new HprofReader(access.asFileBuffer(), in2, dumpNumber,
 101                                               callStack, debugLevel);
 102                         return r.read();
 103                     } else {
 104                          throw new IOException("Unrecognized magic number in gzipped file: " + i);
 105                     }
 106                 }
 107             } else {
 108                 throw new IOException("Unrecognized magic number: " + i);
 109             }
 110         }
 111     }
 112 
 113     /**
 114      * Get Stack Traces from a Hprof file.
 115      *
 116      * @param heapFile The name of a file containing a heap dump
 117      */
 118     public static String getStack(String heapFile, int debugLevel)
 119             throws IOException {
 120         int dumpNumber = 1;
 121         int pos = heapFile.lastIndexOf('#');
 122         if (pos > -1) {
 123             String num = heapFile.substring(pos+1, heapFile.length());
 124             try {
 125                 dumpNumber = Integer.parseInt(num, 10);
 126             } catch (java.lang.NumberFormatException ex) {


< prev index next >