1. /**
  2. * Binary File Handling Utility - Optimized for Memory Usage
  3. */
  4. const binaryFileHandler = {
  5. /**
  6. * Reads a binary file into a Uint8Array.
  7. * @param {File} file The File object to read.
  8. * @returns {Promise<Uint8Array>} A Promise resolving to the Uint8Array.
  9. * @throws {Error} If file reading fails.
  10. */
  11. readBinaryFile: async (file) => {
  12. return new Promise((resolve, reject) => {
  13. const reader = new FileReader();
  14. reader.onload = (event) => {
  15. resolve(new Uint8Array(event.target.result));
  16. };
  17. reader.onerror = (error) => {
  18. reject(error);
  19. };
  20. reader.readAsArrayBuffer(file); // Read as ArrayBuffer for efficient processing
  21. });
  22. },
  23. /**
  24. * Writes a Uint8Array to a binary file.
  25. * @param {Uint8Array} data The data to write.
  26. * @param {string} filePath The file path to write to.
  27. * @returns {Promise<void>} A Promise resolving when the write is complete.
  28. * @throws {Error} If file writing fails.
  29. */
  30. writeBinaryFile: async (data, filePath) => {
  31. return new Promise((resolve, reject) => {
  32. const writer = new FileWriter(filePath, 'w'); // Use FileWriter for file writing
  33. writer.write(data);
  34. writer.onwriteend = () => {
  35. resolve();
  36. };
  37. writer.onerror = (error) => {
  38. reject(error);
  39. };
  40. });
  41. },
  42. /**
  43. * Calculates the MD5 hash of a Uint8Array.
  44. * @param {Uint8Array} data The data to hash.
  45. * @returns {string} The MD5 hash in hexadecimal format.
  46. */
  47. calculateMD5: (data) => {
  48. const crypto = require('crypto'); // Ensure crypto is available (Node.js environment)
  49. const hash = crypto.createHash('md5');
  50. hash.update(data);
  51. return hash.digest('hex');
  52. },
  53. /**
  54. * Calculates the SHA256 hash of a Uint8Array.
  55. * @param {Uint8Array} data The data to hash.
  56. * @returns {string} The SHA256 hash in hexadecimal format.
  57. */
  58. calculateSHA256: (data) => {
  59. const crypto = require('crypto');
  60. const hash = crypto.createHash('sha256');
  61. hash.update(data);
  62. return hash.digest('hex');
  63. },
  64. /**
  65. * Efficiently reads a binary file in chunks.
  66. * @param {string} filePath The path to the binary file.
  67. * @param {number} chunkSize The size of each chunk to read (in bytes).
  68. * @returns {Promise<Uint8Array>} A Promise resolving to an array of Uint8Arrays.
  69. */
  70. readBinaryFileChunks: async (filePath, chunkSize) => {
  71. return new Promise((resolve, reject) => {
  72. const fs = require('fs'); // Ensure fs is available (Node.js environment)
  73. const fileStream = fs.createReadStream(filePath);
  74. const chunks = [];
  75. let currentChunk = [];
  76. fileStream.on('data', (chunk) => {
  77. currentChunk = currentChunk.concat(chunk);
  78. if (currentChunk.length >= chunkSize) {
  79. chunks.push(new Uint8Array(currentChunk));
  80. currentChunk = [];
  81. }
  82. });
  83. fileStream.on('end', () => {
  84. if (currentChunk.length > 0) {
  85. chunks.push(new Uint8Array(currentChunk));
  86. }
  87. resolve(chunks);
  88. });
  89. fileStream.on('error', (error) => {
  90. reject(error);
  91. });
  92. });
  93. },
  94. /**
  95. * Writes an array of Uint8Arrays to a binary file.
  96. * @param {Uint8Array[]} dataArray The array of Uint8Arrays to write.
  97. * @param {string} filePath The path to the file to write.
  98. * @returns {Promise<void>} A Promise that resolves when writing is complete.
  99. */
  100. writeBinaryFileChunks: async (dataArray,

Add your comment