Java - Byte[] to byte[] Java - Byte[] to byte[] arrays arrays

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;}

freeone3000 contributed in this answer :)


A Vector<Byte> is about as inefficient structure as you could use to store bytes. I would serious consider using something more efficient line ByteArrayOutputStream which has a toByteArray() method. i.e. don't just convert the Vector but remove it from the code.