/**
* Binary File Handling Utility - Optimized for Memory Usage
*/
const binaryFileHandler = {
/**
* Reads a binary file into a Uint8Array.
* @param {File} file The File object to read.
* @returns {Promise<Uint8Array>} A Promise resolving to the Uint8Array.
* @throws {Error} If file reading fails.
*/
readBinaryFile: async (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
resolve(new Uint8Array(event.target.result));
};
reader.onerror = (error) => {
reject(error);
};
reader.readAsArrayBuffer(file); // Read as ArrayBuffer for efficient processing
});
},
/**
* Writes a Uint8Array to a binary file.
* @param {Uint8Array} data The data to write.
* @param {string} filePath The file path to write to.
* @returns {Promise<void>} A Promise resolving when the write is complete.
* @throws {Error} If file writing fails.
*/
writeBinaryFile: async (data, filePath) => {
return new Promise((resolve, reject) => {
const writer = new FileWriter(filePath, 'w'); // Use FileWriter for file writing
writer.write(data);
writer.onwriteend = () => {
resolve();
};
writer.onerror = (error) => {
reject(error);
};
});
},
/**
* Calculates the MD5 hash of a Uint8Array.
* @param {Uint8Array} data The data to hash.
* @returns {string} The MD5 hash in hexadecimal format.
*/
calculateMD5: (data) => {
const crypto = require('crypto'); // Ensure crypto is available (Node.js environment)
const hash = crypto.createHash('md5');
hash.update(data);
return hash.digest('hex');
},
/**
* Calculates the SHA256 hash of a Uint8Array.
* @param {Uint8Array} data The data to hash.
* @returns {string} The SHA256 hash in hexadecimal format.
*/
calculateSHA256: (data) => {
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(data);
return hash.digest('hex');
},
/**
* Efficiently reads a binary file in chunks.
* @param {string} filePath The path to the binary file.
* @param {number} chunkSize The size of each chunk to read (in bytes).
* @returns {Promise<Uint8Array>} A Promise resolving to an array of Uint8Arrays.
*/
readBinaryFileChunks: async (filePath, chunkSize) => {
return new Promise((resolve, reject) => {
const fs = require('fs'); // Ensure fs is available (Node.js environment)
const fileStream = fs.createReadStream(filePath);
const chunks = [];
let currentChunk = [];
fileStream.on('data', (chunk) => {
currentChunk = currentChunk.concat(chunk);
if (currentChunk.length >= chunkSize) {
chunks.push(new Uint8Array(currentChunk));
currentChunk = [];
}
});
fileStream.on('end', () => {
if (currentChunk.length > 0) {
chunks.push(new Uint8Array(currentChunk));
}
resolve(chunks);
});
fileStream.on('error', (error) => {
reject(error);
});
});
},
/**
* Writes an array of Uint8Arrays to a binary file.
* @param {Uint8Array[]} dataArray The array of Uint8Arrays to write.
* @param {string} filePath The path to the file to write.
* @returns {Promise<void>} A Promise that resolves when writing is complete.
*/
writeBinaryFileChunks: async (dataArray,
Add your comment