Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 269
Falci Avatar asked Nov 23 '12 17:11

Falci


People also ask

What is getBytes ()?

The method getBytes() encodes a String into a byte array using the platform's default charset if no argument is passed. We can pass a specific Charset to be used in the encoding process, either as a String object or a String object.

How to convert InputStream to String?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

How do you convert Inputstreams to bytes?

The IOUtils type has a static method to read an InputStream and return a byte[] . InputStream is; byte[] bytes = IOUtils. toByteArray(is); Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray() .

How do you read all bytes from InputStream?

Since Java 9, we can use the readAllBytes() method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.


1 Answers

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);
}
like image 183
Phil K Avatar answered Sep 23 '22 04:09

Phil K