1

The advantage of BufferedReader seems to be the reduction of hard disc accesses. But how does that work?

Sure instead of reading every single byte, StreamBuffer reads a whole chunk of 1000 Bytes for example. But how can he do that?

On the hard disk all the bytes of a file are maybe not continuous, like in an array, but rather dispersed because a file is not written in one flush. But maybe the operating system always in advance reserves 1000 Bytes for a file and if that is consumed it will reserve another 1000 Bytes.

Who is managing what address each byte of a file corresponds on the hard drive?

Can you say to the hard disk drive controller give me 1000 Bytes of a file by just one read command?

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
Marcela28
  • 17
  • 1
  • 5
  • Java has no `BufferedStreamReader` nor `StreamBuffer` (in the standard library), you might want to edit the title. Plus, your question does not look _really_ java specific, it immediatly drops down to system level. – GPI Aug 24 '20 at 13:38

1 Answers1

2

Hard disks are divided into sectors, so you never read/write only 1 byte.

The allocation of sectors and linking them into files is the job of the filesystem. It can be NTFS for Windows or EXT4 for Linux or any other. They also try to put bytes of one file into consecutive sectors so they are faster to read/write. If it is not possible because of disk fragmentation, the disk access becomes slower and defragmentation is desired.

The disk drive controller expects to read more than 1 byte and always reads at least one sector and puts all the data in a cache. So reading another byte only accesses the cache, not the physical disk. BufferedReader skips even the operating system and filesystem layers and manages a cache inside Java Virtual Machine which is even faster, especially if you need to parse the read data to split it into text lines.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
majster
  • 104
  • 4
  • 1 sector has 512 Byte . So 1 sector always belongs exclusively to one file ?? What is the difference of reading a raw byte or a string with BufferedReader . – Marcela28 Aug 24 '20 at 10:57
  • Yes, 1 sector cannot be shared by different files. The advantage of BufferedReader is that the cache is in JVM, not at the OS level, and the bytes are already converted to Java chars: "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." It also provides the convenience methods readLine() and lines() – majster Aug 24 '20 at 11:15
  • "bytes are already converted to Java chars" . This means when I save a file from windows editor to hard disk a char is maybe only 1 Byte. But Java char has 2 Bytes. Right ? – Marcela28 Aug 24 '20 at 12:10
  • @Marcela28 the conversion of chars to bytes (on a stream, network or disk) is the role of the encoding. `Reader` and `Writer` classes all have an internal encoding that bi-directionnaly maps bytes to chars (ASCII, UTF-8, CP-1252, you name it). This has nothing to do with how many heap bytes a java char consumes (which used to be 2 bytes per char in a UTF-16 compliant way, https://stackoverflow.com/questions/5078314, but it has changed https://stackoverflow.com/questions/44178432). Totally different concepts. So the comments are not really a good place to advance your knowledge in this matter. – GPI Aug 24 '20 at 13:49