title: Java: I need to get byte array out of outputstream
body: <p>In my case the out put stream is basically FileOutputStream. Hence for this code:</p>
  
  <pre><code>ByteArrayOutputStream bos = (ByteArrayOutputStream) streamToEncrypt;
  </code></pre>
  
  <p>(Where streamToEncrypt is OutputStream) is getting this exception:</p>
  
  <pre><code>java.lang.ClassCastException: java.io.FileOutputStream cannot be cast to java.io.ByteArrayOutputStream
  </code></pre>
  
  <p><strong>All I need to do is to get the  byte array out of this outputstream.
  I do NOT have access to the file at this level. All I have is this output stream that I have to encrypt before pushing it to the file</strong></p>