Class: Protocol::HTTP::Body::Stream
- Inherits:
-
Object
- Object
- Protocol::HTTP::Body::Stream
- Includes:
- Reader
- Defined in:
- lib/protocol/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.
Defined Under Namespace
Modules: Reader
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
- #<<(buffer) ⇒ Object
-
#close(error = nil) ⇒ Object
Close the input and output bodies.
- #close_read ⇒ Object
-
#close_write ⇒ Object
close must never be called on the input stream.
-
#closed? ⇒ Boolean
Whether the stream has been closed.
-
#empty? ⇒ Boolean
Whether there are any output chunks remaining?.
- #flush ⇒ Object
-
#initialize(input = nil, output = Buffered.new) ⇒ Stream
constructor
A new instance of Stream.
- #write(buffer) ⇒ Object
- #write_nonblock(buffer) ⇒ Object
Methods included from Reader
#read, #read_nonblock, #read_partial
Constructor Details
#initialize(input = nil, output = Buffered.new) ⇒ Stream
Returns a new instance of Stream.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/protocol/http/body/stream.rb', line 14 def initialize(input = nil, output = Buffered.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 @closed_read = false end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
26 27 28 |
# File 'lib/protocol/http/body/stream.rb', line 26 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
27 28 29 |
# File 'lib/protocol/http/body/stream.rb', line 27 def output @output end |
Instance Method Details
#<<(buffer) ⇒ Object
143 144 145 |
# File 'lib/protocol/http/body/stream.rb', line 143 def <<(buffer) write(buffer) end |
#close(error = nil) ⇒ Object
Close the input and output bodies.
165 166 167 168 169 170 171 172 |
# File 'lib/protocol/http/body/stream.rb', line 165 def close(error = nil) self.close_read self.close_write return nil ensure @closed = true end |
#close_read ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/protocol/http/body/stream.rb', line 150 def close_read @closed_read = true @buffer = nil @input&.close @input = nil end |
#close_write ⇒ Object
close must never be called on the input stream. huh?
159 160 161 162 |
# File 'lib/protocol/http/body/stream.rb', line 159 def close_write @output&.close @output = nil end |
#closed? ⇒ Boolean
Whether the stream has been closed.
175 176 177 |
# File 'lib/protocol/http/body/stream.rb', line 175 def closed? @closed end |
#empty? ⇒ Boolean
Whether there are any output chunks remaining?
180 181 182 |
# File 'lib/protocol/http/body/stream.rb', line 180 def empty? @output.empty? end |
#flush ⇒ Object
147 148 |
# File 'lib/protocol/http/body/stream.rb', line 147 def flush end |
#write(buffer) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/protocol/http/body/stream.rb', line 130 def write(buffer) if @output @output.write(buffer) return buffer.bytesize else raise IOError, "Stream is not writable, output has been closed!" end end |
#write_nonblock(buffer) ⇒ Object
139 140 141 |
# File 'lib/protocol/http/body/stream.rb', line 139 def write_nonblock(buffer) write(buffer) end |