【发布时间】:2015-08-23 09:35:10
【问题描述】:
有没有一种无需使用Apache IO lib 即可一次读取所有 InputStream 值的方法?
我正在读取 IR 信号并将其从 InputStream 保存到 byte[] 数组中。在调试时,我注意到它只有在我在那里放置延迟时才有效,以便我一次读取所有字节然后处理它。
有更聪明的方法吗?
代码:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[100];
int numberOfBytes;
removeSharedPrefs("mSharedPrefs");
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
numberOfBytes = mmInStream.read(buffer);
Thread.sleep(700); //If I stop it here for a while, all works fine, because array is fully populated
if (numberOfBytes > 90){
// GET AXIS VALUES FROM THE SHARED PREFS
String[] refValues = loadArray("gestureBuffer", context);
if (refValues!=null && refValues.length>90) {
int incorrectPoints;
if ((incorrectPoints = checkIfGesureIsSameAsPrevious(buffer, refValues, numberOfBytes)) < 5) {
//Correct
} else {
//Incorrect
}
}
saveArray(buffer, numberOfBytes);
}else{
System.out.println("Transmission of the data was corrupted.");
}
buffer = new byte[100];
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, numberOfBytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
永远不要假设当你要求读取 N 个字节时,你会得到 N 个字节。你可以得到 0 到 N 个字节,但你永远不能确定你会得到 N 个字节。始终使用循环读取。添加 sleep() 不是解决方案。它可能碰巧起作用,因为它足以让字节可用,但这取决于网络、机器等。在 Java 7 及更高版本中,有一些快捷方法可以完全读取流,但在 Java 6 中没有,AFAIK .也许android有这样的实用程序,但我对Android的了解不够了解。反正自己写很简单。
-
@JBNizet,谢谢。我会尝试做循环的事情,如果你希望你可以回答这个问题。也许给我一些示例代码。非常感谢。能否请您检查一下 Sebastians 的答案是否正确?
-
我认为它是正确的,是的。我对nio不是很了解,你为什么不测试一下?
-
我要测试一下,我只是赶时间。很抱歉。
-
定义“所有 InputStream 值”。大多数情况下,您只需要继续阅读即可。在网络代码中休眠并不能真正解决任何问题。无需从流更改为 NIO。
标签: java android inputstream