mozilla/pdf.js — pull request #4809 by Snuffleupagus (2014-05-18 19:17:32+00:00)
Title: Fix loading of fonts with empty font files (bug 866395 and issue 3522)

Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=866395.
Fixes #3522.

I've struggled with the best way to create the `isEmpty` property for the `DecodeStream`. The first idea, to check if `peekBytes(1)` returns data won't work at the end of the stream. Furthermore just checking if `this.buffer` is null or empty won't work either, because if `ensureBuffer` has been called that no longer holds even if the stream was actually empty. The only remaining (simple) approach seem to be to read enough of the stream and then check if `this.bufferLength === 0`, since that should only happen when the stream is actually empty.

If the current approach used in this PR isn't any good, we might instead do something along these lines:
```javascript
get isEmpty() {
if (this.pos === 0) {
return (this.peekBytes(1).length() === 0);
}
var currentPos = this.pos;
this.reset();
var firstByte = this.peekBytes(1);
this.skip(currentPos);
return (firstByte.length === 0);
}
```

@yurydelendik Which solution do you prefer?