Dart - Whats equivalent of C# Stream/MemoryStream and stream.WriteByte(..) for Dart

763

I don't believe that there is a standard equivalent. (There's a GitHub comment explaining why Uint8List requires a fixed length.)

You instead could:

  • Use List<int> and convert to a Uint8List afterward.
  • Create your own class that wraps a Uint8List. When extra capacity is needed, you would need to allocate a new Uint8List that has, say, double the previous size, and to copy the old elements. (This is what most other growable list implementations (e.g. C++'s std::vector) do.) Possibly there is some existing Dart package on pub.dev that already does this.
  • Make a class that wraps a List<Uint8List>, adding new Uint8List members as capacity is need and concatenating them into a single Uint8List when done. There is a third-party buffer package that can do this, but I have never personally used it and can't vouch for it.
Share:
763
Rohit
Author by

Rohit

Android Application Developer Hardcore PC Gamer Playing Android Games

Updated on December 09, 2022

Comments

  • Rohit
    Rohit over 1 year

    Need to achieve following in dart

    MemoryStream stream = new MemoryStream(288);
    stream.WriteByte((byte) 13);
    stream.WriteByte((byte) 12);
    stream.WriteByte((byte) 10);
    stream.WriteByte((byte) 8);
    stream.WriteByte((byte) 6);
    stream.WriteByte((byte) 9);
    var result = stream.ToArray();
    

    I am coming from C#/java background and trying to use Uint8List which is equivalent to byte[] and also more efficient than List<int>. While I can add int8 in Uint8List but Uint8List can only be initialized with fixed-length. I need something dynamic where I can just say write or add without the need of any index to add. No idea how add() of Uint8List works. Couldn't find much on the internet or in docs.

  • Rohit
    Rohit over 4 years
    Is there anything similar to Outputstream or stream to write data as we go?
  • jamesdlin
    jamesdlin over 4 years
    @Rohit I don't know what Outputstream is. If you're looking for something that can do efficient concatenation (like C++'s std::stringstream), I think the closest equivalent from the Dart SDK would be StringBuffer. There is a third-party buffer package that does the same thing for byte data.
  • Rohit
    Rohit over 4 years
    "Buffer package" looks promising, will give it a try. May be the thing I am looking for! Thanks
  • jamesdlin
    jamesdlin over 4 years
    I forgot to note that I've never used and can't vouch for the buffer package; it was just the first result when I searched for "buffer" on pub.dev.
  • Rohit
    Rohit over 4 years
    Never popped that result for me, may be I was searching different terms like "streams".
  • Rohit
    Rohit over 4 years
    Can you edit your answer with buffer package as a solution. buffer package has ByteDataWriter which does exactly what I mentioned in the code example in the question. @jamesdlin
  • jamesdlin
    jamesdlin over 4 years
    @Rohit Okay, done. (The other approaches I mentioned could do exactly what you mentioned in your code example too. ;) )