Class: CBOR::Streaming::BufferedDecoder

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

Instance Method Summary collapse

Constructor Details

#initialize(callbacks = {}) ⇒ BufferedDecoder

Returns a new instance of BufferedDecoder.

Parameters:

  • callbacks (Hash) (defaults to: {})

    the callbacks to invoke during parsing.

Options Hash (callbacks):

  • :integer (Proc<Fixnum>)

    Integers, both positive and negative

  • :string (Proc<String>)

    Definite string

  • :chunked_string_start (Proc)

    Chunked string. Chunks follow

  • :byte_string (Proc<String>)

    Definite byte string

  • :chunked_byte_string_start (Proc)

    Chunked byte string. Chunks follow

  • :float (Proc<Float>)

    Float

  • :definite_array (Proc<length: Fixnum>)

    Definite array

  • :array_start (Proc)

    Indefinite array start

  • :definite_map (Proc<length: Fixnum>)

    Definite map. Length pairs follow

  • :map_start (Proc)

    Indefinite map start

  • :tag (Proc<value: Fixnum>)

    Tag. Tagged item follows

  • :bool (Proc<Bool>)

    Boolean

  • :null (Proc)

    Null

  • :simple (Proc<value: Fixnum>)

    Simple value other than true, false, nil

  • :break (Proc)

    Indefinite item break



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

#bufferObject (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.

Parameters:

  • data (String)

    CBOR input data

Raises:



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.

Parameters:

  • name (Symbol)

    Callback name

  • args (Array)

    Arguments to pass on



84
85
86
# File 'lib/libcbor/streaming/buffered_decoder.rb', line 84

def callback(name, *args)
	callbacks[name].call(*args)
end