B4J Question SLByteArrayBuffer empty

uniplan

Active Member
Licensed User
Longtime User
Dear,

Dim chunk = SLByteArrayBuffer
chunk.Initialize(32)

....chunkBytesOut.Buffer.Length results to be 32 elements.
I expected it would be empty, since no AppendXXX method has been called on chunk.

Any comment ?

Thanks.
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
You are initializing it with a length of 32. And now you are wondering it is 32?

No,

the parameter name is "capacity" (not "length").

Indeed,

chunk.Initialize(N)
chunk.AppendByte( bufferIn, 0, bufferIn.Length ) 'here bufferIn is an array of bytes

would result in chunk.Buffer.Length = bufferIn.Length
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Works as expected here:
B4X:
Dim SLB As SLByteArrayBuffer
    SLB.Initialize(32)
    Log(SLB.length)

Logs 0

Your code in the first post is syntactically incorrect, is that just a typing mistake?
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Works as expected here:
B4X:
Dim SLB As SLByteArrayBuffer
    SLB.Initialize(32)
    Log(SLB.length)

Logs 0

Your code in the first post is syntactically incorrect, is that just a typing mistake?

Yes, it was a typo, sorry.
Correct version :

Dim chunk As SLByteArrayBuffer
chunk.Initialize(32)

Note that my question is about chunk.Buffer, not chunk.Length

I wonder chunk.Buffer.Length = 32
whereas an additional chunk.AppendByte( someBuffer, 0, someBuffer.Length )
results in chunk.Buffer.Length = someBuffer.Length

I found out the problem when using chunk.Buffer . It resulted
to contain all bytes equal to chr(0) in the case when no AppenByte was called.
On the contrary, if some AppenByte is called, then chunk.Buffer just contain
the appended bytes.

I am going to have a doubt : is AppendByte a "clear+update" operation ?
The name "append" made me think as "append-back" .

Thanks for support.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The Buffer as returned is the Array that backs the ArrayBuffer, so I would expect it to be the size of the capacity. It is managed by the ArrayBuffer and is not guaranteed to be the length of the data.

This simple test may help demonstrate:

B4X:
    Dim SLB As SLByteArrayBuffer
    SLB.Initialize(2)
    Dim Content() As Byte = SLB.ToByteArray
    Log("BAB Len " & SLB.length & " Buffer Len " & SLB.Buffer.Length & " Capacity " & SLB.Capacity & " Content Len " & Content.Length)
   
    SLB.AppendByte(Array As Byte(0xF0,0xF0,0xF0,0xF0),0,4)
    Content = SLB.ToByteArray
    Log("BAB Len " & SLB.length & " Buffer Len " & SLB.Buffer.Length & " Capacity " & SLB.Capacity & " Content Len " & Content.Length)
   
   
    SLB.AppendByte(Array As Byte(0xF0,0xF0),0,2)
    Content = SLB.ToByteArray
    Log("BAB Len " & SLB.length & " Buffer Len " & SLB.Buffer.Length & " Capacity " & SLB.Capacity & " Content Len " & Content.Length)

Results In:

B4X:
Program started.
BAB Len 0 Buffer Len 2 Capacity 2 Content Len 0
BAB Len 4 Buffer Len 4 Capacity 4 Content Len 4
BAB Len 6 Buffer Len 8 Capacity 8 Content Len 6

I think that the nature of arrays means that a new array will be created for each append, and then the old and appended data added to the array.
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
Hence, the solution should be in calling

SLB.ToByteArray

instead of using

SLB.Buffer

Do you agree ?
 
Upvote 0
Top