Skip to content

Instantly share code, notes, and snippets.

@arturaz
Created March 31, 2012 20:12
Show Gist options
  • Save arturaz/2268069 to your computer and use it in GitHub Desktop.
Save arturaz/2268069 to your computer and use it in GitHub Desktop.
def write(string)
if @__write_buffer.nil?
@__write_buffer = string
else
@__write_buffer << string
end
total_written = 0
while @__write_buffer.length != 0
begin
written = write_nonblock(@__write_buffer)
rescue ::IO::WaitWritable
wait_writable
retry
rescue EOFError
return total_written
end
total_written += written
if written < @__write_buffer.length
# Avoid mutating string itself, but we can mutate the remaining data
if @__write_buffer.equal?(string)
# Copy the remaining data so as to avoid mutating string
# Note if we have a large amount of data remaining this could be slow
@__write_buffer = string[written..-1]
else
@__write_buffer.slice!(0, written)
end
end
end
total_written
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment