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
Constant Summary collapse
- NEWLINE =
The default line separator, used by Reader#gets.
"\n"
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
- #The output stream.(outputstream.) ⇒ Object readonly
Instance Method Summary collapse
-
#<<(buffer) ⇒ Object
Write data to the stream using #write.
-
#close(error = nil) ⇒ Object
Close the input and output bodies.
-
#close_read(error = nil) ⇒ Object
Close the input body.
-
#close_write(error = nil) ⇒ Object
Close the output body.
- #closed? ⇒ Boolean
- #empty? ⇒ Boolean
-
#flush ⇒ Object
Flush the output stream.
-
#initialize(input = nil, output = Buffered.new) ⇒ Stream
constructor
Initialize the stream with the given input and output.
-
#inspect ⇒ Object
Inspect the stream.
-
#puts(*arguments, separator: NEWLINE) ⇒ Object
Write lines to the stream.
- #The input stream.=(inputstream. = (value)) ⇒ Object
-
#write(buffer) ⇒ Object
Write data to the underlying stream.
-
#write_nonblock(buffer, exception: nil) ⇒ Object
Write data to the stream using #write.
Methods included from Reader
#each, #gets, #read, #read_nonblock, #read_partial, #read_until, #readpartial
Constructor Details
#initialize(input = nil, output = Buffered.new) ⇒ Stream
Initialize the stream with the given input and output.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/protocol/http/body/stream.rb', line 22 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.
36 37 38 |
# File 'lib/protocol/http/body/stream.rb', line 36 def input @input end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
39 40 41 |
# File 'lib/protocol/http/body/stream.rb', line 39 def output @output end |
#The output stream.(outputstream.) ⇒ Object (readonly)
39 |
# File 'lib/protocol/http/body/stream.rb', line 39 attr :output |
Instance Method Details
#<<(buffer) ⇒ Object
Write data to the stream using #write.
319 320 321 |
# File 'lib/protocol/http/body/stream.rb', line 319 def << buffer write(buffer) end |
#close(error = nil) ⇒ Object
Close the input and output bodies.
376 377 378 379 380 381 382 383 |
# File 'lib/protocol/http/body/stream.rb', line 376 def close(error = nil) self.close_read(error) self.close_write(error) return nil ensure @closed = true end |
#close_read(error = nil) ⇒ Object
Close the input body.
If, while processing the data that was read from this stream, an error is encountered, it should be passed to this method.
350 351 352 353 354 355 356 357 358 |
# File 'lib/protocol/http/body/stream.rb', line 350 def close_read(error = nil) if input = @input @input = nil @closed_read = true @buffer = nil input.close(error) end end |
#close_write(error = nil) ⇒ Object
Close the output body.
If, while generating the data that is written to this stream, an error is encountered, it should be passed to this method.
365 366 367 368 369 370 371 |
# File 'lib/protocol/http/body/stream.rb', line 365 def close_write(error = nil) if output = @output @output = nil output.close_write(error) end end |
#closed? ⇒ Boolean
386 387 388 |
# File 'lib/protocol/http/body/stream.rb', line 386 def closed? @closed end |
#empty? ⇒ Boolean
406 407 408 |
# File 'lib/protocol/http/body/stream.rb', line 406 def empty? @output.empty? end |
#flush ⇒ Object
Flush the output stream.
This is currently a no-op.
342 343 |
# File 'lib/protocol/http/body/stream.rb', line 342 def flush end |
#inspect ⇒ Object
Inspect the stream.
393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/protocol/http/body/stream.rb', line 393 def inspect buffer_info = @buffer ? "#{@buffer.bytesize} bytes buffered" : "no buffer" status = [] status << "closed" if @closed status << "read-closed" if @closed_read status_info = status.empty? ? "open" : status.join(", ") return "#<#{self.class} #{buffer_info}, #{status_info}>" end |
#puts(*arguments, separator: NEWLINE) ⇒ Object
Write lines to the stream.
The current implementation buffers the lines and writes them in a single operation.
329 330 331 332 333 334 335 336 337 |
# File 'lib/protocol/http/body/stream.rb', line 329 def puts(*arguments, separator: NEWLINE) buffer = ::String.new arguments.each do |argument| buffer << argument << separator end write(buffer) end |
#The input stream.=(inputstream. = (value)) ⇒ Object
36 |
# File 'lib/protocol/http/body/stream.rb', line 36 attr :input |
#write(buffer) ⇒ Object
Write data to the underlying stream.
298 299 300 301 302 303 304 305 |
# File 'lib/protocol/http/body/stream.rb', line 298 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, exception: nil) ⇒ Object
Write data to the stream using #write.
Provided for compatibility with IO-like objects.
314 315 316 |
# File 'lib/protocol/http/body/stream.rb', line 314 def write_nonblock(buffer, exception: nil) write(buffer) end |