Class: Aws::EventStream::Decoder
- Inherits:
-
Object
- Object
- Aws::EventStream::Decoder
- 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 ||
.headers
# => { ... }
.payload
# => StringIO / Tempfile
end
# alternatively
= decoder.decode(io)
.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
, chunk_eof = decoder.decode_chunk(chunk_str)
# => Aws::EventStream::Message
chunk_eof
# => true
# chunk containing a partial message
, chunk_eof = decoder.decode_chunk(chunk_str)
# => nil
chunk_eof
# => true
# chunk data is saved at decoder's message_buffer
# chunk containing more that one data message
, chunk_eof = decoder.decode_chunk(chunk_str)
# => 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
-
#message_buffer ⇒ Object
readonly
Returns the value of attribute message_buffer.
Instance Method Summary collapse
-
#decode(io) {|message| ... } ⇒ Enumerable<Message>?
Decodes messages from a binary stream.
-
#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>
Decodes a single message from a chunk of string.
-
#initialize(options = {}) ⇒ Decoder
constructor
A new instance of Decoder.
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( = {}) @format = .fetch(:format, true) @message_buffer = BytesBuffer.new('') end |
Instance Attribute Details
#message_buffer ⇒ Object (readonly)
Returns the value of attribute message_buffer.
85 86 87 |
# File 'lib/aws-eventstream/decoder.rb', line 85 def @message_buffer end |
Instance Method Details
#decode(io) {|message| ... } ⇒ Enumerable<Message>?
Decodes messages from a binary stream
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((io).first) end end |
#decode_chunk(chunk = nil) ⇒ Array<Message|nil, Boolean>
Decodes a single message from a chunk of string
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 (@message_buffer) end |