B4A Library ByteArrayBuffer

This is a wrapper for the org.apache.http.util.ByteArrayBuffer class. It allows the resizing of the buffer which is useful for buffering very large amounts of data without the buffer continually growing (i.e. audio streaming).

SLByteArrayBuffer
Author:
Steve Laming
Version: 1
  • SLByteArrayBuffer
    Methods:
    • AppendByte (b() As Byte, off As Int, len As Int)
      Appends len bytes to this buffer from the given source array starting at index off.
    • AppendChar (b() As Char, off As Int, len As Int)
      Appends len chars to this buffer from the given source array starting at index off.
    • AppendInt (b As Int)
      Appends b byte to this buffer.
    • Buffer As Byte[]
      Returns reference to the underlying byte array.
    • ByteAt (i As Int) As Int
      Returns the byte value in this buffer at the specified index.
    • Capacity As Int
      Returns the current capacity.
    • Clear
      Clears content of the buffer.
    • Initialize (capacity As Int)
      ByteArrayBuffer(int capacity)
      Initialize the ByteArrayBuffer to the given capacity.
    • IsEmpty As Boolean
      Returns true if this buffer is empty, that is, its length() is equal to 0.
    • IsFull As Boolean
      Returns true if this buffer is full, that is, its length() is equal to its capacity().
    • SetLength (len As Int)
      Sets the length of the buffer.
    • ToByteArray As Byte[]
      Converts the content of this buffer to an array of bytes.
    • length As Int
      Returns the length of the buffer (byte count).
Xml Document converted with warwounds B4A library reference generator

V1.0 attached unzip the .jar and .xml files to you additional libraries folder.
 

Attachments

  • SLByteArrayBuffer.zip
    2.7 KB · Views: 1,332

doncx

Active Member
Licensed User
Longtime User
Steve -

Great library idea!

I'm having trouble appending the SLByteArrayBuffer from serial data received. I get a NullPointerException at line 45 in your library when I attempt AppendByte in the (selectively pasted) code below. I'm trying to build a combined byte array from incoming bluetooth traffic for later processing. Can you tell me what I'm doing wrong?

Thanks,

- Don

B4X:
Sub Process_Globals
    Dim BigBuffer As SLByteArrayBuffer
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    LogBytes( Buffer )
End Sub

Sub LogBytes( Msg() As Byte )
    BigBuffer.AppendByte( Msg, 0, Msg.Length )
End Sub
 

doncx

Active Member
Licensed User
Longtime User
Steve -

Great library idea!

I'm having trouble appending the SLByteArrayBuffer from serial data received. I get a NullPointerException at line 45 in your library when I attempt AppendByte in the (selectively pasted) code below. I'm trying to build a combined byte array from incoming bluetooth traffic for later processing. Can you tell me what I'm doing wrong?

Thanks,

- Don

B4X:
Sub Process_Globals
    Dim BigBuffer As SLByteArrayBuffer
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    LogBytes( Buffer )
End Sub

Sub LogBytes( Msg() As Byte )
    BigBuffer.AppendByte( Msg, 0, Msg.Length )
End Sub

Nevermind! I neglected to initialize the SLByteArrayBuffer.

Sheesh. Sorry to bother you.
 

stevel05

Expert
Licensed User
Longtime User
Nevermind! I neglected to initialize the SLByteArrayBuffer.

Easy done, glad you found it.:)
 

freedom2000

Well-Known Member
Licensed User
Longtime User
@stevel05 : First of all Thank you for this great library

I am trying to use this lib to avoid memory overflow while processing audiostream.
Thus I have a streamer which output buffers whose size is 3584

B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
    'collect the recording data
    Buffers.Add(Buffer)

    BuffersSize = BuffersSize + Buffer.Length
    Log(Buffer.length)    '3584
 
End Sub

I can easily empty these buffers into a ByteArrayBuffer with AppendByte
B4X:
BigBuffer.AppendByte( Buffer, 0, Buffer.Length )

However I need to process the data from the BigBuffer every 4096 bytes to perform a FFT.
What would be nice would be to "resize" the bigbuffer any time I read 4096 bytes in order to decrease its size.

Is there a way to do it ?
- streamer happends at the end of BigBuffer
- I read from the beginning 4096 bytes
- BigBuffer start pointer is moved 4096 bytes headed

Thanks
 

stevel05

Expert
Licensed User
Longtime User
To remove from the front of the buffer, you would have to set up a second buffer, append the data you want to keep to that then set the original to point to the new buffer. I'm not sure how quick it would be as you're processing Audio, but worth a try.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Thank you, I will try it

BTW, the FFT proccess is not very fast so I have to wait somewhere !
 

sunish

Member
Licensed User
Longtime User
Is there a direct method to add or assign a string to a SLByteArrayBuffer ?
 

stevel05

Expert
Licensed User
Longtime User
You could use the GetBytes method of the String.
B4X:
bb.append(str.GetBytes("UTF8"),0,str.Length)
 

sunish

Member
Licensed User
Longtime User
You could use the GetBytes method of the String.
B4X:
bb.append(str.GetBytes("UTF8"),0,str.Length)
Thanks, I just realized I had already written the code long back 'RxBuff.AppendByte(SerData.GetBytes("UTF8"),0,SerData.Length)
and nice to know its the right way.
 
Last edited:
Top