Skip to content

Commit

Permalink
refactor: improve buffering in write_file function (rcasia#160)
Browse files Browse the repository at this point in the history
Just a better implementation for writing to file in buffers.
  • Loading branch information
rcasia authored Oct 13, 2024
1 parent 4fe7a53 commit 8003930
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lua/neotest-java/util/write_file.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local nio = require("nio")
local compatible_path = require("neotest-java.util.compatible_path")
local Path = require("plenary.path")
local logger = require("neotest-java.logger")

local BUFFER_SIZE = 500

---@param filepath string
---@param content string
Expand All @@ -10,17 +13,16 @@ local function write_file(filepath, content)
-- create parent directories if they don't exist
nio.fn.mkdir(Path:new(_filepath):parent():absolute(), "p")

logger.debug("writing to file: ", _filepath)

local file = io.open(_filepath, "w") or error("Could not open file for writing: " .. _filepath)
local buffer = ""
for i = 1, #content do
buffer = buffer .. content:sub(i, i)
if i % 500 == 0 then
file:write(buffer)
buffer = ""
end
local pointer = 1
for i = BUFFER_SIZE, #content, BUFFER_SIZE do
file:write(content:sub(pointer, i))
pointer = i + 1
end
if buffer ~= "" then
file:write(buffer)
if pointer <= #content then
file:write(content:sub(pointer))
end

file:close()
Expand Down

0 comments on commit 8003930

Please sign in to comment.