使用ruby发送者和java客户端的java.io.StreamCorruptedException

时间:2022-04-18 04:50:37

I have a ruby program that writes data to a socket with sock.write, and I'm reading the data with ObjectInputStream in a java file. I'm getting an invalid header error that translate to the first few characters of my stream.

我有一个ruby程序,使用sock.write将数据写入套接字,我正在java文件中使用ObjectInputStream读取数据。我收到一个无效的标题错误,转换为我的流的前几个字符。

I've read that if you use ObjectInputStream you must write with ObjectOutputStream, but since the writing file is in ruby im not sure how to accomplish this.

我已经读过如果你使用ObjectInputStream你必须使用ObjectOutputStream编写,但由于写入文件是ruby我不知道如何实现这一点。

1 个解决方案

#1


0  

As you say, ObjectInputStream assumes that the bytes it's receiving have been formatted by an ObjectOutputStream. That is, it is expecting the incoming bytes to be a specific representation of a Java primitive or object.

正如您所说,ObjectInputStream假定它接收的字节已由ObjectOutputStream格式化。也就是说,期望传入的字节是Java原语或对象的特定表示。

Your Ruby code is unlikely to format bytes in such a way.

您的Ruby代码不太可能以这种方式格式化字节。

You need to define exactly the byte format of the message passing from the Ruby to the Java process. You could tell us more about that message format, but it's likely you will need to use Java's ByteArrayInputStream (https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html). The data will come into the Java program as a raw array of bytes, and you will need to parse/unpack/process these bytes into whatever objects are appropriate.

您需要准确定义从Ruby传递到Java进程的消息的字节格式。您可以告诉我们有关该消息格式的更多信息,但您可能需要使用Java的ByteArrayInputStream(https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html)。数据将作为原始字节数组进入Java程序,您需要将这些字节解析/解包/处理为适当的任何对象。

Unless performance is critical, you'd probably be best off using JSON or YAML as the intermediate format. They would make it simple to send simple objects such as strings, arrays, and hashes (maps).

除非性能至关重要,否则最好使用JSON或YAML作为中间格式。它们可以简化发送简单对象,如字符串,数组和散列(映射)。

#1


0  

As you say, ObjectInputStream assumes that the bytes it's receiving have been formatted by an ObjectOutputStream. That is, it is expecting the incoming bytes to be a specific representation of a Java primitive or object.

正如您所说,ObjectInputStream假定它接收的字节已由ObjectOutputStream格式化。也就是说,期望传入的字节是Java原语或对象的特定表示。

Your Ruby code is unlikely to format bytes in such a way.

您的Ruby代码不太可能以这种方式格式化字节。

You need to define exactly the byte format of the message passing from the Ruby to the Java process. You could tell us more about that message format, but it's likely you will need to use Java's ByteArrayInputStream (https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html). The data will come into the Java program as a raw array of bytes, and you will need to parse/unpack/process these bytes into whatever objects are appropriate.

您需要准确定义从Ruby传递到Java进程的消息的字节格式。您可以告诉我们有关该消息格式的更多信息,但您可能需要使用Java的ByteArrayInputStream(https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html)。数据将作为原始字节数组进入Java程序,您需要将这些字节解析/解包/处理为适当的任何对象。

Unless performance is critical, you'd probably be best off using JSON or YAML as the intermediate format. They would make it simple to send simple objects such as strings, arrays, and hashes (maps).

除非性能至关重要,否则最好使用JSON或YAML作为中间格式。它们可以简化发送简单对象,如字符串,数组和散列(映射)。