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: 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
is my inputstream and br
my bufferedreader.
This is not a good approach to read by 2 stream at a time for same file. You have to use just one stream.
BufferedReader
is used forcharacter
stream whereasInputStream
is used forbinary
stream.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.
Here's my new code: