Consider these two functions:
Function A takes inputStream as parameter.
public void processStream(InputStream stream)
{
//Do process routine
}
Function B loads a file content to pass it to Function A as InputStream.
pulic void loadFile()
{
File file =new File("c:\\file.txt");
//Pass file as InputStream
}
How can I pass file from Function B to Function A as InputStream without reading it on first hand?
I did something like this:
File file = new File("c:\\file.txt");
DataInputStream stream= new DataInputStream(new FileInputStream(file));
This generated the exception below:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.io.DataInputStream
EDIT:
loadFile() is passing the InputStream as RMI response.
The following should work just fine
You should only not attempt to serialize an
InputStreaminstance byObjectOutputStreamlike aswhich you’re apparently doing in
processStream()method. That’s namely exactly what the exception is trying to tell you. How to solve it properly depends on the sole functional requirement which you omitted from the question.Update as per the comment
There’s the problem. You cannot pass non-serializable objects around as RMI response, let alone unread streams. You need to read the
InputStreaminto aByteArrayOutputStreamthe usual IO way and then use itstoByteArray()to get abyte[]out of it and pass that instead. Something like:Be careful with large files though. Every byte of a
byte[]eats one byte of JVM’s memory.