我有时候在读取inputStream时没有获得整个数据(收到somtime完整数据).
private String readInputStream(InputStream in) {
PushbackInputStream inputStream = (PushbackInputStream) in;
StringBuffer outputBuffer = null;
try {
int size = inputStream.available();
outputBuffer = new StringBuffer(size);
// append the data into the stringBuilder
for (int j = 0; j < size; j++) {
int ch = inputStream.read();
outputBuffer.append((char) ch);
}
} catch (IOException ioe) {
Log.e("error", "IOException: " +
ioe.getMessage());
}
if (outputBuffer != null) {
return outputBuffer.toString();
}
Run Code Online (Sandbox Code Playgroud)
我应该读取输入流,直到inputStream.available()为零..?inputStream中的数据是大的.Plz建议使用示例代码的一些替代
您误用了available().请参阅 Javadoc。它与“大小”无关,只与当前可以在不阻塞的情况下读取的内容有关。这可以小至零字节。
您应该阅读直到流结束,或者直到您拥有完整的消息、行、文档或您正在阅读的任何内容。
由于您在这段代码中没有使用任何PushbackInputStream,因此它与问题无关。
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |