title: Initialize FileInputStream in JMH @Setup and use in @Benchmark
body: <p><code>InputStream</code> initiliazed in <code>@Setup</code>, when trying to use it in <code>@Benchmark</code> its closed. Changing <code>@State</code> doesn't work. Am I doing this correctly? Is there are way to avoid stream initialization overhead and do proper benchmark?</p>
  
  <p>This doesn't work for <code>XMLStreamReader</code> also, I am doing a serialization deserialization benchmark where I want to avoid file read/stream initialization cost into benchmark score.</p>
  
  <p>sample benchmark</p>
  
  <pre><code>@State( Scope.Thread )
  public class DeserializationBenchMark
  {
      @Param( { "1.xml", "10.xml", "100.xml" } )
      String file;
  
      private InputStream xmlFileInputStream;
  
      @Setup
      public void setup() throws JAXBException, IOException, SAXException, XMLStreamException
      {
          File xmlFile = new File( "src/main/resources/" + file );
          xmlFileInputStream = Files.newInputStream( xmlFile.toPath() );
      }
  
      @Benchmark
      public void jacksonDeserializeStreamTest( Blackhole bh ) throws IOException
      {
          bh.consume( objectMapper.readValue( xmlFileInputStream, Cat.class ) );
      }
  
  }
  </code></pre>
  
  <p>runner</p>
  
  <pre><code>public class BenchMarkRunner
  {
      public static void main( String[] args ) throws RunnerException
      {
          Options opt = new OptionsBuilder()
                                .include( DeserializationBenchMark.class.getSimpleName() )
                                .forks( 1 )
                                .resultFormat( ResultFormatType.JSON )
                                .result( "deserialize-benchmark-report-" + LocalDateTime.now().format( DateTimeFormatter.ofPattern( "ddMMyyyy'T'hhmmss" ) ) + ".json" )
                                .mode( Mode.AverageTime )
                                .warmupIterations( 3 )
                                .measurementIterations( 5 )
                                .timeUnit( TimeUnit.MICROSECONDS )
                                .build();
  
          new Runner( opt ).run();
      }
  }
  </code></pre>
  
  <p>This gives</p>
  
  <pre><code>com.fasterxml.jackson.core.JsonParseException: N/A
      at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsParseException(StaxUtil.java:37)
      at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:534)
      at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:29)
      at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:820)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3058)
      at com.example.DeserializationBenchMark.jacksonDeserializeStreamTest(DeserializationBenchMark.java:118)
      at com.example.generated.DeserializationBenchMark_jacksonDeserializeStreamTest_jmhTest.jacksonDeserializeStreamTest_avgt_jmhStub(DeserializationBenchMark_jacksonDeserializeStreamTest_jmhTest.java:186)
      at com.example.generated.DeserializationBenchMark_jacksonDeserializeStreamTest_jmhTest.jacksonDeserializeStreamTest_AverageTime(DeserializationBenchMark_jacksonDeserializeStreamTest_jmhTest.java:150)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:453)
      at org.openjdk.jmh.runner.BenchmarkHandler$BenchmarkTask.call(BenchmarkHandler.java:437)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.nio.channels.ClosedChannelException
      at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
      at sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:147)
      at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
      at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
      at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
      at com.ctc.wstx.io.StreamBootstrapper.ensureLoaded(StreamBootstrapper.java:482)
      at com.ctc.wstx.io.StreamBootstrapper.resolveStreamEncoding(StreamBootstrapper.java:306)
      at com.ctc.wstx.io.StreamBootstrapper.bootstrapInput(StreamBootstrapper.java:167)
      at com.ctc.wstx.stax.WstxInputFactory.doCreateSR(WstxInputFactory.java:573)
      at com.ctc.wstx.stax.WstxInputFactory.createSR(WstxInputFactory.java:633)
      at com.ctc.wstx.stax.WstxInputFactory.createSR(WstxInputFactory.java:647)
      at com.ctc.wstx.stax.WstxInputFactory.createXMLStreamReader(WstxInputFactory.java:334)
      at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:532)
      ... 18 more
  </code></pre>
  
  <p>Any suggestion would be great! Thank you.</p>
  
  <p>PS:</p>
  
  <p>As @dit mentioned It should be stream close, any idea why it happens at first iteration, </p>
  
  <pre><code># Run progress: 0.00% complete, ETA 00:07:30
  # Fork: 1 of 1
  # Warmup Iteration   1: &lt;failure&gt;
  
  com.fasterxml.jackson.core.JsonParseException: N/A
      at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsParseException(StaxUtil.java:37)
      at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:534)
      at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:29)
      at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:820)
  </code></pre>