Class: AMQ::Protocol::TableValueEncoder

### Home Classes Methods

### In Files

- lib/amq/protocol/table_value_encoder.rb

### Parent

Object

### Methods

- ::array_size
- ::encode
- ::field_value_size

### Included Modules

- AMQ::Protocol::TypeConstants

### Class/Module Index

Quicksearch

- AMQ
- AMQ::BitSet
- AMQ::Endianness
- AMQ::Hacks
- AMQ::IntAllocator
- AMQ::Pack
- AMQ::Protocol
- AMQ::Protocol::AccessRefused
- AMQ::Protocol::BadResponseError
- AMQ::Protocol::Basic
- AMQ::Protocol::Basic::Ack
- AMQ::Protocol::Basic::Cancel
- AMQ::Protocol::Basic::CancelOk
- AMQ::Protocol::Basic::Consume
- AMQ::Protocol::Basic::ConsumeOk
- AMQ::Protocol::Basic::Deliver
- AMQ::Protocol::Basic::Get
- AMQ::Protocol::Basic::GetEmpty
- AMQ::Protocol::Basic::GetOk
- AMQ::Protocol::Basic::Nack
- AMQ::Protocol::Basic::Publish
- AMQ::Protocol::Basic::Qos
- AMQ::Protocol::Basic::QosOk
- AMQ::Protocol::Basic::Recover
- AMQ::Protocol::Basic::RecoverAsync
- AMQ::Protocol::Basic::RecoverOk
- AMQ::Protocol::Basic::Reject
- AMQ::Protocol::Basic::Return
- AMQ::Protocol::BodyFrame
- AMQ::Protocol::Channel
- AMQ::Protocol::Channel::Close
- AMQ::Protocol::Channel::CloseOk
- AMQ::Protocol::Channel::Flow
- AMQ::Protocol::Channel::FlowOk
- AMQ::Protocol::Channel::Open
- AMQ::Protocol::Channel::OpenOk
- AMQ::Protocol::ChannelError
- AMQ::Protocol::Class
- AMQ::Protocol::CommandInvalid
- AMQ::Protocol::Confirm
- AMQ::Protocol::Confirm::Select
- AMQ::Protocol::Confirm::SelectOk
- AMQ::Protocol::Connection
- AMQ::Protocol::Connection::Blocked
- AMQ::Protocol::Connection::Close
- AMQ::Protocol::Connection::CloseOk
- AMQ::Protocol::Connection::Open
- AMQ::Protocol::Connection::OpenOk
- AMQ::Protocol::Connection::Secure
- AMQ::Protocol::Connection::SecureOk
- AMQ::Protocol::Connection::Start
- AMQ::Protocol::Connection::StartOk
- AMQ::Protocol::Connection::Tune
- AMQ::Protocol::Connection::TuneOk
- AMQ::Protocol::Connection::Unblocked
- AMQ::Protocol::ConnectionForced
- AMQ::Protocol::ContentTooLarge
- AMQ::Protocol::EmptyResponseError
- AMQ::Protocol::Error
- AMQ::Protocol::Exchange
- AMQ::Protocol::Exchange::Bind
- AMQ::Protocol::Exchange::BindOk
- AMQ::Protocol::Exchange::Declare
- AMQ::Protocol::Exchange::DeclareOk
- AMQ::Protocol::Exchange::Delete
- AMQ::Protocol::Exchange::DeleteOk
- AMQ::Protocol::Exchange::Unbind
- AMQ::Protocol::Exchange::UnbindOk
- AMQ::Protocol::Float32Bit
- AMQ::Protocol::Frame
- AMQ::Protocol::FrameError
- AMQ::Protocol::FrameSubclass
- AMQ::Protocol::FrameTypeError
- AMQ::Protocol::HardError
- AMQ::Protocol::HeaderFrame
- AMQ::Protocol::HeartbeatFrame
- AMQ::Protocol::InternalError
- AMQ::Protocol::InvalidPath
- AMQ::Protocol::Method
- AMQ::Protocol::MethodFrame
- AMQ::Protocol::NoConsumers
- AMQ::Protocol::NoRoute
- AMQ::Protocol::NotAllowed
- AMQ::Protocol::NotFound
- AMQ::Protocol::NotImplemented
- AMQ::Protocol::PreconditionFailed
- AMQ::Protocol::Queue
- AMQ::Protocol::Queue::Bind
- AMQ::Protocol::Queue::BindOk
- AMQ::Protocol::Queue::Declare
- AMQ::Protocol::Queue::DeclareOk
- AMQ::Protocol::Queue::Delete
- AMQ::Protocol::Queue::DeleteOk
- AMQ::Protocol::Queue::Purge
- AMQ::Protocol::Queue::PurgeOk
- AMQ::Protocol::Queue::Unbind
- AMQ::Protocol::Queue::UnbindOk
- AMQ::Protocol::ResourceError
- AMQ::Protocol::ResourceLocked
- AMQ::Protocol::SoftError
- AMQ::Protocol::SyntaxError
- AMQ::Protocol::Table
- AMQ::Protocol::Table::InvalidTableError
- AMQ::Protocol::TableValueDecoder
- AMQ::Protocol::TableValueEncoder
- AMQ::Protocol::Tx
- AMQ::Protocol::Tx::Commit
- AMQ::Protocol::Tx::CommitOk
- AMQ::Protocol::Tx::Rollback
- AMQ::Protocol::Tx::RollbackOk
- AMQ::Protocol::Tx::Select
- AMQ::Protocol::Tx::SelectOk
- AMQ::Protocol::TypeConstants
- AMQ::Protocol::UnexpectedFrame
- AMQ::URI

