Class: Boss::Parser
Overview
Parser implements IO-like behavior and provides deserializing of BOSS objects. Parser can store multiple root objects and share same object cache for all them
Instance Method Summary collapse
-
#each ⇒ Object
yields all objects in the stream.
-
#eof? ⇒ Boolean
True is underlying IO-like object reached its end.
-
#get ⇒ Object
Load the object (object tree) from the stream.
-
#initialize(src = nil) ⇒ Parser
constructor
construct parser to read from the given string or IO-like object.
-
#read ⇒ Object
Load the object (object tree) from the stream.
Constructor Details
#initialize(src = nil) ⇒ Parser
construct parser to read from the given string or IO-like object
333 334 335 336 337 338 339 |
# File 'lib/boss-protocol.rb', line 333 def initialize(src=nil) @io = src.class <= String ? StringIO.new(src) : src @io.set_encoding Encoding::BINARY if @io.respond_to? :set_encoding @rbyte = @io.respond_to?(:readbyte) ? -> { @io.readbyte } : -> { @io.read(1).ord } @cache = [nil] @stream_mode = false end |
Instance Method Details
#each ⇒ Object
yields all objects in the stream
433 434 435 |
# File 'lib/boss-protocol.rb', line 433 def each yield get until eof? end |
#eof? ⇒ Boolean
True is underlying IO-like object reached its end.
427 428 429 |
# File 'lib/boss-protocol.rb', line 427 def eof? @io.eof? end |
#get ⇒ Object
Load the object (object tree) from the stream. Note that if there is more than one object in the stream that are stored with the same Formatter instance, they will share same cache and references, see Boss.Formatter.put for details. Note that nil is a valid restored object. Check eof? or catch EOFError, or use Boss.Parser.each to read all objects from the stream
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/boss-protocol.rb', line 359 def get code, value = rhdr case code when TYPE_INT value when TYPE_NINT -value when TYPE_EXTRA case value when TTRUE true when TFALSE false when DZERO, FZERO 0 when DONE, FONE 1.0 when DMINUSONE, FMINUSONE -1.0 when TDOUBLE rdouble when TCOMPRESSED type, size = rhdr data = rbin size case type when 0 Boss.load data when 1 Boss.load Zlib::Inflate.new(Zlib::MAX_WBITS).inflate(data) #when 2 # Boss.load Bzip2.uncompress data else raise UnknownTypeException, "type #{type}" end when TTIME Time.at renc when XT_STREAM_MODE @cache = [nil] @stream_mode = true get else raise UnknownTypeException end when TYPE_TEXT, TYPE_BIN s = rbin value s.force_encoding code == TYPE_BIN ? Encoding::BINARY : Encoding::UTF_8 cache_object s s when TYPE_LIST # p "items", value cache_object (list = []) value.times { list << get } list when TYPE_DICT cache_object (dict = {}) value.times { dict[get] = get } dict when TYPE_CREF x = @cache[value] x.freeze if x.is_a?(String) x else raise UnknownTypeException end end |
#read ⇒ Object
Load the object (object tree) from the stream. Note that if there is more than one object in the stream that are stored with the same Formatter instance, they will share same cache and references, see Boss.Formatter.put for details. Note that nil is a valid restored object. Check eof? or catch EOFError, or use Boss.Parser.each to read all objects from the stream
348 349 350 |
# File 'lib/boss-protocol.rb', line 348 def read get end |