readUint8ListCopy method

Uint8List readUint8ListCopy(
  1. [int length]
)

Returns the next bytes. Length is determined by the argument. The method always returns a new copy of the bytes.

Implementation

Uint8List readUint8ListCopy([int length]) {
  if (length == null) {
    length = availableLengthInBytes;
  } else if (length > _byteData.lengthInBytes - index) {
    throw new ArgumentError.value(length, "length");
  }
  final result = new Uint8List(length);
  var i = 0;

  // If 128 or more bytes, we read in 4-byte chunks.
  // This should be faster.
  //
  // This constant is just a guess of a good minimum.
  if (length >> 7 != 0) {
    final optimizedDestination = new Uint32List.view(
      result.buffer,
      result.offsetInBytes,
      result.lengthInBytes,
    );
    while (i + 3 < length) {
      // Copy in 4-byte chunks.
      // We must use host endian during reading.
      optimizedDestination[i] = _byteData.getUint32(index + i, Endian.host);
      i += 4;
    }
  }
  for (var i = 0; i < result.length; i++) {
    result[i] = _byteData.getUint8(index + i);
  }
  this.index = index + length;
  return result;
}