简体   繁体   English

如何通过RMI传递InputStream?

[英]How to pass InputStream over RMI?

InputStream is not serialized by default. 默认情况下,InputStream未序列化。 How to pass InputStream over RMI to access EJB? 如何通过RMI传递InputStream来访问EJB?

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. 您必须将数据读取到字节数组中,要么全部读取到一个数组中,要么全部读取到多个数组中(取决于所读取的数据大小),然后将这些字节数组跨RMI传递。 An InputStream that hasn't been read isn't data that can be passed directly. 尚未读取的InputStream并非可以直接传递的数据。

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. 想象一下InputStream从磁盘读取文件,然后将其作为方法参数发送给在另一台机器上的JVM上执行的EJB。 There is no way for that InputStream to be meaningful when deserialized on the other machine. 在另一台计算机上反序列化该InputStream时,没有任何意义。

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. 您需要将InputStream读入可序列化的对象(例如,byte []数组)中并传递该对象。

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). 如果InputStream读取的内容对于内存而言太大,则需要将其包装在Serializable对象中,传递该对象,并使该Object能够在另一端创建InputStream(当然,假设它可以-否则,您可以必须创建自己的流,这是另一个问题)。

In his answer @Konstantin wrote: @Konstantin在回答中写道:

An InputStream that hasn't been read isn't data that can be passed directly. 尚未读取的InputStream并非可以直接传递的数据。

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"? 例如,如果您尝试序列化以“ java app </ usr / bin / yes”调用的应用程序的System.in内容,您会怎么办? (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. 如果要在RMI上“传递”流,则可以通过创建实现Stream API的包装器类并对原始流对象执行RMI回调以读取或写入数据来实现。 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, 没道理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM