jInterface to create External Erlang Term

℡╲_俬逩灬. 提交于 2019-12-14 02:25:09

问题


How can I format the the following erlang term:

{ atom, "message" }

In jInterface to an external format that I may call in an erlang shell

erlang:binary_to_term( Binary )

Example: Note that since the tuple will be sent over the net, I finish by converting to byte[].

OtpErlangObject[] msg = new OtpErlangObject[2];
msg[0] = new OtpErlangAtom( "atom" );
msg[1] = new OtpErlangString( "message" );

OtpErlangTuple reply = new OtpErlangTuple(msg);

OtpOutputStream stream = new OtpOutputStream(reply);

stream.toByteArray()    // byte[] which I send over net

The binary received by Erlang is:

B = <<104,2,100,0,4,97,116,111,109,107,0,7,109,101,115,115,97,103,101>>

Then in an erlang shell converting the received term to binary gives a badarg.

 binary_to_term( B ).                                                     
** exception error: bad argument
     in function  binary_to_term/1
        called as binary_to_term(<<104,2,107,0,4,97,116,111,109,107,0,7,109,
                                   101,115,115,97,103,101>>)

回答1:


binary_to_term( <<131,104,2,107,0,4,97,116,111,109,107,0,7,109,101,115,115,97,103,101>> ).
{"atom","message"}

It seems that the message is missing the 131 tag required by term_to_binary. As is evident from the Java output, this tag is not being added by jinterface encode. If I simply add 131 to the beginning of the binary it decodes correctly.

Now why is Java not adding it?

I will still accept answers as I have not officially answered my question ( in a supported way ie. not hacking with 131 )

Ref:

http://www.erlang.org/doc/apps/erts/erl_ext_dist.html




回答2:


I haven't tested this, but if you're encoding {atom, "message"}, shouldn't you be sending over a tuple, not 2 objects one after the other? Try creating a Tuple object and adding atom and message as elements.



来源:https://stackoverflow.com/questions/15189447/jinterface-to-create-external-erlang-term

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!