I have code like this:
ByteArray ret;
ret.resize( MAX( body_left, tmp_read.size() ) );
while ( body_left > 0 ) {
ByteArray::Write r = tmp_read.write();
int rec = 0;
err = connection->get_partial_data( r.ptr(), MIN( body_left, tmp_read.size() ), rec );
if ( rec > 0 ) {
ByteArray::Write w = ret.write();
copymem( w.ptr(), r.ptr(), rec );
body_left -= rec;
}
}
I find it challenging to understand this code. A few questions:
Is ret.resize(MAX(body_left,tmp_read.size()));
allocating the ByteArray
of highest body_left
or tmp_read.size()
?
In ByteArray::Write r = tmp_read.write();
does r
become a pointer to location in space that is going to be used to write data?
In ByteArray::Write w = ret.write();
, does w
become a pointer like r
in the previous question?
Also, in this line:
copymem(w.ptr(),r.ptr(),rec);
As I understand this line, all of the data that is gathered under pointer r
is copied to location under the pointer w
. The problem is that they are different size, how to move pointer w.ptr()
to keep data intact and in correct order? Or is w.ptr()
is a pointer to function and this should not be a problem.
Extra context:
Method get_partial_data
returns chunks of data - lets say 20, 20, and 10 bytes each.
Variable ret
is supposed to be 50 bytes long and have those chunks merged into one ByteArray
.
Unfortunately I cannot find the definition of ByteArray
in this project, so I guess it's part of another library (libGL maybe?).
I know this question is not very precise and I am making a leap of faith, but if anyone can assist me I would be grateful.
Original class and project this code was taken from:
https://github.com/okamstudio/godot/blob/master/core/io/http_client.cpp
Lines 503-516.
It's in different shape, as I already have applied dirty hack (that does not work to well).