[英]How to read buffered data as it arrives rather than when the buffer is full?
Using Android, I am reading data (a signed byte array) from a microcontroller. 使用Android,我正在从微控制器读取数据(带符号字节数组)。 I understand that BufferedReader
has a buffer size of 8192 bytes. 我了解BufferedReader
的缓冲区大小为8192字节。 I'm sending about 200-300 bytes every second, this means at the moment I have to wait about 25-30 seconds (8192/300) before the debugger will read the byte array. 我每秒发送大约200-300个字节,这意味着目前我必须等待大约25-30秒(8192/300),然后调试器才能读取字节数组。
I want to be reading this every time the Android receives the 200-300 byte array (every full TCP packet) is there a way to alter the maximum byte size of the BufferedReader
, or is there an alternative way of doing this? 我想在Android每次接收200-300字节数组(每个完整的TCP数据包)时都阅读此内容,是否可以更改BufferedReader
的最大字节大小,或者是否有其他替代方法?
private BufferedReader input;
input = new BufferedReader(new InputStreamReader(
this.clientSocket.getInputStream()));
...
...
while(true)
{
try
{
Log.i("WaitingForSomeData","");
String read = input.readLine();
byte[] bytes = read.getBytes();
}
I am reading data (a signed byte array) from a microcontroller 我正在从微控制器读取数据(带符号字节数组)
You should be using a BufferedInputStream
if you want to buffer binary data. 如果要缓冲二进制数据,则应使用BufferedInputStream
。
You are currently attempting to read a character stream. 您当前正在尝试读取字符流。 Your readLine()
method will block until either \\r
, \\n
or \\r\\n
is received. 您的readLine()
方法将阻塞,直到收到\\r
, \\n
或\\r\\n
为止。 I'm guessing this causes your delay, since you are waiting for an 0x0A
or 0x0D
byte to conveniently arrive. 我猜这会导致您的延迟,因为您正在等待0x0A
或0x0D
字节方便到达。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.