Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

StreamCorruptedException throw by ObjectInputStream created from FileInputStream

Thread view: 
Flavio Castelli - 24 Jul 2007 10:24 GMT
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
Daniel Pitts - 24 Jul 2007 15:00 GMT
> I, I'm trying to load an object serialized to a file using an
> ObjectOutputStream.
[quoted text clipped - 8 lines]
> 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.
[quoted text clipped - 5 lines]
> 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.
Flavio Castelli - 26 Jul 2007 13:27 GMT
> 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...
Roedy Green - 30 Jul 2007 11:26 GMT
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.