Z1maV1

binary.lua

Aug 18th, 2024
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. binary = {}
  2.  
  3. function binary.readAllBytes(path)
  4.     if type(path) ~= "string" then
  5.         return nil
  6.     end
  7.     local handle = fs.open(path, "rb")
  8.     if not handle then
  9.         return nil
  10.     end
  11.  
  12.     local byteArr = {}
  13.  
  14.     local byte = handle.read()
  15.  
  16.     while byte do
  17.         table.insert(byteArr, byte)
  18.         byte = handle.read()
  19.     end
  20.  
  21.     handle.close()
  22.     return byteArr
  23. end
  24.  
  25. function binary.writeAllByte(path, byteArray)
  26.     if type(path) ~= "string" then
  27.         return
  28.     end
  29.     if type(byteArray) ~= "table" then
  30.         return
  31.     end
  32.     local handle = fs.open(path, "wb")
  33.     if not handle then
  34.         return
  35.     end
  36.  
  37.     for i, byte in ipairs(byteArray) do
  38.         handle.write(byte)
  39.         if i % 1000 == 0 then
  40.             os.sleep(0.1)
  41.         end
  42.     end
  43.  
  44.     handle.close()
  45. end
  46.  
  47. return binary
Advertisement
Add Comment
Please, Sign In to add comment