How to convert byte[] to Byte[] and also Byte[] to byte[], in the case of not using any 3rd party library?
Is there a way to do it fast just using the standard library?
byte[] to Byte[] :
byte[] bytes = ...; Byte[] byteObject = ArrayUtils.toObject(bytes); Byte[] to byte[] :
Byte[] byteObject = new Byte[0]; byte[] bytes = ArrayUtils.toPrimitive(byteObject); Note: ArrayUtils are not a part of Java, but Apache lib
ArrayUtils is not part of standard Java packages.Byte class is a wrapper for the primitive byte. This should do the work:
byte[] bytes = new byte[10]; Byte[] byteObjects = new Byte[bytes.length]; int i=0; // Associating Byte array values with bytes. (byte[] to Byte[]) for(byte b: bytes) byteObjects[i++] = b; // Autoboxing. .... int j=0; // Unboxing Byte values. (Byte[] to byte[]) for(Byte b: byteObjects) bytes[j++] = b.byteValue(); Byte.valueOf(b) over new Byte(b). I would be surprised if the Byte class didn't cache every single value for a byte.Byte.valueOf(byte). JavaDocs say that this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.byteObjects[i++] = b;new Byte[bytes.length]; instead of new Byte[10]; to keep it sensible.Java 8 solution:
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.
You could use the toPrimitive method in the Apache Commons lang library ArrayUtils class, As suggested here - Java - Byte[] to byte[]
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; } If someone preferes Stream API over ordinary loops.
private Byte[] toObjects(byte[] bytes) { return IntStream.range(0, bytes.length) .mapToObj(i -> bytes[i]) .toArray(Byte[]::new); } 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:
byte[] fromListOfNotByteArray (List<byte[]> list) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos; try { oos = new ObjectOutputStream(baos); oos.writeObject(list); } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); } 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. ;)
Now you know -- teach others.
Byte[]? Doesn't seem like a good idea... either usebyte[]orList<Byte>.Byte[]to abyte[]if you have null references...