title: StreamCorruptedException with ObjectInputStream and ByteArrayInputStream
body: <p>I have a number of objects written to disk using ObjectOutputStream. During reading, I firstly retrieve the file as a ByteArray for some implementation reasons, I want to read the buffered array and decode the data from it. Here is a code snippet</p>
  
  <pre><code>byte [] fileArray=org.apache.commons.io.IOUtils.toByteArray(filePath);
  
  ObjectInputStream in=new ObjectInputStream(new ByteArrayInputStream(fileArray));
  
  while(true){
   Records pos=(Records)in.readObject();
  }
  </code></pre>
  
  <p>However, I get this error</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: 2F6C6F63
  </code></pre>
  
  <p>In summary, I want to load the file into memory and then decode the objects rather than from disk while reading.</p>
  
  <hr>
  
  <p>The file is written as: </p>
  
  <pre><code>fout=new FileOutputStream(filePath);
  bos=new ByteArrayOutputStream();
  oos=new ObjectOutputStream(bos);
  
  for(int i=0;i&lt;size;i++){
   oos.writeObject(list.get(i));
  }
  oos.flush();
  bos.writeTo(fout);
  bos=null;
  oos=null;
  fout.flush();
  fout.close();
  </code></pre>
  
  <p>oos is not closed at all!</p>
  
  <hr>
  
  <p>Here is a full example to reproduce the error:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  import org.apache.commons.io.IOUtils.*;
  
  public class Example{
  
      private int[] data;
  
      public Example(){
          data=new int[40];
      }
  
      public void generate(){
          for(int i=0;i&lt;data.length;i++){
              data[i]=i;
          }
          System.out.println("Data generated!");
      }
  
      public void write(){
          FileOutputStream fout=null;
          ByteArrayOutputStream bos=null;
          ObjectOutputStream oos=null;
          try{
              fout=new FileOutputStream("obj.data");
              bos=new ByteArrayOutputStream();
              oos=new ObjectOutputStream(bos);
              for(int i=0;i&lt;data.length;i++){
                  oos.writeObject((Integer)data[i]);
              }
              oos.flush();
              bos.writeTo(fout);
              bos=null;
              oos=null;
              fout.flush();
              fout.close();
          }catch(IOException ioe){}
          System.out.println("Data written!");
      }
  
      public void read(){
          ObjectInputStream in=null;
          try{
              byte[] fileArray=org.apache.commons.io.IOUtils.toByteArray("obj.data");
              in=new ObjectInputStream(new ByteArrayInputStream(fileArray));
              while(true){
                  Integer data=(Integer)in.readObject();
              }
          }catch (EOFException eofe){
              try{
                  in.close();
              }catch (IOException ioe){
                  ioe.printStackTrace();
              }
          }catch(ClassNotFoundException e){
              e.printStackTrace();
          }catch (Exception e){
              e.printStackTrace();
          }       
          System.out.println("Data read!");
      }
  
      public static void main(String[] args){
          Example example=new Example();
          example.generate();
          example.write();
          example.read();
      }
  
  
  }
  </code></pre>