lib/falcon/adapters/input.rb in falcon-0.17.5 vs lib/falcon/adapters/input.rb in falcon-0.17.6

- old
+ new

@@ -16,10 +16,12 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +require 'async/io/buffer' + require 'async/http/body' require 'async/http/body/rewindable' module Falcon module Adapters @@ -65,21 +67,25 @@ # read behaves like IO#read. Its signature is read([length, [buffer]]). If given, length must be a non-negative Integer (>= 0) or nil, and buffer must be a String and may not be nil. If length is given and not nil, then this method reads at most length bytes from the input stream. If length is not given or nil, then this method reads all data until EOF. When EOF is reached, this method returns nil if length is given and not nil, or “” if length is not given or is nil. If buffer is given, then the read data will be placed into buffer instead of a newly created String object. # @param length [Integer] the amount of data to read # @param buffer [String] the buffer which will receive the data # @return a buffer containing the data def read(length = nil, buffer = nil) - buffer ||= Async::IO::BinaryString.new + buffer ||= Async::IO::Buffer.new buffer.clear until buffer.bytesize == length @buffer = read_next if @buffer.nil? break if @buffer.nil? remaining_length = length - buffer.bytesize if length if remaining_length && remaining_length < @buffer.bytesize + # We know that we are not going to reuse the original buffer. + # But byteslice will generate a hidden copy. So let's freeze it first: + @buffer.freeze + buffer << @buffer.byteslice(0, remaining_length) - @buffer = @buffer.byteslice(remaining_length..-1) + @buffer = @buffer.byteslice(remaining_length, @buffer.bytesize) else buffer << @buffer @buffer = nil end end