title: Convert ArrayList to byte array
body: <p>I have a code where I am converting array list to byte array and then saving that byte array as a BLOB in MySQL database. Below is code:-</p>
  
  <pre><code>Object temp = attributes.get(columnName);
  if (temp instanceof List &amp;&amp; temp != null) {
      List extraAttributes = (ArrayList) temp;
      resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));    
  </code></pre>
  
  <p>The method <code>createByteArray</code> is defined as below:</p>
  
  <pre><code> private byte [] createByteArray( Object obj)
      {
          byte [] bArray = null;
          try
          {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  ObjectOutputStream objOstream = new ObjectOutputStream(baos);
                  objOstream.writeObject(obj);
                  bArray = baos.toByteArray();
          }
          catch (Exception e)
          {
        TraceDbLog.writeError("Problem in createByteArray", e);
  
          }
  
                  return bArray;
  
      }
  </code></pre>
  
  <p>Well the above code was written earlier for writing HashMap to BLOB i am using same for converting ArrayList if HashMap to BLOB.</p>
  
  <p>The problem which is occurring in read code when i am reading the blob.</p>
  
  <pre><code> private Object readBytes (ResultSet rs, String columnName)
      throws SQLException
      {
          ObjectInputStream ois  = null;
          byte [] newArray;
          Object obj = null;
  
          try
          {
              newArray = rs.getBytes(columnName);
  
              ois = new ObjectInputStream (new ByteArrayInputStream(newArray));
              obj = ois.readObject ();
          }
  </code></pre>
  
  <p>In the read part the object is not coming as arrayList of hasMap and in debug perspective in eclipse eclipse is also not able to inspect the object which is coming.</p>
  
  <p>I have also tried typecasting the object to List but still no success in getting the right response.
  Please tell me whether there is any flaw in reading/writing the above BLOB.</p>