Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package jfr_example;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.util.zip.GZIPInputStream;
  10.  
  11. import java.util.Iterator;
  12.  
  13. import oracle.jrockit.jfr.parser.*;
  14.  
  15. public class Jdk8JfrReader {
  16. @SuppressWarnings({ "deprecation", "restriction" })
  17. public Path read(File file) throws IOException {
  18. if (!file.exists() || !file.getName().endsWith(".jfr")) {
  19. return null;
  20. }
  21. File jfr = null;
  22. if (isGzipCompressed(file)) {
  23. jfr = decompress(file);
  24. } else {
  25. jfr = file;
  26. }
  27. Path dump = Files.createTempFile("dump_temp_", null);
  28.  
  29. try (Parser parser = new Parser(jfr)) {
  30. Iterator<ChunkParser> chunkIter = parser.iterator();
  31. while (chunkIter.hasNext()) {
  32. // ChunkParser chunkParser = chunkIter.next();
  33. // for (FLREvent event : chunkParser) {
  34. // System.out.println(event);
  35. // }
  36. }
  37. }
  38. return dump;
  39. }
  40.  
  41. private File decompress(final File srcFile) throws IOException {
  42. byte[] buffer = new byte[1024];
  43. File target = File.createTempFile("flightrecorder_", null);
  44. target.deleteOnExit();
  45.  
  46. try (FileOutputStream os = new FileOutputStream(target);
  47. GZIPInputStream zipIs = new GZIPInputStream(new FileInputStream(srcFile))) {
  48. int bytes;
  49. while ((bytes = zipIs.read(buffer)) > 0) {
  50. os.write(buffer, 0, bytes);
  51. }
  52. }
  53. return target;
  54. }
  55.  
  56. private boolean isGzipCompressed(File file) throws IOException {
  57. try (FileInputStream ins = new FileInputStream(file)) {
  58. return (byte) ins.read() == (byte) GZIPInputStream.GZIP_MAGIC
  59. && (byte) ins.read() == (byte) (GZIPInputStream.GZIP_MAGIC >> 8);
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment