byte[] toPrimitives(Byte[] oBytes)
{
byte[] bytes = new byte[oBytes.length];
for(int i = 0; i < oBytes.length; i++){
bytes[i] = oBytes[i];
}
return bytes;
}
Inverse:
//byte[] to Byte[]
Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte b : bytesPrim) bytes[i++] = b; //Autoboxing
return bytes;
}
Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
Arrays.setAll(bytes, n -> bytesPrim[n]);
return bytes;
}
Unfortunately, you can't do this to convert from Byte[] to byte[]. Arrays has setAll for double[], int[], and long[], but not for other primitive types.
Step back. Look at the bigger picture. You're stuck converting byte[] to Byte[] or vice versa because of Java's strict type casing with something like this
List< Byte>
or
List<Byte[]>
Now you have byte[] and Byte[] and have to convert. This will help.
Keep all your byte[]s in a list like this: List<byte[]> instead of List< Byte> or List<Byte[]>. (byte is a primitive, byte[] is an object)
As you acquire bytes do this (networking socket example):
ArrayList<byte[]> compiledMessage = new ArrayList<byte[]>;
...
compiledMessage.add(packet.getData());
Then, when you want to put all your bytes in a single message, do this:
That way, you can keep all your parts in List<byte[]> and your whole in byte[] without a bunch of crazy copy tasks in for loops with little baby byte[]s everywhere. ;)