ObjectInputStream throwing EOFException

Here is what I am trying to do. Take an Object and put it in the Database. Then take it back out and make it into an object again. Here is how I am doing it and the error I get is below the code.

import java.io.*;

import java.awt.*;

import java.sql.*;

import java.net.*;

publicclass Test

{

publicstaticvoid main(String[] args)throws Exception

{

// get connection to database

Class.forName("org.postgresql.Driver").newInstance();

Connection con = DriverManager.getConnection("jdbc:postgresql://********");

// make UserProperties Object, simple class containing GUI settings

UserProperties uprops =new UserProperties();

uprops.font(new Font("Verdana", Font.PLAIN, 12));

uprops.foreground(Color.black);

uprops.background(Color.lightGray);

uprops.buttonForeground(Color.black);

uprops.buttonBackground(Color.lightGray);

uprops.lookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

// serialize uprops and put into a String

ByteArrayOutputStream byteOut =new ByteArrayOutputStream();

ObjectOutputStream outStream =new ObjectOutputStream(byteOut);

outStream.writeObject(uprops);

outStream.flush();

outStream.close();

String objectData =new String(byteOut.toByteArray());

// this works great

con.createStatement().executeUpdate("insert into userprops values ('10', '" + URLEncoder.encode(objectData) +"')");

// retrieve what we just inserted and put into a String

ResultSet rs = con.createStatement().executeQuery("select data from userprops where userID=10");

rs.first();

objectData = URLDecoder.decode(rs.getString(1));

rs.close();

con.close();

// get streams

ByteArrayInputStream byteIn =new ByteArrayInputStream(objectData.getBytes());

ObjectInputStream inStream =new ObjectInputStream(byteIn);

// EOFException gets thrown here

UserProperties obj = (UserProperties)inStream.readObject();

inStream.close();

System.out.println(obj.font());

System.out.println(obj.foreground());

System.out.println(obj.background());

System.out.println(obj.buttonForeground());

System.out.println(obj.buttonBackground());

System.out.println(obj.lookAndFeel());

}

}

Exception in thread "main" java.io.EOFException

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2138)

at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:2876)

at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2677)

at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:977)

at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:532)

at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:749)

at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1481)

at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1413)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1604)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1252)

at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1818)

at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1744)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1624)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1252)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:323)

at Test.main(Test.java:42)

[4741 byte] By [geek_freak] at [2007-9-26 14:01:04]
# 1

I answered my own question. I needed to confiure my java.sql.Statement object, PreparedStatement, to write data using a binaryStream.

import java.io.*;

import java.awt.*;

import java.sql.*;

public class Test

{

public static void main(String[] args) throws Exception

{

Class.forName("org.postgresql.Driver").newInstance();

Connection con = DriverManager.getConnection("jdbc:postgresql://******");

UserProperties uprops = new UserProperties();

uprops.font(new Font("Verdana", Font.PLAIN, 12));

uprops.foreground(Color.black);

uprops.background(Color.lightGray);

uprops.buttonForeground(Color.black);

uprops.buttonBackground(Color.lightGray);

uprops.lookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

ByteArrayOutputStream byteOut = new ByteArrayOutputStream ();

ObjectOutputStream out = new ObjectOutputStream(byteOut);

out.writeObject(uprops);

out.flush();

out.close();

byte[] objectData = byteOut.toByteArray();

// need to configure as a binaryStream, I was writing data to database incorrectly

con.createStatement().executeUpdate("delete from userprops");

PreparedStatement ps = con.prepareStatement("insert into userprops (data) values (?)");

ps.setObject(1, objectData, Types.BINARY);

ps.setBinaryStream(1, new ByteArrayInputStream(objectData), objectData.length);

ps.executeUpdate();

ps.close();

ResultSet rs = con.createStatement().executeQuery("select data from userprops");

rs.first();

objectData = rs.getBytes(1);

rs.close();

con.close();

ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objectData));

UserProperties obj = (UserProperties)in.readObject();

