readInt method

int readInt([
  1. IntType type = IntType.int32,
  2. Endian? endian
])

read int out of bytes defaults to int32 with the host's endian

Implementation

int readInt([IntType type = IntType.int32, Endian? endian]) {
  int shouldRead = [1, 2, 4, 8][type.index];
  int result = 0;
  if (endian == null) {
    endian = Endian.host;
  }
  for (var i = 0; i < shouldRead; i++) {
    if (endian == Endian.big) {
      // higher bits goes first, so move the read bytes 8 bits left
      // and use bit-wise OR operator to put the lower bytes into
      // the read bytes
      // eg.
      // read = 0xff, then shift 8 bits left, would be
      // read = 0xff00, then OR the other part to put together
      // read = 0xff00 | 0x0022 => 0xff22
      result <<= 8;
      result |= data[_currentPosition];
    } else {
      result |= data[_currentPosition] << 8;
    }
    _currentPosition++;
  }
  return result;
}