class IO::Stream::Buffered

def syswrite(buffer)

def syswrite(buffer)
	# This fails due to re-entrancy issues with a concurrent call to `sysclose`.
	# return @io.write(buffer)
	
	while true
		result = @io.write_nonblock(buffer, exception: false)
		
		case result
		when :wait_readable
			@io.wait_readable(@io.timeout) or raise ::IO::TimeoutError, "read timeout"
		when :wait_writable
			@io.wait_writable(@io.timeout) or raise ::IO::TimeoutError, "write timeout"
		else
			if result == buffer.bytesize
				return
			else
				buffer = buffer.byteslice(result, buffer.bytesize)
			end
		end
	end
end