我想从流中读取。在实体中,我没有设置内容长度,为此,我无法创建具有固定和正确大小的缓冲区。我创建了一个16384长度的缓冲区,但我的数据更多,我的数据的一部分丢失了!我想从流中读取并写入char数组,我不想阅读readLine。我怎么写这段代码?请解释部分代码。
由于
char[] buffer;
if (contentLength == -1)
buffer = new char[16038];
else
buffer = new char[contentLength];
InputStream stream = responseEntity.getContent();
//String resultS=convertStreamToString(stream);
InputStreamReader streamReader = new InputStreamReader(stream,"UTF-8");
int hasRead = 0;
int readSize = 0;
int bufferLength = buffer.length;
//List<Byte> listChar=new ArrayList<Byte>();
while (hasRead < bufferLength) {
readSize = streamReader.read(buffer, hasRead, bufferLength
- hasRead);
if (readSize == -1)
break;
hasRead += readSize;
}
答案 0 :(得分:0)
将您发布的代码替换为:
InputStream stream = responseEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
char buffer[] = sb.toString().toCharArray();