Merge pull request #4966 from nnethercote/faster-ensureRange-2
Add ChunkedStream.ensureByte().
This commit is contained in:
commit
011eb8468f
@ -31,6 +31,7 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
this.numChunks = Math.ceil(length / chunkSize);
|
this.numChunks = Math.ceil(length / chunkSize);
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.initialDataLength = 0;
|
this.initialDataLength = 0;
|
||||||
|
this.lastSuccessfulEnsureByteChunk = -1; // a single-entry cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// required methods for a stream. if a particular stream does not
|
// required methods for a stream. if a particular stream does not
|
||||||
@ -90,6 +91,18 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
ensureByte: function ChunkedStream_ensureRange(pos) {
|
||||||
|
var chunk = Math.floor(pos / this.chunkSize);
|
||||||
|
if (chunk === this.lastSuccessfulEnsureByteChunk) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(chunk in this.loadedChunks)) {
|
||||||
|
throw new MissingDataException(pos, pos + 1);
|
||||||
|
}
|
||||||
|
this.lastSuccessfulEnsureByteChunk = chunk;
|
||||||
|
},
|
||||||
|
|
||||||
ensureRange: function ChunkedStream_ensureRange(begin, end) {
|
ensureRange: function ChunkedStream_ensureRange(begin, end) {
|
||||||
if (begin >= end) {
|
if (begin >= end) {
|
||||||
return;
|
return;
|
||||||
@ -142,7 +155,7 @@ var ChunkedStream = (function ChunkedStreamClosure() {
|
|||||||
if (pos >= this.end) {
|
if (pos >= this.end) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
this.ensureRange(pos, pos + 1);
|
this.ensureByte(pos);
|
||||||
return this.bytes[this.pos++];
|
return this.bytes[this.pos++];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user