Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can BufferedReader read bytes?

Sorry if this question is a dulplicate but I didn't get an answer I was looking for.

Java docs says this

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders >and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in"));

will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

  1. My first question is If bufferedReader can read bytes then why can't we work on images which are in bytes using bufferedreader.

  2. My second question is Does Bufferedreader store characters in BUFFER and what is the meaning of this line

will buffer the input from the specified file.

  1. My third question is what is the meaning of this line

In general, each read request made of a Reader causes a corresponding read request to be >made of the underlying character or byte stream.

like image 925
TruePS Avatar asked Mar 06 '14 06:03

TruePS


People also ask

Can BufferedReader read INT?

The BufferedReader class doesn't provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too. i.e. Initially you need to read the integers in string format.

What is BufferedReader buffer size?

BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/input. txt")), 16384); This will set the buffer size to 16384 bytes (16 KB). The optimal buffer size depends on factors like the type of the input stream and the hardware on which the code is running.

Which is better scanner or BufferedReader?

BufferedReader is a bit faster as compared to scanner because the scanner does the parsing of input data and BufferedReader simply reads a sequence of characters.


1 Answers

There are two questions here.

1. Buffering

Imagine you lived a mile from your nearest water source, and you drink a cup of water every hour. Well, you wouldn't walk all the way to the water for every cup. Go once a day, and come home with a bucket full of enough water to fill the cup 24 times.

The bucket is a buffer.

Imagine your village is supplied water by a river. But sometimes the river runs dry for a month; other times the river brings so much water that the village floods. So you build a dam, and behind the dam there is a reservoir. The reservoir fills up in the rainy season and gradually empties in the dry season. The village gets a steady flow of water all year round.

The reservoir is a buffer.

Data streams in computing are similar to both those scenarios. For example, you can get several kilobytes of data from a filesystem in a single OS system call, but if you want to process one character at a time, you need something similar to a reservoir.

A BufferedReader contains within it another Reader (for example a FileReader), which is the river -- and an array of bytes, which is the reservoir. Every time you read from it, it does something like:

 if there are not enough bytes in the "reservoir" to fulfil this request
      top up the "reservoir" by reading from the underlying Reader
 endif
 return some bytes from the "reservoir".

However when you use a BufferedReader, you don't need to know how it works, only that it works.

2. Suitability for images

It's important to understand that BufferedReader and FileReader are examples of Readers. You might not have covered polymorphism in your programming education yet, so when you do, remember this. It means that if you have code which uses FileReader -- but only the aspects of it that conform to Reader -- then you can substitute a BufferedReader and it will work just the same.

It's a good habit to declare variables as the most general class that works:

 Reader reader = new FileReader(file);

... because then this would be the only change you need to add buffering:

 Reader reader = new BufferedReader(new FileReader(file));

I took that detour because it's all Readers that are less suitable for images.

Reader has two read methods:

 int read(); // returns one character, cast to an int
 int read(char[] block); // reads into block, returns how many chars it read

The second form is unsuitable for images because it definitely reads chars, not ints.

The first form looks as if it might be OK -- after all, it reads ints. And indeed, if you just use a FileReader, it might well work.

However, think about how a BufferedReader wrapped around a FileReader will work. The first time you call BufferedReader.read(), it will call FileReader.read(buffer) to fill its buffer. Then it will cast the first char of the buffer back to an int, and return that.

Especially when you bring multi-byte charsets into the picture, that can cause problems.

So if you want to read integers, use InputStream not Reader. InputStream has int read(byte[] buf, int offset, int length) -- bytes are much more reliably cast back and forth from int than chars.

like image 184
slim Avatar answered Sep 17 '22 15:09

slim