class FFI::MsgPack::Unpacker

Constants

CHUNK_SIZE

Default chunk-size to expand the buffer by

DEFAULT_SIZE

The default size of the unpacker buffer

Attributes

chunk_size[RW]

The chunk-size to expand the buffer by

stream[RW]

The optional stream to read packed data from

Public Class Methods

create(size=DEFAULT_SIZE) click to toggle source

Creates a new unpacker object.

@param [Integer] size

The buffer size of the unpacker.

@return [Unpacker]

The new unpacker.
# File lib/ffi/msgpack/unpacker.rb, line 51
def Unpacker.create(size=DEFAULT_SIZE)
  Unpacker.new(MsgPack.msgpack_unpacker_new(size))
end
new(*arguments) click to toggle source

Initializes a new unpacker object.

Calls superclass method
# File lib/ffi/msgpack/unpacker.rb, line 35
def initialize(*arguments)
  super(*arguments)

  @chunk_size = CHUNK_SIZE
  @stream = nil
end
release(ptr) click to toggle source

Destroys a previously allocated unpacker object.

@param [FFI::Pointer] ptr

The pointer to the allocated unpacker.
# File lib/ffi/msgpack/unpacker.rb, line 61
def Unpacker.release(ptr)
  MsgPack.msgpack_unpacker_free(ptr)
end

Public Instance Methods

<<(packed) click to toggle source

Writes packed data into the buffer of the unpacker.

@param [String] packed

The packed data.

@return [Unpacker]

The unpacker.
# File lib/ffi/msgpack/unpacker.rb, line 74
def <<(packed)
  # make sure we have space in the buffer
  reserve_buffer(packed.length)

  # copy in the bytes
  self[:buffer].put_bytes(buffer_offset,packed)

  # advace the buffer position
  buffer_consumed!(packed.length)
  return self
end
each() { |to_ruby| ... } click to toggle source

Enumerates over each Msg Object from the buffer in the unpacker.

@yield [obj]

The given block will be passed each unpacked Ruby Object, from
the buffer of the unpacker.

@yieldparam [nil, true, false, Integer, Float, String, Array, Hash] obj

A Ruby Object unpacked from the buffer of the unpacker.

@return [Enumerator, Unpacker]

If no block is given, an enumerator will be returned.
# File lib/ffi/msgpack/unpacker.rb, line 164
def each
  return enum_for(:each) unless block_given?

  each_object do |obj|
    yield obj.to_ruby
  end
end
each_object() { |obj| ... } click to toggle source

Enumerates over each Msg Object from the buffer in the unpacker.

If {#stream} is set, packed data will be read from it, when the buffer of the unpacker is fully unpacked.

@yield [obj]

The given block will be passed each Msg Object.

@yieldparam [MsgObject] obj

An unpacked Msg Object.

@return [Unpacker]

The unpacker.
# File lib/ffi/msgpack/unpacker.rb, line 123
def each_object
  loop do
    ret = MsgPack.msgpack_unpacker_execute(self)

    if ret > 0
      # copy out the next Msg Object and release it's zone
      obj = MsgPack.msgpack_unpacker_data(self)
      zone = MsgPack.msgpack_unpacker_release_zone(self)

      # reset the unpacker
      MsgPack.msgpack_unpacker_reset(self)

      yield obj

      # free the zone now that we are done with it
      MsgPack.msgpack_zone_free(zone)
    elsif ret < 0
      raise(ParseError,"a parse error occurred",caller)
    else
      unless (@stream && read(@stream))
        break
      end
    end
  end

  return self
end
read(io) click to toggle source

Reads packed data from a stream into the buffer of the unpacker.

@param [IO] io

The stream to read the packed data from.

@return [Boolean]

Specifies whether data was read from the stream, or if the stream
is empty.
# File lib/ffi/msgpack/unpacker.rb, line 96
def read(io)
  reserve_buffer(@chunk_size)
  result = io.read(buffer_capacity)

  unless (result.nil? || result.empty?)
    self << result
    return true
  else
    return false
  end
end

Protected Instance Methods

buffer_capacity() click to toggle source

The remaining space of the buffer.

@return [Integer]

The number of bytes free in the buffer.
# File lib/ffi/msgpack/unpacker.rb, line 207
def buffer_capacity
  self[:free]
end
buffer_consumed!(size) click to toggle source

Consumes space in the buffer.

@param [Integer] size

The number of bytes to be consumed.

@return [nil]

# File lib/ffi/msgpack/unpacker.rb, line 219
def buffer_consumed!(size)
  self[:used] += size
  self[:free] -= size

  return nil
end
buffer_offset() click to toggle source

The offset to empty space in the buffer.

@return [Integer]

The number of bytes within the buffer.
# File lib/ffi/msgpack/unpacker.rb, line 197
def buffer_offset
  self[:used]
end
message_size() click to toggle source

The size of the unparsed message in the buffer.

@return [Integer]

The number of bytes that are unparsed.
# File lib/ffi/msgpack/unpacker.rb, line 232
def message_size
  self[:parsed] - self[:off] + self[:used]
end
parsed_size() click to toggle source

The number of bytes that have been parsed in the buffer.

@return [Integer]

The number of parsed bytes.
# File lib/ffi/msgpack/unpacker.rb, line 242
def parsed_size
  self[:parsed]
end
reserve_buffer(size) click to toggle source

Reserves space in the buffer.

@param [Integer] size

The number of bytes to reserve.

@return [Boolean]

Specifies whether the size has been successfully reserved.
# File lib/ffi/msgpack/unpacker.rb, line 183
def reserve_buffer(size)
  if self[:free] >= size
    true
  else
    MsgPack.msgpack_unpacker_expand_buffer(self,size)
  end
end