开发者

How to pass InputStream over RMI?

InputStream is not serialized by default. How to pass InputStrea开发者_高级运维m over RMI to access EJB?


You can't. Imagine the InputStream reads a file from disk, and you send it as a method parameter to an EJB executing on a JVM on another machine. There is no way for that InputStream to be meaningful when deserialized on the other machine.

You need to think about the underlying problem rather than the mechanics. You need to read the InputStream into a serializable object (say a byte[] array) and pass that.

If the InputStream reads something that is too large for memory, then you need to wrap that in a Serializable object, pass that, and have that Object able to create the InputStream on the other end (assuming that it can, of course - otherwise you have to create your own stream, which is a whole other question).


You have to read data into byte array, either all of into one array or into multiple ones (depends on the size of data being read) and then pass those byte arrays across RMI. An InputStream that hasn't been read isn't data that can be passed directly.


In his answer @Konstantin wrote:

An InputStream that hasn't been read isn't data that can be passed directly.

Precisely!

For instance, what do you think would happen if you tried to serialize the contents of System.in for an application invoked as "java app < /usr/bin/yes"? (Hint: read "man yes")

If you want to "pass" a stream across RMI, you could possibly do it by creating a wrapper class that implements the Stream API, and performs RMI callbacks to the original stream object to read or write data. You'd need to arrange that the serialized state of the stream wrapper included a handle to perform the callbacks.

However, it more sensible for your client to explicitly read the entire stream content, and pass it to the server as a byte (or character) array or buffer.


This is like trying to send a telephone over a telephone wire. It doesn't make sense,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