No matching classes.

# AMQ::Protocol::TableValueEncoder

### Public Class Methods

array_size(value) click to toggle source

    # File lib/amq/protocol/table_value_encoder.rb, line 112
    def self.array_size(value)
      acc = 0
      value.each { |v| acc += self.field_value_size(v) }

      acc
    end

encode(value) click to toggle source

API

    # File lib/amq/protocol/table_value_encoder.rb, line 24
    def self.encode(value)
      accumulator = String.new

      case value
      when String then
        accumulator << TYPE_STRING
        accumulator << [value.bytesize].pack(PACK_UINT32)
        accumulator << value
      when Symbol then
        v = value.to_s
        accumulator << TYPE_STRING
        accumulator << [v.bytesize].pack(PACK_UINT32)
        accumulator << v
      when Integer then
        accumulator << TYPE_INTEGER
        accumulator << [value].pack(PACK_UINT32)
      when AMQ::Protocol::Float32Bit then
        accumulator << TYPE_32BIT_FLOAT
        accumulator << [value.value].pack(PACK_32BIT_FLOAT)
      when Float then
        accumulator << TYPE_64BIT_FLOAT
        accumulator << [value].pack(PACK_64BIT_FLOAT)
      when true, false then
        accumulator << TYPE_BOOLEAN
        accumulator << (value ? BOOLEAN_TRUE : BOOLEAN_FALSE)
      when Time then
        accumulator << TYPE_TIME
        accumulator << [value.to_i].pack(PACK_INT64).reverse # FIXME: there has to be a more efficient way
      when nil then
        accumulator << TYPE_VOID
      when Array then
        accumulator << TYPE_ARRAY
        accumulator << [self.array_size(value)].pack(PACK_UINT32)

        value.each { |v| accumulator << self.encode(v) }
      when Hash then
        accumulator << TYPE_HASH
        accumulator << AMQ::Protocol::Table.encode(value)
      else
        # We don't want to require these libraries.
        if defined?(BigDecimal) && value.is_a?(BigDecimal)
          accumulator << TYPE_DECIMAL
          if value.exponent < 0
            decimals = -value.exponent
            raw = (value * (decimals ** 10)).to_i
            accumulator << [decimals + 1, raw].pack(PACK_UCHAR_UINT32) # somewhat like floating point
          else
            # per spec, the "decimals" octet is unsigned (!)
            accumulator << [0, value.to_i].pack(PACK_UCHAR_UINT32)
          end
        else
          raise ArgumentError.new("Unsupported value #{value.inspect} of type #{value.class.name}")
        end # if
      end # case

      accumulator
    end

field_value_size(value) click to toggle source

    # File lib/amq/protocol/table_value_encoder.rb, line 85
    def self.field_value_size(value)
      # the type tag takes 1 byte
      acc = 1

      case value
      when String then
        acc += (value.bytesize + 4)
      when Integer then
        acc += 4
      when Float then
        acc += 8
      when Time, DateTime then
        acc += 8
      when true, false then
        acc += 1
      when nil then
        # nothing, type tag alone is enough
      when Hash then
        acc += (4 + Table.hash_size(value))
      when Array then
        acc += (4 + self.array_size(value))
      end

      acc
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.