本机消息传递主机尝试发送977472013字节长的消息 [英] Native Messaging host tried sending a message that is 977472013 bytes long
问题描述
我启用了Chrome的日志记录功能,因此我可以读取 C:\ Users \%UserName%\AppData\Local\Google\Chrome\User Data\chrome_debug.log
file
我实际上无法使用我的Java应用程序发送或接收消息,但我知道它已被使用。
以下是我的主机清单:
{
allowed_origins:
[
chrome-extension:// EXTENSION- ID /
,
description:my.app,
name:my.app,
path:launch.bat ,
type:stdio
}
这里是批处理文件launch.bat:
java -jar%〜dp0ChromeSEOConnector.jar
$ c $这里是我的Java代码:
private String readMessage(InputStream in)抛出IOException {
字节[] b =新字节[4];
in.read(b);
int size = getInt(b);
b =新字节[size];
in.read(b);
返回新的字符串(b,UTF-8);
}
private void sendMessage(String message)throws IOException {
Text text = new Text(message);
String resposta = serializer.toJson(text);
System.out.write(getBytes(resposta.length()));
System.out.write(resposta.getBytes(UTF-8));
System.out.flush();
}
public int getInt(byte [] bytes){
return(bytes [3]<< 24)& 0xff000000 |
(字节[2] <= 16)& 0x00ff0000 |
(字节[1]<< 8& 0x0000ff00 |
(字节[0] <= 0)& 0x000000ff;
}
public byte [] getBytes(int length){
byte [] bytes = new byte [4];
bytes [0] =(byte)(length& 0xFF);
字节[1] =(字节)((长度>> 8)& 0xFF);
字节[2] =(字节)((长度> 16)&0xFF);
字节[3] =(字节)((长度> 24)&0xFF);
返回字节;
}
似乎System.in从来没有得到我的应用程序的输入,而且System.out也永远不会发送数据。
Chrome继续发生同样的错误:
本地消息传递主机尝试发送977472013字节长的消息。
奇怪的是,消息的大小始终是相同的,即使我手动更改发送消息的大小,就好像该消息根本没有被分析。你有没有遇到那种错误?在此先感谢
解决方案我认为您必须交换定义消息长度的字节顺序。将您的 getBytes()方法更改为:
public byte [] getBytes(int length){
byte [] bytes = new byte [4];
字节[3] =(字节)(长度& 0xFF);
字节[2] =(字节)((长度>> 8)&0xFF);
字节[1] =(字节)((长度> 16)&0xFF);
bytes [0] =(byte)((length>> 24)&0xFF);
返回字节;
}
I'm using a java jar to send and receive messages using Chrome Native Messaging.
I enabled logging of Chrome so I could read C:\Users\%UserName%\AppData\Local\Google\Chrome\User Data\chrome_debug.log
file
I'm actually unable to send or receive message with my Java app, but I know it is used.
Here is the manifest of my host :
{
"allowed_origins" :
[
"chrome-extension://EXTENSION-ID/"
],
"description" : "my.app",
"name" : "my.app",
"path" : "launch.bat",
"type" : "stdio"
}
Here is content of the Batch file launch.bat :
java -jar "%~dp0ChromeSEOConnector.jar"
And here is my Java code :
private String readMessage(InputStream in) throws IOException {
byte[] b = new byte[4];
in.read(b);
int size = getInt(b);
b = new byte[size];
in.read(b);
return new String(b, "UTF-8");
}
private void sendMessage(String message) throws IOException {
Text text = new Text(message);
String resposta = serializer.toJson(text);
System.out.write(getBytes(resposta.length()));
System.out.write(resposta.getBytes("UTF-8"));
System.out.flush();
}
public int getInt(byte[] bytes) {
return (bytes[3] << 24) & 0xff000000 |
(bytes[2] << 16) & 0x00ff0000 |
(bytes[1] << 8) & 0x0000ff00 |
(bytes[0] << 0) & 0x000000ff;
}
public byte[] getBytes(int length) {
byte[] bytes = new byte[4];
bytes[0] = (byte) (length & 0xFF);
bytes[1] = (byte) ((length >> 8) & 0xFF);
bytes[2] = (byte) ((length >> 16) & 0xFF);
bytes[3] = (byte) ((length >> 24) & 0xFF);
return bytes;
}
It seems like the System.in does never get the input of my app, and the System.out never sends data also.
Chrome keeps on getting the same error :
Native Messaging host tried sending a message that is 977472013 bytes long.
What is weird is that the size of the message is always the same, even if I change manually the size of the message sent, as if the message was not analyzed at all. Did you encounter that kind of error ? Thanks in advance
解决方案 I think you have to swap the order of the bytes defining your message length. Change your getBytes() method to this:
public byte[] getBytes(int length) {
byte[] bytes = new byte[4];
bytes[3] = (byte) (length & 0xFF);
bytes[2] = (byte) ((length >> 8) & 0xFF);
bytes[1] = (byte) ((length >> 16) & 0xFF);
bytes[0] = (byte) ((length >> 24) & 0xFF);
return bytes;
}
这篇关于本机消息传递主机尝试发送977472013字节长的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!