StreamCorruptedException throw by ObjectInputStream created from FileInputStream

F

Flavio Castelli

I, I'm trying to load an object serialized to a file using an
ObjectOutputStream.

My code looks like this one:
1. FileInputStream fis = new FileInputStream(pwdFileName);
2. ObjectInputStream keyFile = new ObjectInputStream(fis);
3. SecretKey deskey = (SecretKey) keyFile.readObject();
4. keyFile.close();

I get a StreamCorruptedException at line 3.
Debugging I discovered that FileInputStream::available() return 258 bytes,
while FileInputStream::available() returns 0.

This is the exception's stacktrace:
java.io.StreamCorruptedException
at java.io_ObjectInputStream.readObject0(ObjectInputStream.java:1301)
at java.io_ObjectInputStream.defaultReadFields(ObjectInputStream.java:1845)
at java.io_ObjectInputStream.readSerialData(ObjectInputStream.java:1769)
at
java.io_ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1646)
at java.io_ObjectInputStream.readObject0(ObjectInputStream.java:1274)
at java.io_ObjectInputStream.readObject(ObjectInputStream.java:324)
at it.reply.encryption.DesDecrypter.decrypt(DesDecrypter.java:116)
at it.foo.bar.dao.DSFactory.loadCryptedDataSourceUser(DSFactory.java:26)
at it.foo.bar.ContextListener.contextInitialized(ContextListener.java:78)
at
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3763)
at
org.apache.catalina.core.StandardContext.start(StandardContext.java:4211)
at
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at
org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
at
org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at
org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)

This code is called from the method contextInitialized of a class
implementing ServletContextListener.

If I use the code into a java command-line program everything works fine.

Any hint?

Thanks in advance
Flavio
 
D

Daniel Pitts

I, I'm trying to load an object serialized to a file using an
ObjectOutputStream.

My code looks like this one:
1. FileInputStream fis = new FileInputStream(pwdFileName);
2. ObjectInputStream keyFile = new ObjectInputStream(fis);
3. SecretKey deskey = (SecretKey) keyFile.readObject();
4. keyFile.close();

I get a StreamCorruptedException at line 3.
Debugging I discovered that FileInputStream::available() return 258 bytes,
while FileInputStream::available() returns 0.
[snip exception]

This code is called from the method contextInitialized of a class
implementing ServletContextListener.

If I use the code into a java command-line program everything works fine.

Any hint?

Thanks in advance
Flavio

It could be that you aren't opening the file you think you are. Try
printing or logging "new File(pwdFileName).getCanonicalPath()", that
should tell you exactly which file you are opening. Generally you're
better off putting resources like this in your classpath, and using
ClassLoader.getResource(pwdFileName); the getResource method will
search all of your classpath and return the URL of the file.
 
F

Flavio Castelli

Daniel said:
It could be that you aren't opening the file you think you are. Try
printing or logging "new File(pwdFileName).getCanonicalPath()", that
should tell you exactly which file you are opening.
I've checked it using your suggestion and I'm opening the right file. It's
really strange, isn't it?
Generally you're
better off putting resources like this in your classpath, and using
ClassLoader.getResource(pwdFileName); the getResource method will
search all of your classpath and return the URL of the file.
I'll look also after it.

Any other suggestion?

Thanks
Flavio

PS: forgive me for the triple post in the ng, I had some problems with my
news server and google...
 
R

Roedy Green

3. SecretKey deskey = (SecretKey) keyFile.readObject();
4. keyFile.close();

I get a StreamCorruptedException at line 3.

The problem is where you WROTE the file. Your read does not match
your write, or classes changed in the interim.

Try creating the tiniest program that demonstrates your problem then
post the ENTIRE program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top