Bug? [SOLVED] in debug mode .readbytes only read part of files

sorex

Expert
Licensed User
Longtime User
Hello,

I was checking some of my code to figure out why a depacker returned only 0's from a certain point.

Now I figured out that the code below only reads in half of the file (499 bytes) when using a debugger built.
In release mode it read the file completely (902 bytes).

B4X:
in=File.OpenInput(File.DirAssets,fn)
Log(in.ReadBytes( buffer,0,1000 ))

It happend in genymotion and on a real tablet.

Is there any reason for this?

Needless to say that this will cause more problems for some people.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug. This is how it works.

SS-2016-02-16_15.36.16.png


If you want to read the complete file into an array then use this code:
B4X:
Dim b() As Byte = Bit.InputStreamToBytes(File.OpenInput(File.DirAssets, fn)) 'the input stream is closed by InputStreamToBytes
 

sorex

Expert
Licensed User
Longtime User
now you're confusing me.

if that is how it works then why does it read the file in completely in release mode but only half in debug?
 

sorex

Expert
Licensed User
Longtime User
bizar, your code does it right in both debug & release mode. Thanks ;)

but I still like to know why it goes wrong with the "normal" method.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot rely on in.ReadBytes to read the whole file at once. In this specific case with your specific device it works for you in release mode. With other files or devices it will not return the whole file.

This is a programming mistake.

You need to use the code I posted or check the number of returned bytes and read again until you read all the bytes required.
 
Top