[英]outputStream.write has has buffer overflow?
I'm trying to send a byte[]
to a serial port. 我正在尝试将一个
byte[]
发送到串行端口。 However outputstream.write(byte[]);
但是
outputstream.write(byte[]);
only works when byte[].length
contains less then about 100 bytes. 仅当
byte[].length
时才有效byte[].length
包含少于约100个字节。
Just to know: 想知道:
byte[]
never contains more then 476 bytes byte[]
永远不会包含超过476个字节 This code works: 此代码有效:
NRSerialPort port = new NRSerialPort(portname, DEFAULTBAUDRATE); port.connect(); OutputStream outputStream = port.getOutputStream(); for(int i = 0; i<bytes.length; i++){ if(i%10==0){ Thread.sleep(1); } outputStream.write(bytes[i]); } outputStream.flush(); outputStream.close(); port.disconnect();
Advantage: works on all systems 优势:适用于所有系统
Disadvantage: may sleep unnecessarily 缺点:可能会不必要地睡觉
And so does this: 这样做:
NRSerialPort port = new NRSerialPort(portname, DEFAULTBAUDRATE); port.connect(); OutputStream outputStream = port.getOutputStream(); for(byte b : bytes){ outputStream.write(b); } outputStream.flush(); outputStream.close(); port.disconnect();
Advantage: no unnecessarily sleeping 优点:没有不必要的睡眠
Disadvantage: may not work on fast systems because they can process the for each much faster 缺点:可能无法在快速系统上运行,因为它们可以更快地处理每个系统
But the code below will fail if bytes contains more then about 100 bytes: 但是如果字节包含大约100个字节,则下面的代码将失败:
NRSerialPort port = new NRSerialPort(portname, DEFAULTBAUDRATE); port.connect(); OutputStream outputStream = port.getOutputStream(); outputStream.write(bytes); outputStream.flush(); outputStream.close(); port.disconnect();
Although write(byte[])
is a valid method in the sun library 尽管
write(byte[])
是sun库中的有效方法
I have some ideas about this: 我对此有一些想法:
write(byte[])
does not cut the byte[]
to smaller pieces write(byte[])
不会将byte[]
切割成较小的块 You might wonder why I ask this question if I got a working solution. 你可能想知道为什么我问这个问题,如果我有一个有效的解决方案。 Well:
好:
I want to know which one of my solutions is better and/or if there is a other/better way to do this. 我想知道我的哪个解决方案更好和/或是否有其他/更好的方法来做到这一点。 besides why make a method
write(byte[])
if its processing capability's depend on hardware (at least say so in the JavaDoc?) 除了为什么要使用一个方法
write(byte[])
如果它的处理能力依赖于硬件(至少在JavaDoc中这样说?)
Assuming I've googled the correct code, this does not look like a problem with Java but with the implementation of the NRSerialPort
library. 假设我已经使用Google搜索了正确的代码,这看起来不像Java的问题,而是使用
NRSerialPort
库的实现。
Diving into the code, I can see that SerialOutputStream provides the implementation of the OutputStream
and it calls two different methods depending on whether the write(byte)
or write(byte[])
method is used: 深入了解代码,我可以看到SerialOutputStream提供了
OutputStream
的实现,它根据是使用write(byte)
还是write(byte[])
方法调用两种不同的方法:
protected native void writeByte( int b, boolean i ) throws IOException;
protected native void writeArray( byte b[], int off, int len, boolean i )
throws IOException;
Without digging into the native code, I would surmise that there may be a bug in the implementation of the writeArray
method. 在不深入研究本机代码的情况下,我猜测
writeArray
方法的实现可能存在错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.