Class: CBOR::Streaming::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/libcbor/streaming/encoder.rb

Overview

Provides streaming encoding facilities. Initialize it with an output object and either feed it encodable objects, or invoke the methods directly

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Encoder

Returns a new instance of Encoder.

Parameters:

  • stream (StringIO, Socket, File, #write)

    Output object. Must support write method that takes a String as a parametr



9
10
11
# File 'lib/libcbor/streaming/encoder.rb', line 9

def initialize(stream)
	@stream = stream
end

Instance Method Details

#<<(object) ⇒ void

This method returns an undefined value.

Serializes and writes an object to the stream

Parameters:

  • object (#to_cbor)

    Object to serialize and write



17
18
19
# File 'lib/libcbor/streaming/encoder.rb', line 17

def <<(object)
	stream.write(object.public_send(CBOR.method_name))
end

#breakvoid

This method returns an undefined value.

Encodes indefinite item break code.



70
71
72
# File 'lib/libcbor/streaming/encoder.rb', line 70

def break
	stream.write("\xff")
end

#start_arrayvoid

This method returns an undefined value.

Encodes a ‘start array’ mark. You are responsible for correctly #breaking it



25
26
27
# File 'lib/libcbor/streaming/encoder.rb', line 25

def start_array
	stream.write("\x9f")
end

#start_chunked_byte_stringvoid

This method returns an undefined value.

Encodes a ‘start indefinite byte string’ mark. You are responsible for correctly #breaking it



49
50
51
# File 'lib/libcbor/streaming/encoder.rb', line 49

def start_chunked_byte_string
	stream.write("\x5f")
end

#start_chunked_stringvoid

This method returns an undefined value.

Encodes a ‘start indefinite string’ mark. You are responsible for correctly #breaking it



41
42
43
# File 'lib/libcbor/streaming/encoder.rb', line 41

def start_chunked_string
	stream.write("\x7f")
end

#start_mapvoid

This method returns an undefined value.

Encodes a ‘start map’ mark. You are responsible for correctly #breaking it



33
34
35
# File 'lib/libcbor/streaming/encoder.rb', line 33

def start_map
	stream.write("\xbf")
end

#tag(value) ⇒ void

This method returns an undefined value.

Encodes a tag with the give value.

You are responsible for correctly supplying the item that follows (i.e. the one the tag will apply to)

Parameters:

  • value (Fixnum)

    Tag value.



60
61
62
63
64
65
# File 'lib/libcbor/streaming/encoder.rb', line 60

def tag(value)
	@@bfr ||= FFI::Buffer.new(:uchar, 9)
	stream.write(
		@@bfr.get_bytes(0, LibCBOR.cbor_encode_tag(value, @@bfr, 9))
	)
end