System.out.println(obj.font());

System.out.println(obj.foreground());

System.out.println(obj.background());

System.out.println(obj.buttonForeground());

System.out.println(obj.buttonBackground());

System.out.println(obj.lookAndFeel());

}

}

Print Out looks like this

java.awt.Font[family=Verdana,name=Verdana,style=plain,size=12]

java.awt.Color[r=0,g=0,b=0]

java.awt.Color[r=192,g=192,b=192]

java.awt.Color[r=0,g=0,b=0]

java.awt.Color[r=192,g=192,b=192]

javax.swing.plaf.metal.MetalLookAndFeel

geek_freak at 2007-7-2 15:12:01 > top of Java-index,Core,Core APIs...
# 2

I realize that this is an old posting, but here goes nothing. Maybe some Duke Dollars will help it out.

I'm wondering if I have the same problem as the original poster. I'm sending an ImageIcon to ObjectOutputStream and I'm getting the same error. But I don't know if what fixed your code would help me. Does anyone know if it will help?

private void sendImageToServlet(Image image) {

if (image != null) {

ImageIcon icon = new ImageIcon(image);

try {

URL url = new URL(getCodeBase(),"/License/SnapshotServlet");

System.out.println("Trying to connect to URL - " + url);

URLConnection con = url.openConnection();

con.setDoOutput(true);

con.setUseCaches(false);

con.setRequestProperty("Content-Type", "application/x-java-serialized-object");

OutputStream os = con.getOutputStream();

BufferedOutputStream bos = new BufferedOutputStream(os);

ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(icon);

InputStream is = con.getInputStream();

BufferedInputStream bis = new BufferedInputStream(is);

ObjectInputStream ois = new ObjectInputStream(bis);

String respStr = (String) ois.readObject();

System.out.println("The response is: " + respStr);

oos.flush();

oos.close();

ois.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

Here is my error from the Java Console in the browser:

java.io.EOFException

at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2150)

at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2619)

at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:726)

at java.io.ObjectInputStream.<init>(ObjectInputStream.java:251)

at dps.license.webcam.SnapshotApplet.sendImageToServlet(SnapshotApplet.java:194)

at dps.license.webcam.SnapshotApplet.actionPerformed(SnapshotApplet.java:148)

at java.awt.Button.processActionEvent(Button.java:381)

at java.awt.Button.processEvent(Button.java:350)

at java.awt.Component.dispatchEventImpl(Component.java:3526)

at java.awt.Component.dispatchEvent(Component.java:3367)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:190)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)

Here is my error on the Server side:

java.io.EOFException

at java.io.DataInputStream.readFully(DataInputStream.java:168)

at java.io.ObjectInputStream.readFully(ObjectInputStream.java:2076)

at java.io.ObjectInputStream.inputArray(ObjectInputStream.java:1085)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:380)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:242)

at javax.swing.ImageIcon.readObject(ImageIcon.java:373)

at java.lang.reflect.Method.invoke(Native Method)

at java.io.ObjectInputStream.invokeObjectReader(ObjectInputStream.java:2219)

at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1416)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:392)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:242)

at dps.license.webcam.SnapshotServlet.doPost(SnapshotServlet.java:39)

at dps.license.webcam.SnapshotServlet.service(SnapshotServlet.java:75)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager.java:827)

at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycleServlet.java:167)

at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServlet.java:297)

at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycleServlet.java:110)

at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:472)

at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletManager.java:1012)

at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManager.java:913)

at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:523)

at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:282)

at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:112)

at com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java:91)

at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:184)

at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:67)

at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(CacheableInvocationContext.java:106)

at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:125)

at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener.java:315)

at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.java:60)

Does anyone have any idea what is going on here?

wbracken at 2007-7-2 15:12:01 > top of Java-index,Core,Core APIs...
# 3
It won't let me add duke dollars to this, but I'll get them to you, I promise.
wbracken at 2007-7-2 15:12:01 > top of Java-index,Core,Core APIs...