一起使用BufferedReader和InputStream [英] Use BufferedReader and InputStream together
问题描述
我使用BufferedReader从InputStream中读取行。当我直接从InputStream读取内容时,BufferedReader会忽略我的读取并继续在同一位置读取。是否可以防止这种行为?如果不是这样做的好习惯?
I use a BufferedReader to read lines from an InputStream. When I read something directly from the InputStream, the BufferedReader ignores my read and continues reading at the same location. Is it possible to prevent this behavior? If not what is a good practice to do this?
PS:这是我的代码:
PS: Here's my code:
byte[] ba = new byte[1024*1024];
int off = 0;
int len = 0;
do {
len = Integer.parseInt(br.readLine());
in.read(ba, off, len);
br.readLine();
off += len;
} while(len > 0);
in
是我的输入流和 br
我的bufferedreader。
in
is my inputstream and br
my bufferedreader.
推荐答案
同时从BufferedReader和InputStream读取不可能。如果你需要二进制数据,你应该使用多个readLine()调用。
Reading from a BufferedReader and an InputStream at the same time is not possible. If you need binary data, you should use multiple readLine() calls.
这是我的新代码:
byte[] ba = new byte[1024*1024];
int off = 0;
int len = 0;
do {
len = Integer.parseInt(br.readLine().split(";" , 2)[0],16);
for (int cur = 0; cur < len;) {
byte[] line0 = br.readLine().getBytes();
for (int i = 0; i < line0.length; i++) {
ba[off+cur+i] = line0[i];
}
cur += line0.length;
if(cur < len) {
ba[off+cur] = '\n';
cur++;
}
}
off += len;
} while(len > 0);
这篇关于一起使用BufferedReader和InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!