The method below receives byte[] b of indeterminate length. It contains a string of characters to be printed to a console-style window. I would like to split b at the first line-feed character, so that that character is included in the first array. However, at the moment this throws an ArrayOutOfBoundsError, because stringBytes and extraBytes are declared with zero size. How can I get around this problem?
byte[] stringBytes = {};
byte[] extraBytes = {};
int i = 0;
while(i < b.length) {
stringBytes[i] = b[i];
if(b[i] == '\n' && i + 1 != b.length) {
while(i < b.length) {
extraBytes[i - stringBytes.length] = b[i++];
}
break;
}
i++;
}
byte[]to a String and split the string on\n.extraBytesafter you know how long it will be.byte[] stringBytes = new byte[b.length]and shrink the arrays later on or useLists and transform those to arrays afterwards.