Dart fill ByteData with List<int> - dart

List<int> l = [ 1, 2, 3, 4 ];
var b = ByteData(10);
May I know what is the easiest way to fill b (position 4 ~ 7) with data from l.
I can certainly iterate through l and then fill b one by one. But this is just part of a larger solution. So I hope there is a simpler way (for easy maintenance in future).

ByteData represent an area of memory counted in bytes but does not tell us anything how we want to represent the data inside this block of memory.
Normally, we would use one of the specific data types from dart:typed_data like e.g. Uint8List, Int8List, Uint16List and so on which have a lot more functionality.
But you can easily get the same by making a view over your ByteData. In this example I guess you want to insert your numbers as Uint8:
import 'dart:typed_data';
void main() {
List<int> l = [ 1, 2, 3, 4 ];
var b = ByteData(10);
var uInt8ListViewOverB = b.buffer.asUint8List();
uInt8ListViewOverB.setAll(4, l);
print(uInt8ListViewOverB); // [0, 0, 0, 0, 1, 2, 3, 4, 0, 0]
}
I recommend reading the documentation for the different methods on ByteBuffer (returned by buffer). You can e.g. make subview of a limited part of your ByteData if your ByteData needs to contain different types of data:
https://api.dart.dev/stable/2.15.0/dart-typed_data/ByteBuffer/asUint8List.html

Related

Applying a windowing function to a Dart stream

I am pretty new to Dart, and still wrapping my head around streams. Specifically I am having some difficulty with finding the proper way of making a function that takes a window of N elements from a stream, applies a function to it and restreams the results.
To clarify what I mean, I include an example that I implemented myself which led me to this question. The code takes a byte stream from a file and converts 4 byte chunks to an integer stream. By using an await for I was able to accomplish what I wanted but I am looking for a more idiomatic stream based function that accomplishes the same thing, more succinctly.
Stream<int> loadData(String path) async* {
final f = File(path);
final byteStream = f.openRead();
var buffer = Uint8List(8);
var i = 0;
// This is where I would like to use a windowing function
await for(var bs in byteStream) {
for(var b in bs) {
buffer[i++] = b;
if(i == 8) {
var bytes = new ByteData.view(buffer.buffer);
yield bytes.getUint16(0);
i = 0;
}
}
}
}
Look at bufferCount method from RxDart package.
Buffers a number of values from the source Stream by count then emits the buffer and clears it, and starts a new buffer ...
Here is an example:
import 'dart:typed_data';
import 'package:rxdart/rxdart.dart';
main() {
var bytes = Uint8List.fromList([255, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 2, 1, 0, 0]);
Stream<int>.fromIterable(bytes)
.bufferCount(4)
.map((bytes) => Uint8List.fromList(bytes).buffer)
.map((buffer) => ByteData.view(buffer).getInt32(0, Endian.little))
.listen(print); // prints 255 256 257 258
}
It is worth noting that this particular task can be performed much easier:
bytes.buffer.asInt32List();

Equivalent for Java System.arraycopy in Dart?

How do I convert the below java code to equivalent dart.
private static final byte[] mIdBytes = new byte[]{(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x7E};
byte[] data;
System.arraycopy(mIdBytes, 2, data, 0, 4);
Is there any Dart method that does a similar kind of operation?
I was looking into this:
https://pub.dev/documentation/ckb_dart_sdk/latest/ckb-utils_number/arrayCopy.html
To match Java's System.arrayCopy(source, sourceOffset, target, targetOffset, length)
you should use
target.setRange(targetOffset, targetOffset + length, source, sourceOffset);
This is more efficient than using List.copyRange for some lists, for example copying between typed-data lists with the same element size (like two Uint8Lists).
Well, I found the way to do it.
you can just use
List.copyRange(data, 0, mIdBytes, 2);
This is a workaround I kinda found to be done in your case. This is called sublist(), this method will take the start index, and an end index.
IDEA:
Use sublist(), and copy the elements to be started from, that sourcePos = you_pos
Source array will be used like sourceArray.sublist(startIndext, endIndex)
The destination array will be initialized with the value using sublist()
Till what length the item should be added would be mentioned in the end index+2, since it will ignore the last item, and copy till the index-1
FINAL CODE
void main() {
List<int> source = [1, 2, 3, 4, 5, 6];
List<int> target = [];
int startPos = 1;
int length = 4;
// to ensure the length doesn't exceeds limit
// length+2 because, it targets on the end index, that is 4 in source list
// but the end result should be length+2 to contain a length of 5 items
if(length+1 <= source.length-1){
target = source.sublist(startPos, length+2);
print(target);
}else{
print('Cannot copy items till $length: index out of bound');
}
}
//OUTPUT
[2, 3, 4, 5, 6]

How to convert number words to int (like three to 3) using Dart

I want to convert number words (like one, two, three and so on) to int (like 1, 2, 3 etc) using Dart
You have to depend on machine learning library or pair every string with the respective number.
int convertStrToNum(String str) {
var number = <String, num>{'one': 1, ...};
return number[str];
}
Of course, machine learning might be the fastest and best way to do this, but I can't really help you there. So, here's an implementation that would work assuming the "number word" follows a certain format, up until 10. (You could implement a RegExp parser to make this easier, but that would get tricky).
int convStrToNum(String str) {
var oneten = <String, num> {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
}
if (oneten.keys.contains(str)) {
return oneten[str];
}
}
int convStrToInt(String str) {
var list = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
];
return list.indexOf(str);
}
I just pushed a repo addressing this issue. It's open to open to build upon so feel free to contribute, it would really help improve the package. Heres the link https://github.com/michaelessiet/wordstonumbers.dart

