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 - 25 Jul 2007 13:43 GMT
Hi, 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
Esmond Pitt - 30 Jul 2007 03:02 GMT
> Any hint?

How did you write the stream? e.g. did you know you can't append
ObjectOutputStreams?
Flavio Castelli - 30 Jul 2007 08:31 GMT
> How did you write the stream? e.g. did you know you can't append
> ObjectOutputStreams?

Here's the code:

KeyGenerator keygen = KeyGenerator.getInstance("DES");
key = keygen.generateKey();
FileOutputStream fos = new FileOutputStream(deskey_file);
ObjectOutputStream keyFile = new ObjectOutputStream(fos);
keyFile.writeObject(key);
keyFile.close();
fos.close();

As you can see I don't write in append mode.

The strange thing is that I'm able to successfully load the keyFile
from a command-line program but NOT from the ServletContextListener.

Thanks in advance
Flavio
Daniel Pitts - 30 Jul 2007 17:17 GMT
On Jul 30, 12:31 am, Flavio Castelli <flavio.caste...@gmail.com>
wrote:

> > How did you write the stream? e.g. did you know you can't append
> > ObjectOutputStreams?
[quoted text clipped - 16 lines]
> Thanks in advance
> Flavio

Are you using an absolute path or a relative path?
Have you tried using ClassLoader.getResourceAsStream()?


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.