StreamCorruptedException throw by ObjectInputStream created from FileInputStream

F

Flavio Castelli

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
 
F

Flavio Castelli

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
 
D

Daniel Pitts

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

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

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