Class: Webmachine::Streaming::IOEncoder Private
- Includes:
- Enumerable
- Defined in:
- lib/webmachine/streaming/io_encoder.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Implements a streaming encoder for IO response bodies, such as File objects.
Constant Summary collapse
- CHUNK_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
8192
Instance Attribute Summary
Attributes inherited from Encoder
#body, #charsetter, #encoder, #resource
Instance Method Summary collapse
-
#copy_stream(outstream) ⇒ Object
private
If IO#copy_stream is supported, and the stream is unencoded, optimize the output by copying directly.
-
#each {|chunk| ... } ⇒ Object
private
Iterates over the IO, encoding and yielding individual chunks of the response entity.
- #empty? ⇒ Boolean private
-
#size ⇒ Fixnum
(also: #bytesize)
private
Returns the length of the IO stream, if known.
-
#to_io ⇒ IO?
private
Allows the response body to be converted to a IO object.
Methods inherited from Encoder
Constructor Details
This class inherits a constructor from Webmachine::Streaming::Encoder
Instance Method Details
#copy_stream(outstream) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
If IO#copy_stream is supported, and the stream is unencoded, optimize the output by copying directly. Otherwise, defers to using #each.
25 26 27 28 29 30 31 |
# File 'lib/webmachine/streaming/io_encoder.rb', line 25 def copy_stream(outstream) if can_copy_stream? IO.copy_stream(body, outstream) else each {|chunk| outstream << chunk } end end |
#each {|chunk| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iterates over the IO, encoding and yielding individual chunks of the response entity.
15 16 17 18 19 |
# File 'lib/webmachine/streaming/io_encoder.rb', line 15 def each while chunk = body.read(CHUNK_SIZE) and chunk != "" yield resource.send(encoder, resource.send(charsetter, chunk)) end end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 |
# File 'lib/webmachine/streaming/io_encoder.rb', line 59 def empty? size == 0 end |
#size ⇒ Fixnum Also known as: bytesize
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the length of the IO stream, if known. Returns nil if the stream uses an encoder or charsetter that might modify the length of the stream, or the stream size is unknown.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/webmachine/streaming/io_encoder.rb', line 44 def size if is_unencoded? if is_string_io? body.size else begin body.stat.size rescue SystemCallError # IO objects might raise an Errno if stat is unsupported. nil end end end end |
#to_io ⇒ IO?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Allows the response body to be converted to a IO object.
35 36 37 |
# File 'lib/webmachine/streaming/io_encoder.rb', line 35 def to_io IO.try_convert(body) end |