title: How to write an nt:file programmatically
body: <p>I'm trying to store binary data in JCR, which is created on the fly. My problem is that the only way provided by the JCR API is via an <code>InputStream</code>:</p>
  
  <pre><code>Session session = request.getResourceResolver().adaptTo(Session.class);
  ValueFactory valueFactory = session.getValueFactory();
  Binary bin = valueFactory.createBinary(is);
  </code></pre>
  
  <p>As CQ/Sling is RESTful I can see why this is the case as you usually get a form post or an httprequest to another source, where you always have an <code>InputStream</code> to use. But in my case I am creating the binary on the fly which usually is represented as an <code>OutputStream</code>.</p>
  
  <ol>
  <li>Is there any other way I overlooked in the JCR API where I could create an <code>OutputStream</code> directly on the nt:file node, just like a <code>FileOutputStream</code>?</li>
  <li>If no, is there an easy way to have an <code>OutpuStream</code> transformed to an <code>InputStream</code>?</li>
  </ol>
  
  <p>I know the other way is available from the Apache Commons <code>IOUtils.copy()</code>. I've seen some examples on SO where they just use the <code>ByteArrayOutputStream.toByteArray()</code> to create an <code>InputStream</code>. But as the data could get rather large, this is not a good solution. Besides I tried it and somehow the stream was incomplete, so it seems there is a buffer limmit. The next approach was with piped streams, but there I have other problems to which I opened another question: <a href="https://stackoverflow.com/questions/27171638/multiple-quotes-cause-pipedoutputstream-outputstreamwriter-to-fail">Multiple quotes cause PipedOutputStream/OutputStreamWriter to fail</a></p>
  
  <p>EDIT:
  Removed PipedStream code example as I posted the issue with it in another question. So here I am still just looking for an easy way to create an nt:file where the input is not an <code>InputStream</code>.</p>