/** * Converts the given input stream into a string with the given charset. * * @see #toBytes(InputStream) * @param is * the input stream to read from * @param charset * the charset for the created string * @return the string created from the given input stream */ public static String toString(InputStream is, Charset charset) { return new String(toBytes(is), charset); }
@Test public void testToBytes() throws IOException { assertArrayEquals("test".getBytes(), IOUtils.toBytes(new ByteArrayInputStream("test".getBytes()))); }
@Test public void testToBytesWithEmptyByteArray() throws IOException { assertArrayEquals(new byte[0], IOUtils.toBytes(new ByteArrayInputStream(new byte[0]))); }
@Test public void testToBytesRethrowsIOExceptionWrapped() throws IOException { InputStream is = mock(InputStream.class); IOException thrownException = new IOException("whatever"); when(is.read(any(), anyInt(), anyInt())).thenThrow(thrownException); try { IOUtils.toBytes(is); fail("expected exception to be thrown"); } catch (RuntimeException re) { assertSame(thrownException, re.getCause()); } }
private void testBase64DecodingFails(String base64InputString) throws WebApplicationException, IOException { InputStream is = fetchWrappedInputStream(base64InputString); try { IOUtils.toBytes(is); } catch (Exception e) { throw new Base64DecodingFailedException(e); } }
@Test public void testToBytesRethrowsRuntimeExceptionAsIs() throws IOException { InputStream is = mock(InputStream.class); RuntimeException thrownException = new RuntimeException("whatever"); when(is.read(any(), anyInt(), anyInt())).thenThrow(thrownException); try { IOUtils.toBytes(is); fail("expected exception to be thrown"); } catch (RuntimeException re) { assertSame(thrownException, re); } }
private void testBase64Decoding(String base64InputString, byte[] expectedReadBytes) throws IOException { assertArrayEquals(expectedReadBytes, IOUtils.toBytes(fetchWrappedInputStream(base64InputString))); }
@Test public void testNonBinaryZippedWhenZippingRequestedAndNotBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(false), (jersey) -> { Response response = jersey.target() .path("/non-binary") .request() .header(HttpHeaders.ACCEPT_ENCODING, "gzip") .get(); assertEquals(200, response.getStatus()); assertNotNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNotNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); InputStream unzipStream = new GZIPInputStream(response.readEntity(InputStream.class)); assertEquals("non-binary", new String(IOUtils.toBytes(unzipStream))); }); }
@Test public void createContainerRequest_BodyGiven_ShouldMapToInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody("some body") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals("some body".getBytes(), IOUtils.toBytes(containerRequest.getEntityStream())); }
@Test public void createContainerRequest_NullBodyGiven_ShouldMapToEmptyInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody(null) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals(new byte[0], IOUtils.toBytes(containerRequest.getEntityStream())); }