title: Spring Integration: Custom Splitter with Header Enrichment
body: <p>Is it possible to have an implementation of a message splitter that can return an <code>Iterator</code> <em>AND</em> add custom header information?</p>
  
  <p>For instance if I have the following class</p>
  
  <pre><code>public class CsvFileToIteratorSplitter extends AbstractMessageSplitter {
  
      @Override
      protected Object splitMessage(Message&lt;?&gt; message) {
          Object payload = message.getPayload();
          Assert.isInstanceOf(File.class, payload, "Expected java.io.File in the message payload");
  
          try {
              InputStream source = new FileInputStream((File) payload);
              BufferedReader reader = new BufferedReader(new InputStreamReader(source));
  
              String header = reader.lines().findFirst().orElse(null);
  
              return MessageBuilder.withPayload(reader.lines().iterator())
                      .setHeaderIfAbsent("HEADER", header)
                      .build();
  
          } catch (IOException e) {
              throw new UncheckedIOException(e);
          }
      }
  }
  </code></pre>
  
  <p>Then I can add to the header but the payload is actually an instance of <code>Iterator</code> and the split fails</p>
  
  <p>If I modify so that the class is now</p>
  
  <pre><code>public class CsvFileToIteratorSplitter extends AbstractMessageSplitter {
  
      @Override
      protected Object splitMessage(Message&lt;?&gt; message) {
          log.debug("{}", message.toString());
  
          Object payload = message.getPayload();
          Assert.isInstanceOf(File.class, payload, "Expected java.io.File in the message payload");
  
          try {
              InputStream source = new FileInputStream((File) payload);
              BufferedReader reader = new BufferedReader(new InputStreamReader(source));
  
              return reader.lines().iterator();
  
          } catch (IOException e) {
              throw new UncheckedIOException(e);
          }
      }
  }
  </code></pre>
  
  <p>The split works but I lose the header info.</p>
  
  <p>Is there any way to have a functioning split with the ability to add to the header?</p>