@Test public void bufferSize() throws Exception { InputStream in = new ByteArrayInputStream(new byte[8 * 1024]); OutputStream out = mock(OutputStream.class); IOUtils.copyLarge(in, out); // Default buffer size of 4KB required two reads to copy 8KB input stream verify(out, times(2)).write(any(byte[].class), anyInt(), anyInt()); }
/** * Re-implement copy method to allow buffer size to be configured. This won't impact all methods because there is no * polymorphism for static methods, but rather just direct use of these two methods. */ public static long copyLarge(InputStream input, OutputStream output) throws IOException { return copyLarge(input, output, bufferSize); }
private OutputHandler handleInputStream(InputStream is) { return (event, out) -> { try { IOUtils.copyLarge(is, out); } finally { is.close(); } }; }
private Object transformStream(InputStream is) throws IOException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try { IOUtils.copyLarge(is, byteOut); } finally { is.close(); } return byteOut.toByteArray(); } }
protected String createStringFromInputStream(InputStream input, Charset outputEncoding) throws TransformerException { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copyLarge(input, outputStream); return outputStream.toString(outputEncoding.name()); } catch (IOException e) { throw new TransformerException(errorReadingStream(), e); } finally { try { input.close(); } catch (IOException e) { logger.warn("Could not close stream", e); } } }
@Test public void bufferSize() throws Exception { InputStream in = new ByteArrayInputStream(new byte[8 * 1024]); OutputStream out = mock(OutputStream.class); IOUtils.copyLarge(in, out); // Default buffer size of 4KB required two reads to copy 8KB input stream verify(out, times(2)).write(any(byte[].class), anyInt(), anyInt()); }