Class: CBOR::Streaming::BufferedDecoder
- Inherits:
-
Object
- Object
- CBOR::Streaming::BufferedDecoder
- Defined in:
- lib/libcbor/streaming/buffered_decoder.rb
Overview
Decodes a stream of data and invokes the appropriate callbacks
To use it, just initialize it with the desired set of callbacks and start feeding data to it. Please see network_streaming for an example
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Instance Method Summary collapse
-
#<<(data) ⇒ void
Append data to the internal buffer.
-
#callback(name, *args) ⇒ void
private
Invocation target for the callback proxy.
-
#initialize(callbacks = {}) ⇒ BufferedDecoder
constructor
A new instance of BufferedDecoder.
Constructor Details
#initialize(callbacks = {}) ⇒ BufferedDecoder
Returns a new instance of BufferedDecoder.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/libcbor/streaming/buffered_decoder.rb', line 25 def initialize(callbacks = {}) @callbacks = { integer: Proc.new {}, string: Proc.new {}, chunked_string_start: Proc.new {}, byte_string: Proc.new {}, chunked_byte_string_start: Proc.new {}, float: Proc.new {}, definite_array: Proc.new {}, array_start: Proc.new {}, definite_map: Proc.new {}, map_start: Proc.new {}, tag: Proc.new {}, bool: Proc.new {}, null: Proc.new {}, simple: Proc.new {}, break: Proc.new {}, }.merge(callbacks) @buffer = '' @proxy = CallbackSimplifier.new(self) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
88 89 90 |
# File 'lib/libcbor/streaming/buffered_decoder.rb', line 88 def buffer @buffer end |
Instance Method Details
#<<(data) ⇒ void
This method returns an undefined value.
Append data to the internal buffer.
Parsing will ensue and all the appropriate callbacks will be invoked. The method will block until all the callbacks have finished.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/libcbor/streaming/buffered_decoder.rb', line 55 def <<(data) @buffer += data loop do result = LibCBOR.cbor_stream_decode( FFI::MemoryPointer.from_string(@buffer), @buffer.bytes.length, @proxy.callback_set.to_ptr, nil ) read = result[:read] break if read == 0 && result[:status] == :not_enough_data unless result[:status] == :finished raise DecodingError, "Invalid input near byte #{read} of the buffer." end @buffer = @buffer[read .. -1] break if buffer.empty? end end |
#callback(name, *args) ⇒ void
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.
This method returns an undefined value.
Invocation target for the callback proxy. Do not use.
84 85 86 |
# File 'lib/libcbor/streaming/buffered_decoder.rb', line 84 def callback(name, *args) callbacks[name].call(*args) end |