Class: LEON::Encoder

Inherits:
Object show all
Defined in:
lib/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Encoder

Returns a new instance of Encoder.



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/io.rb', line 237

def initialize(*args)
  if args.length > 1
    @spec = args[1]
    @hasSpec = true
  else
    @hasSpec = false
  end
  @payload = args[0]
  @OLI = Array.new
  @stringIndex = Array.new
  @buffer = StringBuffer.new
end

Instance Method Details

#append(buf) ⇒ Object



249
250
251
# File 'lib/io.rb', line 249

def append(buf)
  @buffer = StringBuffer.concat([@buffer, buf])
end

#exportObject



260
261
262
# File 'lib/io.rb', line 260

def export()
  return @buffer.buffer
end

#writeDataObject



252
253
254
255
256
257
258
259
# File 'lib/io.rb', line 252

def writeData()
  if @hasSpec
    writeValueWithSpec(@payload)
  else
    writeValue(@payload, LEON::type_check(@payload))
  end
  return self
end

#writeOLIObject



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/io.rb', line 422

def writeOLI()
  if @stringIndex.length === 0
    return self
  end
  @OLI = LEON::gather_layouts(@payload, @stringIndex)
  if @OLI.length === 0
    writeValue(0xFF, Constants::UNSIGNED_CHAR, true)
    return self
  end
  @OLItype = LEON::type_check(@OLI.length)
  writeValue(@OLI.length, @OLItype)
  @OLI.each { |v|
    type = LEON::type_check(v.length)
    writeValue(v.length, type)
    v.each { |v|
      writeValue(v, @stringIndexType, true)
    }
  }
  return self
end

#writeSIObject



442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/io.rb', line 442

def writeSI()
  @stringIndex = LEON::gather_strings(@payload)
  if @stringIndex.length === 0
    writeValue(0xFF, Constants::UNSIGNED_CHAR, true)
    return self
  end
  @stringIndexType = LEON::type_check(@stringIndex.length)
  writeValue(@stringIndex.length, @stringIndexType)
  @stringIndex.each { |v|
    writeString(v)
  }
  return self
end

#writeString(str) ⇒ Object



414
415
416
417
418
419
420
421
# File 'lib/io.rb', line 414

def writeString(str)
  len = str.length
  writeValue(len, LEON::type_check(len))
  for i in 0...len
    writeValue(str[i].ord, Constants::UNSIGNED_CHAR, true)
  end
  return len + 2
end

#writeValue(*args) ⇒ Object



291
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
# File 'lib/io.rb', line 291

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_i, Constants::UNSIGNED_INT, true)
    return byteCount + 4
  end
end

#writeValueWithSpec(*args) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/io.rb', line 263

def writeValueWithSpec(*args)
  if args.length > 1
    spec = args[1]
  else
    spec = @spec
  end
  val = args[0]
  if spec.kind_of? Array
    writeValue(val.length, LEON::type_check(val.length))
    val.each { |v|
      writeValueWithSpec(v, spec[0])
    }
  elsif spec.kind_of? Hash
    if val.kind_of? Hash
      spec.sort.each { |k, v|
        writeValueWithSpec(val[k], v)
      }
    else
      spec.sort.each { |k, v|
        writeValueWithSpec(val.send(k), v)
      }
    end
  elsif spec === Constants::BOOLEAN
    writeValue(val, LEON::type_check(val), true)
  else
    writeValue(val, spec, true)
  end
end