Class: Aws::EventStream::Decoder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws-eventstream/decoder.rb

Overview

This class provides method for decoding binary inputs into single or multiple messages (Aws::EventStream::Message).

  • #decode - decodes messages from an IO like object responds to #read that containing binary data, returning decoded Aws::EventStream::Message along the way or wrapped in an enumerator

## Examples

decoder = Aws::EventStream::Decoder.new

# decoding from IO
decoder.decode(io) do |message|
  message.headers
  # => { ... }
  message.payload
  # => StringIO / Tempfile
end

# alternatively
message_pool = decoder.decode(io)
message_pool.next
# => Aws::EventStream::Message
  • #decode_chunk - decodes a single message from a chunk of data, returning message object followed by boolean(indicating eof status of data) in an array object

## Examples

# chunk containing exactly one message data
message, chunk_eof = decoder.decode_chunk(chunk_str)
message
# => Aws::EventStream::Message
chunk_eof
# => true

# chunk containing a partial message
message, chunk_eof = decoder.decode_chunk(chunk_str)
message
# => nil
chunk_eof
# => true
# chunk data is saved at decoder's message_buffer

# chunk containing more that one data message
message, chunk_eof = decoder.decode_chunk(chunk_str)
message
# => Aws::EventStream::Message
chunk_eof
# => false
# extra chunk data is saved at message_buffer of the decoder

Constant Summary collapse

ONE_MEGABYTE =
1024 * 1024
PRELUDE_LENGTH =

bytes of prelude part, including 4 bytes of total message length, headers length and crc checksum of prelude

12
OVERHEAD_LENGTH =

bytes of total overhead in a message, including prelude and 4 bytes total message crc checksum

16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Decoder

Returns a new instance of Decoder.



79
80
81
82
# File 'lib/aws-eventstream/decoder.rb', line 79

def initialize(options = {})
  @format = options.fetch(:format, true)
  @message_buffer = BytesBuffer.new('')
end

Instance Attribute Details

#message_bufferObject (readonly)

Returns the value of attribute message_buffer.



85
86
87
# File 'lib/aws-eventstream/decoder.rb', line 85

def message_buffer
  @message_buffer
end

Instance Method Details

#decode(io) {|message| ... } ⇒ Enumerable<Message>?

Decodes messages from a binary stream

Parameters:

  • io (IO#read)

    An IO-like object that responds to ‘#read`

Yield Parameters:

Returns:

  • (Enumerable<Message>, nil)

    Returns a new Enumerable containing decoded messages if no block is given



95
96
97
98
99
100
101
102
# File 'lib/aws-eventstream/decoder.rb', line 95

def decode(io, &block)
  io = BytesBuffer.new(io.read)
  return decode_io(io) unless block_given?
  until io.eof?
    # fetch message only
    yield(decode_message(io).first)
  end
end

#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>

Decodes a single message from a chunk of string

Parameters:

  • chunk (String) (defaults to: nil)

    A chunk of string to be decoded, chunk can contain partial event message to multiple event messages When not provided, decode data from #message_buffer

Returns:

  • (Array<Message|nil, Boolean>)

    Returns single decoded message and boolean pair, the boolean flag indicates whether this chunk has been fully consumed, unused data is tracked at #message_buffer



113
114
115
116
117
# File 'lib/aws-eventstream/decoder.rb', line 113

def decode_chunk(chunk = nil)
  @message_buffer.write(chunk) if chunk
  @message_buffer.rewind
  decode_message(@message_buffer)
end