Class: Takagi::Serialization::OctetStreamSerializer

Inherits:
Base
  • Object
show all
Defined in:
lib/takagi/serialization/octet_stream_serializer.rb

Overview

Octet-stream serializer (RFC 2046)

Handles application/octet-stream content-format (code 42). Pass-through for raw binary data with no transformation.

Examples:

Encoding

serializer = OctetStreamSerializer.new
bytes = serializer.encode("\x00\x01\x02")  # => "\x00\x01\x02" (unchanged)

Decoding

data = serializer.decode(bytes)  # => raw bytes (unchanged)

Instance Method Summary collapse

Methods inherited from Base

#name

Instance Method Details

#binary?Boolean

Octet-stream is binary

Returns:

  • (Boolean)

    true



77
78
79
# File 'lib/takagi/serialization/octet_stream_serializer.rb', line 77

def binary?
  true
end

#content_format_codeInteger

CoAP content-format code for octet-stream

Returns:

  • (Integer)

    42 (RFC 7252 §12.3)



70
71
72
# File 'lib/takagi/serialization/octet_stream_serializer.rb', line 70

def content_format_code
  CoAP::Registries::ContentFormat::OCTET_STREAM
end

#content_typeString

MIME type for octet-stream

Returns:

  • (String)

    ‘application/octet-stream’



63
64
65
# File 'lib/takagi/serialization/octet_stream_serializer.rb', line 63

def content_type
  'application/octet-stream'
end

#decode(bytes) ⇒ String

Decode binary bytes

Pass-through decoder - bytes are returned as-is.

Examples:

Binary data pass-through

decode(bytes)  # => bytes (unchanged)

Parameters:

  • bytes (String)

    Binary data

Returns:

  • (String)

    Binary string (ASCII-8BIT)

Raises:



51
52
53
54
55
56
57
58
# File 'lib/takagi/serialization/octet_stream_serializer.rb', line 51

def decode(bytes)
  return nil if bytes.nil? || bytes.empty?

  # Return as binary string
  bytes.b
rescue StandardError => e
  raise DecodeError, "Octet-stream decoding failed: #{e.message}"
end

#encode(data) ⇒ String

Encode data to binary bytes

Pass-through serializer - data is returned as-is in binary form. Non-string objects are converted to string first.

Examples:

Binary data pass-through

encode("\x00\x01\x02")  # => "\x00\x01\x02"

String to binary

encode("hello")  # => "hello" (as binary)

Parameters:

  • data (Object)

    Data to encode

Returns:

  • (String)

    Binary string (ASCII-8BIT)

Raises:



31
32
33
34
35
36
37
38
39
# File 'lib/takagi/serialization/octet_stream_serializer.rb', line 31

def encode(data)
  return ''.b if data.nil? || data == ''

  # Convert to binary string
  result = data.is_a?(String) ? data : data.to_s
  result.b
rescue StandardError => e
  raise EncodeError, "Octet-stream encoding failed: #{e.message}"
end