I have a byte array in memory, read from a file. I would like to split the byte array at a certain point (index) without having to just create a new byte array and copy each byte at a time, increasing the in memory foot print of the operation. What I would like is something like this:
byte[] largeBytes = [1,2,3,4,5,6,7,8,9]; byte[] smallPortion; smallPortion = split(largeBytes, 3);
smallPortion
would equal 1,2,3,4largeBytes
would equal 5,6,7,8,9
Solution: To split a byte string into a list of lines—each line being a byte string itself—use the Bytes. split(delimiter) method and use the Bytes newline character b'\n' as a delimiter.
To concatenate multiple byte arrays, you can use the Bytes. concat() method, which can take any number of arrays.
ByteArray is an extremely powerful Class that can be used for many things related to data manipulation, including (but not limited to) saving game data online, encrypting data, compressing data, and converting a BitmapData object to a PNG or JPG file.
You can simply iterate the byte array and print the byte using System. out. println() method.
In C# with Linq you can do this:
smallPortion = largeBytes.Take(4).ToArray(); largeBytes = largeBytes.Skip(4).Take(5).ToArray();
;)
FYI. System.ArraySegment<T>
structure basically is the same thing as ArrayView<T>
in the code above. You can use this out-of-the-box structure in the same way, if you'd like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With