Method: AMQ::Protocol::Method.encode_body

Defined in:
lib/amq/protocol/client.rb

.encode_body(body, channel, frame_size) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/amq/protocol/client.rb', line 164

def self.encode_body(body, channel, frame_size)
  return [] if body.empty?

  # 8 = 1 + 2 + 4 + 1
  # 1 byte of frame type
  # 2 bytes of channel number
  # 4 bytes of frame payload length
  # 1 byte of payload trailer FRAME_END byte
  limit        = frame_size - 8
  return [BodyFrame.new(body, channel)] if body.bytesize < limit

  # Otherwise String#slice on 1.9 will operate with code points,
  # and we need bytes. MK.
  body.force_encoding("ASCII-8BIT") if RUBY_VERSION.to_f >= 1.9

  array = Array.new
  while body && !body.empty?
    payload, body = body[0, limit], body[limit, body.length - limit]
    array << BodyFrame.new(payload, channel)
  end

  array
end