Java - Byte [] to byte []

There is Vector and DataOutputStream. I need to write bytes from Vector (toArray returns Byte []) in a stream, but it only understands byte []. How to convert Byte [] to byte []?

+16
java byte bytearray
Jun 21 '11 at 19:23
source share
3 answers

Can you use the toPrimitive method in the Apache Commons lang library ArrayUtils class?

+28
Jun 21 '11 at 19:28
source share
— -
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; } 

freeone3000 in this answer :)

+8
Jun 16 '14 at 10:01
source share

The <Byte> vector is an inefficient structure, as you could use for storing bytes. I would seriously consider using a more efficient ByteArrayOutputStream string, which has a toByteArray () method. those. not just convert the vector, but remove it from the code.

+1
Jun 22 '11 at 8:10
source share



All Articles