class Async::HTTP::Body::Stream

The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.

Attributes

input[R]
output[R]

Public Class Methods

new(input, output = Writable.new) click to toggle source
# File lib/async/http/body/stream.rb, line 30
def initialize(input, output = Writable.new)
        @input = input
        @output = output
        
        raise ArgumentError, "Non-writable output!" unless output.respond_to?(:write)
        
        # Will hold remaining data in `#read`.
        @buffer = nil
        @closed = false
end

Public Instance Methods

close(error = nil) click to toggle source

Close the input and output bodies.

# File lib/async/http/body/stream.rb, line 142
def close(error = nil)
        self.close_read
        self.close_write
ensure
        @closed = true
end
close_read() click to toggle source
# File lib/async/http/body/stream.rb, line 133
def close_read
        @input&.close
end
close_write() click to toggle source
# File lib/async/http/body/stream.rb, line 137
def close_write
        @output&.close
end
closed?() click to toggle source

Whether the stream has been closed.

# File lib/async/http/body/stream.rb, line 150
def closed?
        @closed
end
empty?() click to toggle source

Whether there are any output chunks remaining?

# File lib/async/http/body/stream.rb, line 155
def empty?
        @output.empty?
end
flush() click to toggle source
# File lib/async/http/body/stream.rb, line 130
def flush
end
read(size = nil, buffer = nil) click to toggle source

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

# File lib/async/http/body/stream.rb, line 51
def read(size = nil, buffer = nil)
        return '' if size == 0
        
        buffer ||= Async::IO::Buffer.new
        if @buffer
                buffer.replace(@buffer)
                @buffer = nil
        end
        
        if size
                while buffer.bytesize < size and chunk = read_next
                        buffer << chunk
                end
                
                @buffer = buffer.byteslice(size, buffer.bytesize)
                buffer = buffer.byteslice(0, size)
                
                if buffer.empty?
                        return nil
                else
                        return buffer
                end
        else
                while chunk = read_next
                        buffer << chunk
                end
                
                return buffer
        end
end
read_nonblock(length, buffer = nil) click to toggle source
# File lib/async/http/body/stream.rb, line 101
def read_nonblock(length, buffer = nil)
        @buffer ||= read_next
        chunk = nil
        
        return nil if @buffer.nil?
        
        if @buffer.bytesize > length
                chunk = @buffer.byteslice(0, length)
                @buffer = @buffer.byteslice(length, @buffer.bytesize)
        else
                chunk = @buffer
                @buffer = nil
        end
        
        if buffer
                buffer.replace(chunk)
        else
                buffer = chunk
        end
        
        return buffer
end
read_partial(size = nil) click to toggle source

Read at most `size` bytes from the stream. Will avoid reading from the underlying stream if possible.

# File lib/async/http/body/stream.rb, line 83
def read_partial(size = nil)
        if @buffer
                buffer = @buffer
                @buffer = nil
        else
                buffer = read_next
        end
        
        if buffer and size
                if buffer.bytesize > size
                        @buffer = buffer.byteslice(size, buffer.bytesize)
                        buffer = buffer.byteslice(0, size)
                end
        end
        
        return buffer
end
write(buffer) click to toggle source
# File lib/async/http/body/stream.rb, line 124
def write(buffer)
        @output.write(buffer)
end
Also aliased as: write_nonblock
write_nonblock(buffer)
Alias for: write

Private Instance Methods

read_next() click to toggle source
# File lib/async/http/body/stream.rb, line 161
def read_next
        if chunk = @input&.read
                return chunk
        else
                @input = nil
                return nil
        end
end