Discussion:
[dart-misc] Byte array representation in Dart 2
Denis Portnov
2018-06-11 22:53:26 UTC
Permalink
Hi all,

What is idiomatic way in Dart 2 for representing byte array? What type to
use?

I see that io, convert, crypto etc. all require or produce byte arrays as
List<int>
It's not very convenient for library author as it requires either to
check/truncate individual ints from List, or use Uint8List.fromList()
everywhere.

Flutter explicitly goes with Uint8List
https://docs.flutter.io/flutter/widgets/Image/Image.memory.html

So let's say I'm doing library for IP address, UUID, hash, or any structure
with underlying byte array.

Is it fine to have this


class IPAddress {
IPAddress.fromBytes(Uint8List bytes) {}

Uint8List get bytes;
}

class SomeBinaryEncoder extends Converter<Uint8List, Uint8List> {

Uint8List convert(Uint8List input) {}

}


Thanks
Denis
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/478c312b-8066-4e22-9bc3-9ed524e5016b%40dartlang.org.
'Florian Loitsch' via Dart Misc
2018-06-12 09:22:29 UTC
Permalink
Yes.
It's perfectly fine to stick to Uint8Lists if that's what you use
internally, and if you expect most users of your API to be comfortable with
it.
dart:io, ... would probably now switch to explicit `Uint8List` as well.

Alternatively, you can take `List<int>` and create a function
`ensureUint8List` that does an `is` check and only converts the list if it
isn't already one.
Post by Denis Portnov
Hi all,
What is idiomatic way in Dart 2 for representing byte array? What type to
use?
I see that io, convert, crypto etc. all require or produce byte arrays as
List<int>
It's not very convenient for library author as it requires either to
check/truncate individual ints from List, or use Uint8List.fromList()
everywhere.
Flutter explicitly goes with Uint8List
https://docs.flutter.io/flutter/widgets/Image/Image.memory.html
So let's say I'm doing library for IP address, UUID, hash, or any
structure with underlying byte array.
Is it fine to have this
class IPAddress {
IPAddress.fromBytes(Uint8List bytes) {}
Uint8List get bytes;
}
class SomeBinaryEncoder extends Converter<Uint8List, Uint8List> {
Uint8List convert(Uint8List input) {}
}
Thanks
Denis
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/478c312b-8066-4e22-9bc3-9ed524e5016b%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/478c312b-8066-4e22-9bc3-9ed524e5016b%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAGhYCvAdyqb_WkhT5JCpw2ADT5XW7yNACCBbOVX8z9yDCr-Rhw%40mail.gmail.com.
Istvan Soos
2018-06-12 10:16:06 UTC
Permalink
Both should work in most cases just fine, however I have a slight
preference on the least restrictive inputs. E.g. when a method expects
bytes as input, I'd prefer to have it as List<int>, but when it
returns these values, it is could be nicer if it returns them as
Uint8List, if/when the implementation will use that anyway.

As Florian suggested, you can easily convert it to Uint8List
internally if that is important for the implementation logic.

Also, for some byte-concatenation tasks, CombinedListView from
package:collection can be useful.

Cheers,
Istvan
Post by Denis Portnov
Hi all,
What is idiomatic way in Dart 2 for representing byte array? What type to
use?
I see that io, convert, crypto etc. all require or produce byte arrays as
List<int>
It's not very convenient for library author as it requires either to
check/truncate individual ints from List, or use Uint8List.fromList()
everywhere.
Flutter explicitly goes with Uint8List
https://docs.flutter.io/flutter/widgets/Image/Image.memory.html
So let's say I'm doing library for IP address, UUID, hash, or any structure
with underlying byte array.
Is it fine to have this
class IPAddress {
IPAddress.fromBytes(Uint8List bytes) {}
Uint8List get bytes;
}
class SomeBinaryEncoder extends Converter<Uint8List, Uint8List> {
Uint8List convert(Uint8List input) {}
}
Thanks
Denis
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/478c312b-8066-4e22-9bc3-9ed524e5016b%40dartlang.org.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CALdQGgv0rqjxz56fQA5EEoGDVn-6kk883b6gbDO_WsqB6AkDfQ%40mail.gmail.com.
Denis Portnov
2018-06-13 02:04:49 UTC
Permalink
Florian, Istvan, Than your for your comments.
Btw, is it correct that the right assumption that on VM/dart-js level
typed_data collections are more optimal in terms of processing compared to
List<int>?
Thanks
Denis
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/f0461b25-1fa8-4580-8ca0-77bc3a116e5c%40dartlang.org.
Loading...