Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Read the file's content into a buffer.
- * @param {Buffer} buffer - The buffer to write onto.
- * @param {number} length - The maximum number of bytes to be written.
- * @param {number} position - The position to start reading from.
- * @returns {number} The actual number of bytes written.
- */
- async read(buffer, length, position) {
- const apiObject = this.getApiObject();
- if (!apiObject.blob.length) {
- const connector = LynApp.getConnector();
- await connector.filesets.downloadBlob({
- "_id": apiObject.fileset._id,
- "fileId": apiObject._id
- });
- }
- const inputBuffers = apiObject.blob;
- let outputBufferOffset = 0;
- let currentInputBufferAbsoluteBegin = 0;
- let currentInputBufferAbsoluteEnd = inputBuffers[0].length;
- for (let currentInputBufferIndex = 0;
- currentInputBufferIndex < inputBuffers.length;
- ++currentInputBufferIndex) {
- const currentInputBuffer = inputBuffers[currentInputBufferIndex];
- const sliceAbsoluteBegin = Math.max(position, currentInputBufferAbsoluteBegin);
- const sliceAbsoluteEnd = Math.min(position + length, currentInputBufferAbsoluteEnd);
- const sliceLength = sliceAbsoluteEnd - sliceAbsoluteBegin;
- if (sliceLength > 0) {
- const sliceRelativeBegin = sliceAbsoluteBegin - currentInputBufferAbsoluteBegin;
- const sliceRelativeEnd = sliceAbsoluteEnd - currentInputBufferAbsoluteBegin;
- const slice = currentInputBuffer.slice(sliceRelativeBegin, sliceRelativeEnd);
- buffer.set(slice, outputBufferOffset);
- outputBufferOffset += sliceLength;
- }
- currentInputBufferAbsoluteBegin = currentInputBufferAbsoluteEnd;
- currentInputBufferAbsoluteEnd += currentInputBuffer.length;
- }
- return outputBufferOffset;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement