title: How to solve: java.io.IOException: Stream closed
body: <p>This is a program to parse a JSON structure in a streaming manner.
  It is 10 PM here, my brain is tired,  and that is why I seem to be not able to figure out why I am getting the exception:
     java.io.Exception: Stream closed.</p>
  
  <p>If the experts out here can help me get rid of this exception, I will be thankful for helping me solve this problem at this time of the night.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.*;
  
  import com.fasterxml.jackson.core.*;
  import com.fasterxml.jackson.databind.*;
  
  import java.util.Properties;
  
  public class JacksonStreaming {
  
      public static void main(String[] args) {
          System.out.println("Entered Main");
          try {
              new JacksonStreaming().getNames();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  ObjectMapper jsonMapper = new ObjectMapper();
  JsonFactory jsonFactory = new JsonFactory();
  
  Properties prop = new Properties();
  String filePath = "";
  final String[] path = new String[] {"myReport", "docReports", "part1/.", "myAnalysis", "matches", "name"};
  
  void getNames()  {
      System.out.println("Entered getNames"); 
      //final String resourceName = "C:/Users/Ilango/ellyworkspace/JacksonStreamingFindData/src/jsonFormattedModified.json";
      JsonNode rootNode;
  
  
              try {
                  //InputStream in = getClass().getClassLoader().getResourceAsStream("config.properties");
                  BufferedReader fileReader = new BufferedReader(new FileReader("C:/Users/Ilango/ellyworkspace/JacksonStreamingFindData/src/jsonFormattedModified.json"));
                  System.out.println("fileReader is: " + fileReader); 
                  rootNode = jsonMapper.readTree(fileReader);
                  System.out.println("Return value of jsonMapper.readTree is: " +rootNode);
                  findByPath(rootNode);
                  JsonParser jsonParser = jsonFactory.createParser(fileReader);
                  System.out.println("JsonParser is: " + jsonParser);
  
                  int pathIndex = 0;
                  List&lt;String&gt; names = new ArrayList&lt;String&gt;();
                  boolean breakOnClose = false;
  
  
                  //try {
                      //try {
                          while (jsonParser.nextToken() != null) {
                              final String fieldName = jsonParser.getCurrentName();
                              if (fieldName == null) {
                                  continue;
                              }
                              if (breakOnClose &amp;&amp; fieldName.equals(path[path.length - 2])) {
                                  System.out.println("Stopping search at end of node " + fieldName);
                                  break;
                              }
                              if (jsonParser.getCurrentToken() != JsonToken.FIELD_NAME) {
                                  continue;
                              }
                              // System.out.println("Field " + fieldName);
                              if (pathIndex &gt;= path.length - 1) {
                                  if (fieldName.equals(path[path.length - 1])) {
                                      // move from field name to field value.
                                      try {
                                          jsonParser.nextToken();
                                      } catch (IOException e) {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      } 
                                      String name;
                                      //try {
                                          name = jsonParser.getValueAsString();
                                      //} catch (IOException e) {
                                          // TODO Auto-generated catch block
                                          //e.printStackTrace();
                                      //}
                                      if (name == null) {
                                          throw new RuntimeException("No value exists for field " + fieldName);
                                      }
                                      names.add(name);
                                      System.out.println("Found " + fieldName + " value: " + name);
                                  }
                              } else if (fieldName.equals(path[pathIndex])) {
                                  System.out.println("Found node " + path[pathIndex]);
                                  pathIndex++;
                                  if (pathIndex &gt;= path.length - 1) {
                                      System.out.println("Looking for names ...");
                                      breakOnClose = true;
                                      // prevent breaking on "matches" value json-token.
                                      try {
                                          jsonParser.nextFieldName();
                                      } catch (IOException e) {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                      } 
                                  }
                              }
                          }
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } 
  
              } 
  
  
  
  
  
  
              void findByPath(JsonNode jn) {
                  System.out.println("Entered findByPath");   
                  JsonNode matchesNamesNode = jn;
                  for (int i = 0; i &lt; path.length - 1; i++) {
                      matchesNamesNode = matchesNamesNode.path(path[i]);
                  }
                  if (matchesNamesNode.isMissingNode()) {
                      throw new RuntimeException("No node with names found.");
                  }
                  System.out.println("Tree names: " + matchesNamesNode.findValuesAsText("name"));
              }
  
  
       }// end of class JacksonStreaming
        def setCount(count: Int) {
          this.count = count
        }
      }
  </code></pre>
  
  <p><strong>Stack trace:</strong></p>
  
  <pre><code>Entered Main
  Entered getNames
  fileReader is: java.io.BufferedReader@33e5ccce
  Return value of jsonMapper.readTree is: {"type":"ImportantIncidentInfo","incidentTimestamp":"2014-05-15T10:09:27.989-05:00","numOfMatches":4,"myReport":{"docReports":{"part1/.":{"path":["unknown"],"myAnalysis":{"matches":[{"id":{"major":1,"minor":0},"name":"US SSN","position":13,"string":" 636-12-4567 "},{"id":{"major":3,"minor":0},"name":"MasterCard Credit Card Number","position":35,"string":" 5424-1813-6924-3685 "}]},"cleanedUpData":[{"startPosition":0,"endPosition":65,"frameContent":""}],"minedMetadata":{"Content-Encoding":"ISO-8859-1","Content-Type":"text/html; charset=iso-8859-1"},"deducedMetadata":{"Content-Type":"text/html; iso-8859-1"}},"part2/.":{"path":["unknown"],"myAnalysis":{"matches":[{"id":{"major":1,"minor":0},"name":"SSN","position":3,"string":" 636-12-4567\r"},{"id":{"major":3,"minor":0},"name":"MasterCard Credit Card Number","position":18,"string":"\n5424-1813-6924-3685\r"}]},"cleanedUpData":[{"startPosition":0,"endPosition":44,"frameContent":""}],"minedMetadata":{"Content-Encoding":"windows-1252","Content-Type":"text/plain; charset=windows-1252"},"deducedMetadata":{"Content-Type":"text/plain; iso-8859-1"}}}},"whatSetItOffEntry":{"action":"Log","component":{"type":"aComponent","components":[{"type":"PatternComponent","patterns":[1],"not":false}],"not":false},"ticketInfo":{"createIncident":true,"tags":[],"seeRestrictedIds":[{"type":"userGroup","name":"SiteMasters","description":"Group for SiteMasters","masters":["04fb02a2bc0fba"],"members":[],"id":"04fade"}]},"letmeknowInfo":{"createNotification":true,"contactNames":["someguy@gmail.com"]}},"seeRestrictedIds":["04fade66c0"],"status":"New","timeStamps":["2015-05-15T10:09:27.989-05:00"],"count":1}
  Entered findByPath
  Tree names: [US SSN, MasterCard Credit Card Number]
  JsonParser is: com.fasterxml.jackson.core.json.ReaderBasedJsonParser@23223dd8
  java.io.IOException: Stream closed
      at java.io.BufferedReader.ensureOpen(BufferedReader.java:122)
      at java.io.BufferedReader.read(BufferedReader.java:278)
      at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.loadMore(ReaderBasedJsonParser.java:153)
      at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipWSOrEnd(ReaderBasedJsonParser.java:1855)
      at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:571)
      at com.myorg.myapi.JacksonStreaming.getNames(JacksonStreaming.java:55)
      at com.myorg.myapi.JacksonStreaming.main(JacksonStreaming.java:19)
  </code></pre>