@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
83 lines (82 loc) • 2.97 kB
JavaScript
import { _isNullOrUndefined } from '../utils';
import { _Inflater } from './inflater';
/**
* Provides a deflate decoding stream that inflates compressed data from the backing buffer on demand.
*
* @private
*/
var _DeflateStream = /** @class */ (function () {
function _DeflateStream(data, offset, leaveOpen) {
if (_isNullOrUndefined(data)) {
this._data = data;
}
else {
this._data = [];
}
if (_isNullOrUndefined(leaveOpen)) {
this._leaveOpen = leaveOpen;
}
this._offset = offset;
this._inflater = new _Inflater();
this._buffer = Array(8192).fill(0);
}
/**
* Inflates data into the target array, requesting more input as needed until
* the requested count is satisfied or the end of the stream is reached.
*
* @private
* @param {number[]} array Target array to fill with uncompressed bytes.
* @param {number} offset Offset in the target array where writing begins.
* @param {number} count Maximum number of bytes to read.
* @returns {{count: number, data: number[]}} result Object containing bytes written and final array.
*/
_DeflateStream.prototype._read = function (array, offset, count) {
var length;
var cOffset = offset;
var rCount = count;
while (true) { // eslint-disable-line
var inflateResult = this._inflater._inflate(array, cOffset, rCount);
length = inflateResult.count;
array = inflateResult.data;
cOffset += length;
rCount -= length;
if (rCount === 0) {
break;
}
if (this._inflater._finished) {
break;
}
var result = this._readBytes();
var bytes = result.count;
this._buffer = result.buffer;
if (bytes === 0) {
break;
}
this._inflater._setInput(this._buffer, 0, bytes);
}
return { count: count - rCount, data: array };
};
/**
* Reads a chunk of compressed bytes from the source data into the internal buffer
* and advances the current offset.
*
* @private
* @returns {{buffer: number[], count: number}} result The read buffer and number of bytes read.
*/
_DeflateStream.prototype._readBytes = function () {
if (_isNullOrUndefined(this._offset) && this._offset >= this._data.length) {
return { buffer: [], count: 0 };
}
else {
var count = 0;
for (var i = 0; i < this._buffer.length && i + this._offset < this._data.length; i++) {
this._buffer[i] = this._data[this._offset + i];
count++;
}
this._offset += count;
return { buffer: this._buffer, count: count };
}
};
return _DeflateStream;
}());
export { _DeflateStream };