@@ -32,12 +32,30 @@ def write_timeout=(timeout)
32
32
@write_timeout = ( timeout if timeout && timeout > 0 )
33
33
end
34
34
35
- def read ( nbytes )
36
- result = @buffer . slice! ( 0 , nbytes )
35
+ string_capacity_support = begin
36
+ String . new ( capacity : 0 )
37
+ true # Ruby 2.4+
38
+ rescue ArgumentError
39
+ false # Ruby 2.3
40
+ end
41
+
42
+ if string_capacity_support
43
+ def read ( nbytes )
44
+ result = @buffer . slice! ( 0 , nbytes )
37
45
38
- result << _read_from_socket ( nbytes - result . bytesize ) while result . bytesize < nbytes
46
+ buffer = String . new ( capacity : nbytes , encoding : Encoding ::ASCII_8BIT )
47
+ result << _read_from_socket ( nbytes - result . bytesize , buffer ) while result . bytesize < nbytes
39
48
40
- result
49
+ result
50
+ end
51
+ else
52
+ def read ( nbytes )
53
+ result = @buffer . slice! ( 0 , nbytes )
54
+
55
+ result << _read_from_socket ( nbytes - result . bytesize , "" . b ) while result . bytesize < nbytes
56
+
57
+ result
58
+ end
41
59
end
42
60
43
61
def gets
@@ -48,9 +66,9 @@ def gets
48
66
@buffer . slice! ( 0 , crlf + CRLF . bytesize )
49
67
end
50
68
51
- def _read_from_socket ( nbytes )
69
+ def _read_from_socket ( nbytes , buffer = nil )
52
70
loop do
53
- case chunk = read_nonblock ( nbytes , exception : false )
71
+ case chunk = read_nonblock ( nbytes , buffer , exception : false )
54
72
when :wait_readable
55
73
unless wait_readable ( @timeout )
56
74
raise Redis ::TimeoutError
0 commit comments