12

I'm testing the IOUtils. I have problems to convert an InputStream into a byte array:

private static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

@Test
public void testInputStreamToByteArray() throws IOException {

    byte[] expecteds = LOREM_IPSUM.getBytes();
    byte[] actuals = org.apache.commons.io.IOUtils.toByteArray(new StringInputStream(LOREM_IPSUM));

    assertArrayEquals(expecteds, actuals);
}

Stacktrace:

java.lang.AssertionError: array lengths differed, expected.length=56 actual.length=112
    at org.junit.Assert.fail(Assert.java:91)
    at org.junit.internal.ComparisonCriteria.assertArraysAreSameLength(ComparisonCriteria.java:72)
    at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:36)
    at org.junit.Assert.internalArrayEquals(Assert.java:414)
    at org.junit.Assert.assertArrayEquals(Assert.java:200)
    at org.junit.Assert.assertArrayEquals(Assert.java:213)
    at [...].testInputStreamToByteArray(HttpsTest.java:20)[...]

I do not see why not pass the test. What is wrong?

0

1 Answer 1

10

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);
}
3
  • Thanks! But toByteArray not accept InputStream and String. Two options: IOUtils.toByteArray (new StringReader (LOREM_IPSUM), "UTF-8"); IOUtils.toByteArray (new InputStreamReader (is), "UTF-8");
    – Falci
    Nov 23, 2012 at 17:27
  • Ah, didn't notice that you're using java.io.InputStream. You'll need to use a Reader, or more specifically StringReader: byte[] actuals = org.apache.commons.io.IOUtils.toByteArray(new StringReader(LOREM_IPSUM), "UTF-8");
    – Phil K
    Nov 23, 2012 at 17:30
  • My comment was just to clarify. Thank you again.
    – Falci
    Nov 23, 2012 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.