class Webmachine::Streaming::IOEncoder

Implements a streaming encoder for IO response bodies, such as File objects. @api private

Constants

CHUNK_SIZE

Public Instance Methods

bytesize()
Alias for: size
copy_stream(outstream) click to toggle source

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
each() { |send(encoder, send)| ... } click to toggle source

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
empty?() click to toggle source
# File lib/webmachine/streaming/io_encoder.rb, line 59
def empty?
  size == 0
end
size() click to toggle source

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
Also aliased as: bytesize
to_io() click to toggle source

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

can_copy_stream?() click to toggle source
# File lib/webmachine/streaming/io_encoder.rb, line 67
def can_copy_stream?
  IO.respond_to?(:copy_stream) && is_unencoded? && !is_string_io?
end
is_string_io?() click to toggle source
# File lib/webmachine/streaming/io_encoder.rb, line 71
def is_string_io?
  StringIO === body
end