Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine a set of Uint8List

Tags:

dart

I'm building a bunch of Uint8List (of different sizes, for now they are stored in a generic List) and I need to combine/concatenate them before sending on a websocket.

What would be the best approach ?

I though of combining them in a new Uint8List, but since I don't need byte access anymore after it is combined, I can maybe use a different List<int> implementation ... ?

Thanks in advance.

like image 920
datto Avatar asked Jan 16 '19 16:01

datto


2 Answers

Uint8List implements List<int>. You can combine them to a new List<int> and then create a new Uint8List with

List<List<int>> myByteLists = ...;
var bytes = Uint8List.fromList(myByteList.expand((x) => x).toList());
like image 86
Günter Zöchbauer Avatar answered Oct 17 '22 14:10

Günter Zöchbauer


Using BytesBuilder seems to be the most efficient way to concatenate Uint8List's in Dart:

  var b = BytesBuilder();
  var l1 = Uint8List(4);
  var l2 = Uint8List(4);
  b.add(l1);
  b.add(l2);
  var ll = b.toBytes();
like image 34
Maxim Saplin Avatar answered Oct 17 '22 15:10

Maxim Saplin