Class: Webmachine::Streaming::IOEncoder Private

Inherits:
Encoder
  • Object
show all
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

Methods inherited from Encoder

#initialize

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.

Parameters:

  • outstream (IO)

    the output stream to copy the body into



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.

Yields:

  • (chunk)

Yield Parameters:

  • chunk (String)

    a chunk of the response, encoded



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.

Returns:

  • (Boolean)


59
60
61
# File 'lib/webmachine/streaming/io_encoder.rb', line 59

def empty?
  size == 0
end

#sizeInteger 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.

Returns:

  • (Integer)

    the size, in bytes, of the underlying IO, or nil if unsupported



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_ioIO?

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.

Returns:

  • (IO, nil)

    the body as a IO object, or nil.



35
36
37
# File 'lib/webmachine/streaming/io_encoder.rb', line 35

def to_io
  IO.try_convert(body)
end