I need to truncate a byte array. For example the array has 100 elements but only the first 3 are used. So I wrote this method to truncate the array. The copy is working correctly (b2 looks good). But when the calling method gets the array it only has one element in it.
// return new array by truncating passed array.
private static byte Truncate(byte b1, int nextAvailable)
byte b2 = new byte[nextAvailable];
System.arraycopy(b1, 0, b2, 0, nextAvailable);
return b2; // b2 looks ok here
I am calling like this:
byte b = Truncate(result, nextAvailable); // b only contains element zero here.