Skip to content
Ingvar Stepanyan edited this page Apr 8, 2014 · 6 revisions

Reading/Writing

  • read(type, offset = binary.tell()): Read value of specified type. If offset given, read it from custom position, otherwise read it from current position and move pointer forward (streaming mode).
var firstInt = binary.read('uint32'); // uint32 value at offset 0
var secondInt = binary.read('uint32'); // following uint32 value at offset 4
var byteAtOffset100 = binary.read('uint8', 100); // uint8 value at custom position
var thirdInt = binary.read('uint32'); // third uint32 value at offset 8 (since read operation with custom position didn't move internal pointer)
  • readAll(): Read entire data as value of type specified by 'jBinary.all' key in typeset.

  • write(type, data, offset = binary.tell()): Write value of specified type. Same offset behavior.

binary.write('uint32', 1); // writing uint32 value of 1 at offset 0
binary.write('uint32', 2); // writing uint32 value of 2 at offset 4
binary.write('uint8', 0xff, 100); // writing uint8 value of 255 at offset 100
binary.write('uint32', 3); // writing uint32 value of 3 at offset 8
  • writeAll(data): Write entire data as value of type specified by 'jBinary.all' key in typeset.

Position methods

  • tell(): Return the current position.
var currentPos = binary.tell();
  • seek(position[, callback]): Go to position; if callback is given, execute it and return to the previous position.
binary.seek(100); // just went to offset 100
var intAt200 = binary.seek(200, function () { return this.binary.read('int32') }); // go to offset 200, get int32 value there and return back to offset 100
  • skip(count[, callback]): Advance in the binary by count bytes; same callback behavior.
binary.skip(1); // just skip one byte we are not interested in
var intAfter8 = binary.skip(8, function () { return this.binary.read('int32') }); // go 8 bytes forward, get int32 value there and return back

Instance helpers

  • slice(start, end, forceCopy = false): Returns sliced version of current binary with same type set. If forceCopy set to true, underlying jDataView will be created on copy of original data not linked to it.
var pointerSlice = binary.slice(10, 20); // now you can manipulate on this jBinary instance and all the changes will be mirrored to original binary's data, but with new bound check rules
var copySlice = binary.slice(10, 20, true); // now you can manipulate on this jBinary instance as on absolutely new one and any data changes will stay isolated from original binary
  • as(typeSet, modifyOriginal = false): Casts jBinary instance to given typeSet while pointing to the same data, pointers and methods as original instance; if modifyOriginal set to true, original instance will be modified instead of being inherited.
var binary = someExternalBinary.as(TAR); // casting external jBinary instance with no or with irrelevant typeset to TAR typeSet object (any operations including both data and pointer manipulations will affect both instances).
Clone this wiki locally