title: Camel - Stream cache not caching / can't convert?
body: <p>I seem to be losing my 'in' body after reading it once.  Note that I am using Camel's stream caching, and that the input is a json file from the http component.   I have a processor with the following code. </p>
  
  <pre><code>    log.debug("Body Type: " + exchange.getIn().getBody().getClass().getCanonicalName());
      log.debug("In msg1:"  + exchange.getIn().getBody(String.class));
      log.debug("In msg2:"  + exchange.getIn().getBody(String.class));
  </code></pre>
  
  <p>What I'd expect to see here is that msg1 and msg2 are the same output, However msg2 returns a blank string (not null).  Here are the logs at TRACE level.</p>
  
  <pre><code>1- DEBUG com.mycompany.MyProcessor : Body Type: org.apache.camel.converter.stream.InputStreamCache
  2- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Converting org.apache.camel.converter.stream.InputStreamCache -&gt; java.lang.String with value: org.apache.camel.converter.stream.InputStreamCache@780a5cef
  3- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Using converter: StaticMethodTypeConverter: public static java.lang.String org.apache.camel.converter.IOConverter.toString(java.io.InputStream,org.apache.camel.Exchange) throws java.io.IOException to convert [class org.apache.camel.converter.stream.InputStreamCache=&gt;class java.lang.String]
  4- DEBUG com.mycompany.MyProcessor : In msg1:{myJson}
  5- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Converting org.apache.camel.converter.stream.InputStreamCache -&gt; java.lang.String with value: org.apache.camel.converter.stream.InputStreamCache@780a5cef
  6- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Using converter: StaticMethodTypeConverter: public static java.lang.String org.apache.camel.converter.IOConverter.toString(java.io.InputStream,org.apache.camel.Exchange) throws java.io.IOException to convert [class org.apache.camel.converter.stream.InputStreamCache=&gt;class java.lang.String]
  7- DEBUG com.mycompany.MyProcessor : In msg2:
  </code></pre>
  
  <p>Things to note from the logs:</p>
  
  <ul>
  <li>Line 1- The Body Type is correctly showing a cached input stream</li>
  <li>Line 4- Converting to String <em>does work</em> to produce msg1, even though line 3, the conversion code, seems to fail with an IOException</li>
  <li>Line 6- Also failing the conversion but it's important to note that the body is still a cached stream.</li>
  <li>Line 7- My message is lost.</li>
  </ul>
  
  <p>So where did msg2 go?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Some things to mention in addition to Peter's answer below:</p>
  
  <p>Camel's MessageHelper static class has two useful functions:</p>
  
  <ul>
  <li><code>resetStreamCache</code></li>
  <li><code>extractBodyAsString</code></li>
  </ul>
  
  <p>Both of which will help for this situation</p>