• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamCorruptedException

 
Pardha Bhee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to store some blob object into database, and while retrieving the object i get the exception.......
My code and exception as below..............

Please help me ASAP!

-Pardha

private void insertRecord() {
String query = "INSERT INTO TEST VALUES(?,?)";
PreparedStatement statement = null;
ByteArrayOutputStream regStore = null;
ObjectOutputStream regObjectStream = null;
ByteArrayInputStream regArrayStream = null;
NameVO vo = new NameVO();
vo.setFirstName("xxxxx");
vo.setLastName("yyyyyyyy");

try {
Connection con = makeConnection();
con.setAutoCommit(false);
regStore = new ByteArrayOutputStream();
regObjectStream = new ObjectOutputStream(regStore);
regObjectStream.writeObject(vo);

byte[] regBytes = regStore.toByteArray();
regArrayStream = new ByteArrayInputStream(regBytes);
con.setAutoCommit(false);
statement = con.prepareStatement(query);
statement.setString(1, "1");
statement.setBinaryStream(2, regArrayStream, regBytes.length);
statement.executeUpdate();
regObjectStream.flush();
regArrayStream.close();
regObjectStream.close();
regStore.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void readRecord() {
String query = "SELECT * FROM TEST WHERE ID = ?";
PreparedStatement statement = null;
ResultSet rs = null;
Connection con = null;
ByteArrayInputStream bais = null;

//BufferedInputStream bis = null;
ObjectInputStream ois = null;
NameVO vo = null;

try {
con = makeConnection();
statement = con.prepareStatement(query);
statement.setString(1, "1");
rs = statement.executeQuery();

byte[] regBytes = new byte[1024];

while (rs.next()) {
regBytes = rs.getBytes(2);
System.out.println(new String(regBytes));
bais = new ByteArrayInputStream(regBytes);
System.out.println("1" + bais.toString());
ois = new ObjectInputStream(bais);
System.out.println(ois.toString());
vo = (NameVO) ois.readObject();
}

System.out.println(vo.getFirstName() + " " + vo.getLastName());
bais.close();
ois.close();
statement.close();
rs.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
NameVO vo = new NameVO();
vo.setFirstName("Saurabh");
vo.setLastName("Mathur");

BlobTest test = new BlobTest();
test.insertRecord();
test.readRecord();
}



java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at util.BlobTest.readRecord(BlobTest.java:145)
at util.BlobTest.main(BlobTest.java:169)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!