title: Reading and parsing stream from FlightRecorderMXBean
body: <p>I use the following code in order to start JFR recording and to save results in file. The file is indeed created, but how to parse it? I thought it should be jfr file type, but jdk.jfr.consumer.RecordingFile fails to parse it (&quot;java.io.IOException: Not a Flight Recorder file&quot;).
  Any help/examples will be appreciated.</p>
  <pre><code>    String host = &quot;localhost&quot;;
      int port = 9999;
      String s = &quot;/jndi/rmi://&quot; + host + &quot;:&quot; + port + &quot;/jmxrmi&quot;;
      JMXServiceURL url = new JMXServiceURL(&quot;rmi&quot;, &quot;&quot;, 0, s);
      JMXConnector jmxConnector = JMXConnectorFactory.newJMXConnector(url, null);
      jmxConnector.connect();
      MBeanServerConnection mbeanConnection = jmxConnector.getMBeanServerConnection();
      FlightRecorderMXBean frb = JMX.newMXBeanProxy(mbeanConnection, new ObjectName(&quot;jdk.management.jfr:type=FlightRecorder&quot;),
              FlightRecorderMXBean.class);
  
      long recId = frb.newRecording();
  
      System.out.println(&quot;RECORDING STARTED&quot;);
      frb.startRecording(recId);
      Thread.sleep(60000);
      frb.stopRecording(recId);
      System.out.println(&quot;RECORDING ENDED&quot;);
              
      long streamId = frb.openStream(recId, null);
  
      FileOutputStream fw = new FileOutputStream(&quot;./jfrstream_&quot; + recId);
      byte[] buff = frb.readStream(streamId);
      while (buff != null)
      {
          buff = frb.readStream(streamId);
          if (buff != null)
          {
              //System.out.println(&quot;buff.length=&quot; + buff.length);
              fw.write(buff);
          }
      }
      frb.closeStream(streamId);
      fw.flush();
      fw.close();
      
  </code></pre>