Class: Async::HTTP::Body::Stream
- Inherits:
-
Object
- Object
- Async::HTTP::Body::Stream
- Defined in:
- lib/async/http/body/stream.rb
Overview
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.
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #close ⇒ Object
- #close_read ⇒ Object
-
#close_write ⇒ Object
close must never be called on the input stream.
- #flush ⇒ Object
-
#initialize(input, output = Writable.new) ⇒ Stream
constructor
A new instance of Stream.
-
#read(length = nil, buffer = nil) ⇒ Object
read behaves like IO#read.
- #read_nonblock(length, buffer = nil) ⇒ Object
- #write(buffer) ⇒ Object (also: #write_nonblock)
Constructor Details
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
36 37 38 |
# File 'lib/async/http/body/stream.rb', line 36 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
37 38 39 |
# File 'lib/async/http/body/stream.rb', line 37 def output @output end |
Instance Method Details
#close ⇒ Object
113 114 115 116 |
# File 'lib/async/http/body/stream.rb', line 113 def close self.close_read self.close_write end |
#close_read ⇒ Object
104 105 106 |
# File 'lib/async/http/body/stream.rb', line 104 def close_read @input&.close end |
#close_write ⇒ Object
close must never be called on the input stream. huh?
109 110 111 |
# File 'lib/async/http/body/stream.rb', line 109 def close_write @output&.close end |
#flush ⇒ Object
101 102 |
# File 'lib/async/http/body/stream.rb', line 101 def flush end |
#read(length = nil, buffer = nil) ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/async/http/body/stream.rb', line 46 def read(length = nil, buffer = nil) 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, @buffer.bytesize) else buffer << @buffer @buffer = nil end end return nil if buffer.empty? && length && length > 0 return buffer end |
#read_nonblock(length, buffer = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/async/http/body/stream.rb', line 74 def read_nonblock(length, buffer = nil) @buffer ||= read_next chunk = 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 |
#write(buffer) ⇒ Object Also known as: write_nonblock
95 96 97 |
# File 'lib/async/http/body/stream.rb', line 95 def write(buffer) @output.write(buffer) end |