java.io.StreamCorruptedException: invalid stream header: 5B42403

bingoboy

New Member
I've searched around on a lot of forums but have yet to find an answer to my issue. I've read quite a few topics from people having the same issue as me here on this site but most of them were using applets and servlets and I couldn't see what I was doing wrong compared to them.Anyways, on to my issue. I've recently just taken up learning MySQL, so if you see anything horrendous, forgive me. I'm trying to take a serializable object, and write it to a table in MySQL. From the research I did, I figured out that I needed to use BLOB as the field and that I would need to write to it using a byte[]. Writing works fine, no issues, but when I try to read it, I get the above error.My write method:\[code\] public void addColonist(String p, Colonist c) { // Serializing the Colonist ByteArrayOutputStream baos; baos = new ByteArrayOutputStream(); byte[] byteObject = null; try { out = new ObjectOutputStream(baos); out.writeObject(c); out.flush(); out.close(); baos.flush(); byteObject = baos.toByteArray(); baos.close(); } catch (Exception e) { e.printStackTrace(); } // Adding it to MySQL sql.query("INSERT INTO colonistdb (`player`, `colonist`) VALUES('" + p + "', '" + byteObject + "')"); }\[/code\]And my read/find method:\[code\] public Colonist getColonist(String p) { // Getting the object from MySQL ResultSet rs = sql.query("SELECT `colonist` FROM colonistdb WHERE `player` = '" + p + "'"); byte[] byteObject = null; try { if (rs.first()) { byteObject = (byte[]) rs.getObject("colonist"); } } catch (SQLException e) { return null; } if (byteObject == null) { System.out.println("byteObject is null"); return null; } // Deserializing it for use ByteArrayInputStream bais; Colonist colonist = null; try { bais = new ByteArrayInputStream(byteObject); in = new ObjectInputStream(bais); colonist = (Colonist) in.readObject(); in.close(); bais.close(); } catch (Exception e) { e.printStackTrace(); return null; } System.out.println("Colonist successfully retrived"); return colonist; }\[/code\]From what I've read on a bunch of other questions and posts, I might need to make an ObjectOutputStream in my read method, but I tried that to no avail...Any help is greatly appreciated :)
 
Back
Top