Class MessagePack::Unpacker
In: ext/unpack.c
Parent: Object

Deserializer class that includes Buffered API and Unbuffered API.

Buffered API uses the internal buffer of the Unpacker. Following code uses Buffered API with an input stream:

  # create an unpacker with input stream.
  pac = MessagePack::Unpacker.new(STDIN)

  # deserialize object one after another.
  pac.each {|obj|
    # ...
  }

Following code doesn‘t use the input stream and feeds buffer manually. This is useful to use special stream or with event-driven I/O library.

  # create an unpacker without input stream.
  pac = MessagePack::Unpacker.new()

  # feed buffer to the internal buffer.
  pac.feed(input_bytes)

  # deserialize object one after another.
  pac.each {|obj|
    # ...
  }

You can manage the buffer manually with the combination of execute, *finished?*, data and reset method.

  # create an unpacker.
  pac = MessagePack::Unpacker.new()

  # manage buffer and offset manually.
  offset = 0
  buffer = ''

  # read some data into the buffer.
  buffer << [1,2,3].to_msgpack
  buffer << [4,5,6].to_msgpack

  while true
    offset = pac.execute(buffer, offset)

    if pac.finished?
      obj = pac.data

      buffer.slice!(0, offset)
      offset = 0
      pac.reset

      # do something with the object
      # ...

      # repeat execution if there are more data.
      next unless buffer.empty?
    end

    break
  end

Methods

data   each   execute   execute_limit   feed   fill   finished?   new   reset   stream   stream=  

Public Class methods

Document-method: MessagePack::Unpacker#initialize

Creates instance of MessagePack::Unpacker.

You can specify a stream for input stream. It is required to implement sysread or readpartial method.

With the input stream, buffers will be feeded into the deserializer automatically.

Without the input stream, use feed method manually. Or you can manage the buffer manually with execute, *finished?*, data and reset methods.

Public Instance methods

Document-method: MessagePack::Unpacker#data

Gets the object deserialized by execute method.

Use this method with execute method.

Document-method: MessagePack::Unpacker#each

Deserializes objects repeatedly. This calls fill method automatically.

UnpackError is throw when parse error is occured. This method raises exceptions that fill method raises.

Document-method: MessagePack::Unpacker#execute

Deserializes one object over the specified buffer from offset bytes.

This method doesn‘t use the internal buffer.

Call *reset()* method before calling this method again.

This returns offset that was parsed to. Use *finished?* method to check an object is deserialized and call data method if it returns true.

UnpackError is throw when parse error is occured.

Document-method: MessagePack::Unpacker#execute_limit

Deserializes one object over the specified buffer from offset bytes upto limit bytes.

This method doesn‘t use the internal buffer.

Call *reset()* method before calling this method again.

UnpackError is throw when parse error is occured.

Document-method: MessagePack::Unpacker#feed

Fills the internal buffer with the specified buffer.

Document-method: MessagePack::Unpacker#fill

Fills the internal buffer using the input stream.

If the input stream is not specified, it returns nil. You can set it on initialize or *stream=* methods.

This methods raises exceptions that _stream.sysread_ or _stream.readpartial_ method raises.

Document-method: MessagePack::Unpacker#finished?

Returns true if an object is ready to get with data method.

Use this method with execute method.

Document-method: MessagePack::Unpacker#reset

Resets the internal state of the unpacker.

Document-method: MessagePack::Unpacker#stream

Gets the input stream.

Document-method: MessagePack::Unpacker#stream=

Resets the input stream. You can set nil not to use input stream.

[Validate]