Does cloning an iterator copy the entire underlying vector?

I would like to iterate over a vector several times:
let my_vector = vec![1, 2, 3, 4, 5];
let mut out_vector = vec![];
for i in my_vector {
for j in my_vector {
out_vector.push(i * j + i + j);
}
}
The j-loop has a "value used here after move" error. I know that I can place an & before the two my_vectors and borrow the vectors, but it is nice to have more than one way to do things. I would like a little insight as well.
Alternatively, I can write the following:
let i_vec = vec![1, 2, 3, 4, 5, 6];
let iterator = i_vec.iter();
let mut out_vec = vec![];
for i in iterator.clone() {
for j in iterator.clone() {
out_vec.push(i * j + i + j);
}
}
I looked at What's the most efficient way to reuse an iterator in Rust?:
Iterators in general are Clone-able if all their "pieces" are Clone-able.
Is the actual heap allocated data a "piece" of the iterator or is it the memory address that points to the heap data the aforementioned piece?
Cloning a slice iterator (this is the type of iterator you get when calling iter() on a Vec or an array) does not copy the underlying data. Both iterators still point to data stored in the original vector, so the clone operation is cheap.
The same is likely true for clonable iterators on other types.
In your case, instead of calling i_vec.iter() and then cloning it, you can also call i_vec.iter() multiple times:
for i in i_vec.iter() {
for j in i_vec.iter() {
which gives the same result but is probably more readable.

Why might '_Uint8ArrayView' be returned to a Dart command line client's websocket onData handler?

I have a command line Dart script client interacting over a [dart:io] websocket with a Jetty server. I've implemented a custom message subprotocol that uses reflection (both sides) to exchange Dart objects with Java objects in Jetty using binary encoding. PODO -> Uint8List -> wire -> ByteBufer -> POJO (and in reverse for the return trip). The local round trip unit tests execute correctly on either side (i.e. PODO -> Uint8List -> PODO; POJO -> ByteBuffer -> POJO). I've tested the connection with a different service endpoint using a series of simple 'string' exchanges. The transmission from Dart to Jetty works but the response data, although correctly received, produces an odd type that I don't understand and which the decoder doesn't understand as either a Uint8List, ByteBuffer, etc.
Although I can't easily distill this into a small example, here is the relevant code and some output:
Dart Client:
WebSocket.connect(url).then((WebSocket socket) {
_log.finer('connected');
_websocket = socket
..listen(_onResponse, onError: (e, StackTrace st) => print('Session error: $e; $st'));
...
}
_onResponse(data) {
print('response raw data: $data');
InstanceMirror im = reflect(data);
print('instance: $im');
...
decode(data)
}
decode(Uint8List data) {
var b = data.buffer;
ByteData bd = new ByteData.view(b);
int offset = 0;
const ENDIANNESS = Endianness.LITTLE_ENDIAN;
int msgLength = bd.getInt32(offset, ENDIANNESS); // is 6645122; should be 101
...
}
Output:
response raw data: [101, 0, 0, 0, ...]
instance: InstanceMirror on Instance of '_Uint8ArrayView'
The IntelliJ debugger shows:
data = {List[id=1]} size = 101
> im = {_LocalInstanceMirror[id=2]} InstanceMirror on Instance of '_Uint8ArrayView'
_reflectee = {List[id=1]}
_type = null
hasReflectee = true
Dart supports the general ByteBuffer type which just represents a list of bytes as you can see here:
https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:typed_data.ByteBuffer
As the ByteBuffer class is abstract, you create lists from either receiving a ByteBuffer object or by instantiating a new list. The list usually is fixed length and working with it is hard. That's why you can create views from a ByteBuffer. A view represents a subset of the ByteBuffers bytes, given by an offset and a length which could also be the whole buffer.
Let's look at a small example:
import 'dart:typed_data';
void main() {
Uint8List data8 = new Uint8List.fromList([1,2,3,4,5,6,256]);
Uint16List data16 = new Uint16List.fromList([1,2,3,4,5,6,256]);
print(data8);
print(data16);
print(data8.buffer.lengthInBytes);
print(data16.buffer.lengthInBytes);
print(data16.buffer.asUint8List());
print(data16.buffer.asUint16List());
}
which gives you:
[1, 2, 3, 4, 5, 6, 0] // List
[1, 2, 3, 4, 5, 6, 256] // List
7
14
[1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 0, 1] // View
[1, 2, 3, 4, 5, 6, 256] // View
This means: ByteBuffer is some abstract class which gives you the interface and basic functionality and you can get views from a buffer to access the data as you need it.
The buffer accessed by 'data.buffer' is an internal buffer backing the Uint8List view referenced by 'data'. The transmitted payload -- what I want to wrap with the ByteData view -- is offset by some number of bytes into this buffer. The 'offset' of the payload can be determined through the 'offsetInBytes' property of the 'data' object. The following changes to decode() make it possible to use the ByteData API to decode various fragments of the payload byte array:
int offset = data.offsetInBytes; // sets offset to the starting position of the payload
int msgLength = bd.getInt32(offset);
double somethingElse = bd.getFloat64(offset+4);
// etc.

Resources