typed_data icon indicating copy to clipboard operation
typed_data copied to clipboard

ByteBuffer.asUint8List() has incorrect length

Open bliep opened this issue 1 year ago • 0 comments

The following code demonstrates that the length of the Uint8List returned by ByteBuffer.asUint8List() has an unexpected value. Its value is perhaps related to the allocation size of the ByteBuffer, but does not equal the amount of bytes in the ByteBuffer.

import 'dart:typed_data';
import 'package:typed_data/typed_buffers.dart';

void main() {
  Uint8List ul = Uint8List(1);
  var b = Uint8Buffer();
  for (int i = 0; i < 10; i++) {
    b.addAll(ul);
    print("${b.lengthInBytes}, ${b.buffer.asUint8List().length}");
  }
}

will produce:

1, 1
2, 8
3, 8
4, 8
5, 8
6, 8
7, 8
8, 8
9, 16
10, 16

Dart SDK version: 3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64".

pubspec.lock:

# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  collection:
    dependency: transitive
    description:
      name: collection
      sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
      url: "https://pub.dev"
    source: hosted
    version: "1.18.0"
  typed_data:
    dependency: "direct main"
    description:
      name: typed_data
      sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
      url: "https://pub.dev"
    source: hosted
    version: "1.3.2"
sdks:
  dart: ">=3.0.0 <4.0.0"
  flutter: ">=3.3.0"

bliep avatar Mar 05 '24 17:03 bliep