- while (1) {
- int const this_time = fread (buffer + read, 1, amount - read, _current_file);
+ while (true) {
+
+ bool eof = false;
+ size_t to_read = amount - read;
+
+ DCPOMATIC_ASSERT (_current_file);
+
+ auto const current_position = _current_file->tell();
+ if (current_position == -1) {
+ to_read = 0;
+ eof = true;
+ } else if ((current_position + to_read) > _current_size) {
+ to_read = _current_size - current_position;
+ eof = true;
+ }
+
+ int const this_time = _current_file->read(buffer + read, 1, to_read);