Method: LEON::Encoder#writeValue

Defined in:
lib/io.rb

#writeValue(*args) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
# File 'lib/io.rb', line 292

def writeValue(*args)
  val = args[0]
  type = args[1]
  if args.length > 2
    implicit = args[2]
  else
    implicit = false
  end
  typeByte = StringBuffer.new
  if type === Constants::NATIVE_OBJECT
    typeByte.writeUInt8(Constants::OBJECT, 0)
  else
    typeByte.writeUInt8(type, 0)
  end
  if type === Constants::UNDEFINED or type === Constants::BOOLEAN or type === Constants::BOOLEAN + 1 or type === Constants::NULL or type === Constants::NAN or type === Constants::MINUS_INFINITY or type === Constants::INFINITY
    append(typeByte)
    return 1
  end
  byteCount = 0
  if !implicit
    append(typeByte)
    byteCount += 1
  end
  if type === Constants::STRING
    if val.kind_of? Symbol
      val = val.to_s
    end
    if @stringIndex.length === 0
      writeString(val)
      return 1 + byteCount + val.length
    end
    writeValue(@stringIndex.index(val), @stringIndexType, true)
    return byteCount + 1
  elsif type == Constants::CHAR
    bytes = StringBuffer.new
    bytes.writeInt8(val, 0)
    append(bytes)
    return byteCount + 1
  elsif type == Constants::UNSIGNED_CHAR
    bytes = StringBuffer.new
    bytes.writeUInt8(val, 0)
    append(bytes)
    return byteCount + 1
  elsif type === Constants::SHORT
    bytes = StringBuffer.new
    bytes.writeInt16LE(val, 0)
    append(bytes)
    return byteCount + 2
  elsif type === Constants::UNSIGNED_SHORT
    bytes = StringBuffer.new
    bytes.writeUInt16LE(val, 0)
    append(bytes)
    return byteCount + 2
  elsif type === Constants::INT
    bytes = StringBuffer.new
    bytes.writeInt32LE(val, 0)
    append(bytes)
    return byteCount + 4
  elsif type === Constants::UNSIGNED_INT
    bytes = StringBuffer.new
    bytes.writeUInt32LE(val, 0)
    append(bytes)
    return byteCount + 4
  elsif type === Constants::FLOAT
    bytes = StringBuffer.new
    bytes.writeFloatLE(val, 0)
    append(bytes)
    return byteCount + 4
  elsif type === Constants::DOUBLE
    bytes = StringBuffer.new
    bytes.writeDoubleLE(val, 0)
    append(bytes)
    return byteCount + 8
  elsif type === Constants::ARRAY
    writeValue(val.length, LEON::type_check(val.length))
    val.each { |v|
      writeValue(v, LEON::type_check(v))
    }
    return
  elsif type === Constants::OBJECT
    index = LEON::match_layout(val, @stringIndex, @OLI)
    if !implicit
      writeValue(index, @OLItype, true)
    end
    for i in 0..(@OLI[index].length - 1)
      key = @stringIndex[@OLI[index][i]]
      if not val.has_key? key
        key = key.to_sym
      end
      tmp = val[key]
      writeValue(tmp, LEON::type_check(tmp))
    end
    return
  elsif type === Constants::NATIVE_OBJECT
    index = LEON::match_layout(val, @stringIndex, @OLI)
    if !implicit
      writeValue(index, @OLItype, true)
    end
    for i in 0..(@OLI[index].length - 1)
      key = @stringIndex[@OLI[index][i]]
      tmp = val.send key
      writeValue(tmp, LEON::type_check(tmp))
    end
    return
  elsif type === Constants::BUFFER
    len = val.buffer.length
    writeValue(len, LEON::type_check(len))
    for i in 0..(len - 1)
      writeValue(val.buffer[i].ord, Constants::UNSIGNED_CHAR, true)
    end
    return byteCount + val.buffer.length
  elsif type === Constants::REGEXP
    writeString(val.pattern)
    writeString(val.modifier)
    return byteCount + 2 + val.pattern.length + val.modifier.length
  elsif type === Constants::DATE
    if val.kind_of? Date 
      val = Time.parse(val.to_s)
    end
    writeValue(val.to_f * 1000, Constants::DOUBLE, true)
    return byteCount + 8
  end
end