Difference between String.getBytes() and IOUtils.toByteArray()? Difference between String.getBytes() and IOUtils.toByteArray()? arrays arrays

Difference between String.getBytes() and IOUtils.toByteArray()?


Specifying the encoding is important.

You haven't provided any encoding for the libraries to work with, and as a result the "default" encoding will be used instead. I'm guessing that since one of your byte arrays is twice the size of the other, one encoding used is UTF-16 and the other UTF-8/ASCII.

Try this:

public void testInputStreamToByteArray() throws IOException {    byte[] expecteds = LOREM_IPSUM.getBytes("UTF-8");    byte[] actuals = org.apache.commons.io.IOUtils.toByteArray(new StringReader(LOREM_IPSUM), "UTF-8");    assertArrayEquals(expecteds, actuals);}