class Webmachine::Streaming::IOEncoder
Implements a streaming encoder for IO response bodies, such as File objects. @api private
Constants
- CHUNK_SIZE
Public Instance Methods
If IO#copy_stream is supported, and the stream is unencoded, optimize the output by copying directly. Otherwise, defers to using each
. @param [IO] outstream the output stream to copy the body into
# 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
Iterates over the IO, encoding and yielding individual chunks of the response entity. @yield [chunk] @yieldparam [String] chunk a chunk of the response, encoded
# File lib/webmachine/streaming/io_encoder.rb, line 15 def each while (chunk = body.read(CHUNK_SIZE)) && (chunk != '') yield resource.send(encoder, resource.send(charsetter, chunk)) end end
# File lib/webmachine/streaming/io_encoder.rb, line 59 def empty? size == 0 end
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. @return [Integer] the size, in bytes, of the underlying IO, or
nil if unsupported
# 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
Allows the response body to be converted to a IO object. @return [IO,nil] the body as a IO object, or nil.
# File lib/webmachine/streaming/io_encoder.rb, line 35 def to_io IO.try_convert(body) end
Private Instance Methods
# File lib/webmachine/streaming/io_encoder.rb, line 67 def can_copy_stream? IO.respond_to?(:copy_stream) && is_unencoded? && !is_string_io? end
# File lib/webmachine/streaming/io_encoder.rb, line 71 def is_string_io? StringIO === body end