diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/InputStreamResponseListener.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/InputStreamResponseListener.java index 44b886063f5..916efe9acb9 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/InputStreamResponseListener.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/InputStreamResponseListener.java @@ -220,7 +220,7 @@ public class InputStreamResponseListener extends Response.Listener.Empty else if (bytes != null) { if (index < bytes.length) - return bytes[index++]; + return bytes[index++] & 0xFF; length.addAndGet(-index); bytes = null; index = 0; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientStreamTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientStreamTest.java index ab7aaf13814..f4dac19a634 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientStreamTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientStreamTest.java @@ -58,6 +58,8 @@ import org.junit.Assert; import org.junit.Test; import static java.nio.file.StandardOpenOption.CREATE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class HttpClientStreamTest extends AbstractHttpClientServerTest @@ -150,6 +152,46 @@ public class HttpClientStreamTest extends AbstractHttpClientServerTest Assert.assertSame(response, result.getResponse()); } + @Test + public void testDownloadOfUTF8Content() throws Exception + { + final byte[] data = new byte[]{(byte)0xC3, (byte)0xA8}; // UTF-8 representation of è + start(new AbstractHandler() + { + @Override + public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + baseRequest.setHandled(true); + response.getOutputStream().write(data); + } + }); + + InputStreamResponseListener listener = new InputStreamResponseListener(); + client.newRequest("localhost", connector.getLocalPort()) + .scheme(scheme) + .send(listener); + Response response = listener.get(5, TimeUnit.SECONDS); + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatus()); + + InputStream input = listener.getInputStream(); + Assert.assertNotNull(input); + + for (byte b : data) + { + int read = input.read(); + assertTrue(read >= 0); + assertEquals(b & 0xFF, read); + } + + assertEquals(-1, input.read()); + + Result result = listener.await(5, TimeUnit.SECONDS); + Assert.assertNotNull(result); + Assert.assertFalse(result.isFailed()); + Assert.assertSame(response, result.getResponse()); + } + @Test public void testDownloadWithFailure() throws Exception {