j接口创建外部Erlang术语 [英] jInterface to create External Erlang Term

查看:162
本文介绍了j接口创建外部Erlang术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化以下erlang术语:

  {atom,message} 

在jInterface中,我可以在erlang shell中调用外部格式

  erlang:binary_to_term(Binary)

示例:
注意,由于元组将通过网络发送,我通过转换为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()//我通过net发送的字节[]

Erlang收到的二进制文件是:

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

然后在一个erlang shell中将接收到的术语转换为二进制给出一个badarg。

  binary_to_term(B)。 
**异常错误:函数中的
函数binary_to_term / 1
称为binary_to_term(<< 104,210,70,4,97,116,111,109,107,0,7,109,
$ 100 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $¬$>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> binary_to_term(< 131,104,2,107,0,4,91,116,111,109,107,0,7,109,101,115,115,97,103,101>>)。
{atom,message}

似乎该邮件缺少term_to_binary需要131个标签。从Java输出可以看出,这个标签没有被jinterface编码添加。如果我只是添加131到二进制的开始,它正确解码。



现在为什么Java不添加它?



我仍然会接受答案,因为我还没有正式回答我的问题(以支持的方式,即不用131进行黑客入侵)



参考:



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


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>>)

解决方案

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

这篇关于j接口创建外部Erlang术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