decodeLength static method

int decodeLength(
  1. Uint8List encodedBytes
)

Calculates the length of the value bytes for the given encodedBytes.

Will return -1 if the length byte equals 0x80. Throws an ArgumentError if the length could not be calculated for the given encodedBytes.

Implementation

static int decodeLength(Uint8List encodedBytes) {
  var valueStartPosition = 2;
  var length = encodedBytes[1];
  if (length <= 0x7F) {
    return length;
  }
  if (length == 0x80) {
    return -1;
  }
  if (length > 127) {
    var length = encodedBytes[1] & 0x7F;

    var numLengthBytes = length;

    length = 0;
    for (var i = 0; i < numLengthBytes; i++) {
      length <<= 8;
      length |= encodedBytes[valueStartPosition++] & 0xFF;
    }
    return length;
  }
  throw ArgumentError('Could not calculate the length from the given bytes.');
}