B4A Library ByteArrayBuffer

Discussion in 'Additional libraries, classes and official updates' started by stevel05, Nov 8, 2013.

  1. stevel05

    stevel05 Expert Licensed User

    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.
     

    Attached Files:

    noeleon, Volga, koaunglay and 6 others like this.
  2. doncx

    doncx Member Licensed 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

    Code:
    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
     
  3. doncx

    doncx Member Licensed User

    Nevermind! I neglected to initialize the SLByteArrayBuffer.

    Sheesh. Sorry to bother you.
     
  4. stevel05

    stevel05 Expert Licensed User

    Easy done, glad you found it.:)
     
  5. freedom2000

    freedom2000 Well-Known Member Licensed 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

    Code:
    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
    Code:
    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
     
  6. stevel05

    stevel05 Expert Licensed 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 likes this.
  7. freedom2000

    freedom2000 Well-Known Member Licensed User

    Thank you, I will try it

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

    sunish Member Licensed User

    Is there a direct method to add or assign a string to a SLByteArrayBuffer ?
     
  9. stevel05

    stevel05 Expert Licensed User

    You could use the GetBytes method of the String.
    Code:
    bb.append(str.GetBytes("UTF8"),0,str.Length)
     
    sunish likes this.
  10. sunish

    sunish Member Licensed User

    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: Sep 27, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice