Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jfr_example;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.zip.GZIPInputStream;
- import java.util.Iterator;
- import oracle.jrockit.jfr.parser.*;
- public class Jdk8JfrReader {
- @SuppressWarnings({ "deprecation", "restriction" })
- public Path read(File file) throws IOException {
- if (!file.exists() || !file.getName().endsWith(".jfr")) {
- return null;
- }
- File jfr = null;
- if (isGzipCompressed(file)) {
- jfr = decompress(file);
- } else {
- jfr = file;
- }
- Path dump = Files.createTempFile("dump_temp_", null);
- try (Parser parser = new Parser(jfr)) {
- Iterator<ChunkParser> chunkIter = parser.iterator();
- while (chunkIter.hasNext()) {
- // ChunkParser chunkParser = chunkIter.next();
- // for (FLREvent event : chunkParser) {
- // System.out.println(event);
- // }
- }
- }
- return dump;
- }
- private File decompress(final File srcFile) throws IOException {
- byte[] buffer = new byte[1024];
- File target = File.createTempFile("flightrecorder_", null);
- target.deleteOnExit();
- try (FileOutputStream os = new FileOutputStream(target);
- GZIPInputStream zipIs = new GZIPInputStream(new FileInputStream(srcFile))) {
- int bytes;
- while ((bytes = zipIs.read(buffer)) > 0) {
- os.write(buffer, 0, bytes);
- }
- }
- return target;
- }
- private boolean isGzipCompressed(File file) throws IOException {
- try (FileInputStream ins = new FileInputStream(file)) {
- return (byte) ins.read() == (byte) GZIPInputStream.GZIP_MAGIC
- && (byte) ins.read() == (byte) (GZIPInputStream.GZIP_MAGIC >> 8);
- }
- }
- }
Add Comment
Please, Sign In to add comment