B4J Library jSLByteArrayBuffer

This is a wrapper for the org.apache.http.util.ByteArrayBuffer class compiled for B4j. 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

It depends on httpcore-4.0.jar, which I believe is in the core library release. Please let me know if your system can't find it.

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

Attachments

  • jSLByteArrayBuffer.zip
    2.7 KB · Views: 599

stevel05

Expert
Licensed User
Longtime User
That really depends on the context, it's an advanced version of an array to which you can append elements up to the current capacity.

Without writing a complete app it's something like:

B4X:
Dim Arr As SLByteArrayBuffer
Arr.Initialize(100)
Dim b() As Byte = Array As Byte(0xF0,0xF1,0xFF)
Dim c() As Byte = Array As Byte(0x20,0x21,0X22)
Arr.AppendByte(b,0,b.length)
Arr.AppendByte(c,0,c.length)
Log(Arr.Capacity)
Log(Arr.Length)

Dim Result() As Byte = Arr.ToByteArray

The Oracle documentation is here: http://hc.apache.org/httpcomponents...ocs/org/apache/http/util/ByteArrayBuffer.html
 

stevel05

Expert
Licensed User
Longtime User
When I first found this I was writing a streaming app in B4a, the benefit I found in using this was that redimming an existing array didn't immediately free the unused memory, whereas this did.
 

tigrot

Well-Known Member
Licensed User
Longtime User
Hi Stevel05,
can you give some hint, pls? I understand that i can "append" data. How can I "return & delete" data from buffer?
I have to return a fixed length amount of buffer, to pass to an encoder and I must leave the rest of the buffer "as is".
Thank you very much for this work of yours!

Mauro
 

stevel05

Expert
Licensed User
Longtime User
Hi Mauro,

There are no methods to delete data from arbitrary positions in the buffer.

You can get a normal byte array from the buffer with the ToByteArray method, then extract your data from that. Then clear the buffer, and append the rest of the normal byte array back into the buffer using the offset and length parameters to select the data you want to send back.

I haven't tried it, but you may be able to use the Buffer method to get a reference to the underlying array, and work directly on that, as long as you aren't still appending data while you do it. Although you still won't be able to delete from the array.

I think the first option is the cleanest.

Steve
 

sunish

Member
Licensed User
Longtime User
This is a wrapper for the org.apache.http.util.ByteArrayBuffer class compiled for B4j. 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

It depends on httpcore-4.0.jar, which I believe is in the core library release. Please let me know if your system can't find it.

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

I am getting the message cannot find httpcore-4.0 jar after compilation, using B4a 3.82
Android.jar is level 19.
Sunish
 
Top