Thursday, July 17, 2025

ByteArrayBuilder: A simplfied version of java.nio.ByteBuffer

I was doing some work in video streaming from mobile phones to PC recently and I was dealing with a lot of byte array streams from client to server, which I also needed to collocate on the server side. 

I decided to write a class that simplifies the whole process, making the array management process as simple as using StringBuilder in Java; but for byte arrays.

"Why did you need to do that when you could use java.nio.ByteBuffer?" you may ask me, but well sometimes you need something simpler in usage and well you wonder secretly, if you can do it faster.

So I wrote ByteArrayBuilder.java
I didn't think of benchmarking it that much since I had little thought about it. I was more focused on the ease of use.

Recently however, I asked Github Copilot to analyze the file and it told me a lot about it, including the fact that I was better off with using java.nio.ByteBuffer . What I was doing was tantamount to reinventing the wheel according to the AI. It also advised me to run benchmarks of ByteArrayBuilder.java against java.nio.ByteBuffer. And that was what I did.

I wrote a benchmark found at: ByteArrayBuilderWars.java
Here are the results:


I sent a  number of byte arrays say 
numberOfArrays
and each of these byte arrays had a size of
dataArraySize

public static void main(String[] args) throws IOException {

        int dataArraySize = 10000;
        int numberOfArrays = 100000;
        
        for (int i = 0; i < 10; i++) {
            ByteArrayBuilder builder = ByteArrayBuilderWars.benchmarkAppendForByteArrayBuilder(dataArraySize, numberOfArrays);
            ByteBuffer buffer = ByteArrayBuilderWars.benchmarkAppendForByteBuffer(dataArraySize, numberOfArrays);

            ByteArrayBuilderWars.benchmarkReadBytes(builder);
            ByteArrayBuilderWars.benchmarkReadBytes(buffer);
        }

    }
Here are the results:

  
RESULTS for append ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
1000010022.16614.363
1000010011.3610.988
1000010012.4311.959
1000010010.38211.187
1000010011.04111.182
RESULTS for bytes-reader ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
100001000.0050.477
100001000.0090.737
100001000.0050.589
100001000.0060.154
100001000.0050.146
RESULTS for append ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
100001000120.38110.924
100001000104.201105.941
100001000103.048102.265
100001000103.61102.372
100001000102.899101.902
RESULTS for bytes-reader ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
1000010000.0085.089
1000010000.0062.847
1000010000.0052.734
1000010000.0052.896
1000010000.0072.681
RESULTS for append ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
10000100001106.5891077.174
10000100001023.7011012.844
10000100001067.0481041.951
10000100001049.6631029.896
10000100001069.8671046.699
RESULTS for bytes-reader ops(ms)
arraySizenumArraysByteArrayBuilder(ms)java.nio.ByteBuffer(ms)
10000100000.00530.745
10000100000.00622.519
10000100000.00324.707
10000100000.00324.299
10000100000.00325.242


The results say it all. I ran each result row and took the average of 10 samples of the quantity being measured, then I recorded 5 samples of the said average.




The append rates are very much similar, though java.nio.ByteBuffer consistently outperforms ByteArrayBuilder, even though, it is ever so slightly.

There are at least 3 instances though where the ByteArrayBuilder outperforms the java.nio.ByteBuffer
, even though the difference once again is ever so minute.

When it comes to reading the bytes though, it is a classic massacre.
The ByteArrayBuilder is thousands of time faster than the java.nio.ByteBuffer every single time.
This is because when well written, the ByteArrayBuilder has the array to be read at the ready and simply returns it.

So Github Copilot, sorry ByteArrayBuilder  is a much better choice than the java.nio.ByteBuffer for simple byte array collocation and reading.

Thanks guys.

You can view the class and its benchmarks on Github at ByteArrayBuilder.java  and

 ByteArrayBuilderWars.java , respectively


 

ByteArrayBuilder: A simplfied version of java.nio.ByteBuffer

I was doing some work in video streaming from mobile phones to PC recently and I was dealing with a lot of byte array streams from client to...