The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

StreamCorruptedException: does not contain a serialized object

Posted by Bobby K. on November 29, 2001 at 8:33 PM

Can someone tell me why am I getting this exception:

C:\javapr>java FetchObject

[code]
Couldn't retrieve binary data: java.io.StreamCorruptedException: InputStream
does not contain a serialized object
java.io.StreamCorruptedException: InputStream does
not contain a serialized object
at java.io.ObjectInputStream.readStreamHeader
(ObjectInputStream.java:849)
at java.io.ObjectInputStream.
(ObjectInputStream.java:168)
at FetchObject.main(FetchObject.java:23)
[/code]

[code]
import java.sql.*;
import java.util.*;
import java.io.*;

class FetchObject implements Serializable {

public static void main (String[] args) {
try {

String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);

String url = "jdbc:oracle:thin:@mymachine:1521:homedeva";
Connection conn = DriverManager.getConnection(url,"cnn","cnn");

FetchObject i = new FetchObject();

// Select related
try
{
byte[] recdBlob = i.selectBlob( 1 , conn );
ByteArrayInputStream bytes = new ByteArrayInputStream(recdBlob);
ObjectInputStream deserialize = new ObjectInputStream( bytes );
Employee x = (Employee)deserialize.readObject();
}
catch( Exception ex )
{
System.err.println("Couldn't retrieve binary data: " + ex);
ex.printStackTrace();
}


}
catch( Exception ex )
{
ex.printStackTrace();
}
}

public byte[] selectBlob( int id, Connection conn )
{

byte[] returndata = null;

try
{
Statement stmt = conn.createStatement();
String sql = "SELECT id, rowdata FROM blobs WHERE id = " + id;
ResultSet rs = stmt.executeQuery(sql);

if ( rs.next() )
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
BufferedInputStream bis = new BufferedInputStream( rs.getBinaryStream("rowdata") );

byte[] bindata = new byte[4096];
int bytesread = 0;

if ( !rs.wasNull() )
{

if ( (bytesread = bis.read(bindata,0,bindata.length)) != -1 )
{
baos.write(bindata,0,bytesread);
}

returndata = baos.toByteArray();
}
baos.flush();
bis.close();
}
catch ( Exception ex )
{
System.err.println("Problem retrieving binary data: " + ex);
}
rs.close();
stmt.close();
}
}
catch ( Exception ex )
{
System.err.println("Couldn't retrieve binary data: " + ex);
}
return returndata;
}
}
[/code]

[code]
import java.io.*;

class Employee implements Serializable
{
private String lastName;
private String firstName;
public Employee(String lastName, String firstName)
{
this.lastName = lastName;
this.firstName = firstName;
}
}
[/code]



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us