Remove stringio usage from line buffer util, add max buffer length

This commit is contained in:
Chris Roberts 2021-06-29 09:57:18 -07:00
parent ef18b45786
commit 35de0d724e

View File

@ -1,34 +1,59 @@
require 'stringio'
module Vagrant
module Util
class LineBuffer
def initialize
@buffer = StringIO.new
# Maximum number of characters to buffer before sending
# to callback without detecting a new line
MAX_LINE_LENGTH = 5000.freeze
# Create a new line buffer. The registered block
# will be called when a new line is encountered on
# provided input, or the max line length is reached
def initialize(&callback)
raise ArgumentError,
"Expected callback but received none" if callback.nil?
@mu = Mutex.new
@callback = callback
@buffer = ""
end
def lines(data, &block)
if data == nil
return
end
remaining_buffer = StringIO.new
@buffer << data
@buffer.string.each_line do |line|
if line.end_with? "\n"
block.call(line.rstrip)
else
remaining_buffer << line
break
# Add string data to output
#
# @param [String] str String of data to output
# @return [self]
def <<(str)
@mu.synchronize do
while i = str.index("\n")
@callback.call((@buffer + str[0, i+1]).rstrip)
@buffer.clear
str = str[i+1, str.length].to_s
end
@buffer << str.to_s
if @buffer.length > MAX_LINE_LENGTH
@callback.call(@buffer.dup)
@buffer.clear
end
end
@buffer = remaining_buffer
self
end
def remaining(&block)
if @buffer.length > 0
block.call(@buffer.string.rstrip)
@buffer = StringIO.new
# Closes the buffer. Any remaining data that has
# been buffered will be given to the callback.
# Once closed the instance will no longer be usable.
#
# @return [self]
def close
@mu.synchronize do
# Send any remaining output on the buffer
@callback.call(@buffer.dup) if !@buffer.empty?
# Disable this buffer instance
@callback = nil
@buffer.clear
@buffer.freeze
end
self
end
end
end