Move the code for copy a specific amount to IOUtils
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/57d1e47f Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/57d1e47f Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/57d1e47f Branch: refs/heads/master Commit: 57d1e47f9559475c0e95a6adeb56da47f19c02ed Parents: 1138017 Author: Daniel Kulp <[email protected]> Authored: Tue Mar 25 12:53:26 2014 -0400 Committer: Daniel Kulp <[email protected]> Committed: Tue Mar 25 12:53:26 2014 -0400 ---------------------------------------------------------------------- .../java/org/apache/cxf/helpers/IOUtils.java | 53 ++++++++++++++++++++ .../cxf/interceptor/LoggingInInterceptor.java | 12 +---- 2 files changed, 54 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/57d1e47f/core/src/main/java/org/apache/cxf/helpers/IOUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java index 140fca2..56c26c0 100644 --- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java +++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java @@ -166,6 +166,35 @@ public final class IOUtils { } return total; } + + /** + * Copy at least the specified number of bytes from the input to the output + * or until the inputstream is finished. + * @param input + * @param output + * @param atLeast + * @throws IOException + */ + public static void copyAtLeast(final InputStream input, + final OutputStream output, + int atLeast) throws IOException { + final byte[] buffer = new byte[4096]; + int n = atLeast > buffer.length ? buffer.length : atLeast; + n = input.read(buffer, 0, n); + while (-1 != n) { + if (n == 0) { + throw new IOException("0 bytes read in violation of InputStream.read(byte[])"); + } + output.write(buffer, 0, n); + atLeast -= n; + if (atLeast <= 0) { + return; + } + n = atLeast > buffer.length ? buffer.length : atLeast; + n = input.read(buffer, 0, n); + } + } + public static void copy(final Reader input, final Writer output, final int bufferSize) throws IOException { @@ -295,6 +324,30 @@ public final class IOUtils { //nothing - just discarding } } + + /** + * Consumes at least the given number of bytes from the input stream + * @param input + * @param atLeast + * @throws IOException + */ + public static void consume(final InputStream input, + int atLeast) throws IOException { + final byte[] buffer = new byte[4096]; + int n = atLeast > buffer.length ? buffer.length : atLeast; + n = input.read(buffer, 0, n); + while (-1 != n) { + if (n == 0) { + throw new IOException("0 bytes read in violation of InputStream.read(byte[])"); + } + atLeast -= n; + if (atLeast <= 0) { + return; + } + n = atLeast > buffer.length ? buffer.length : atLeast; + n = input.read(buffer, 0, n); + } + } public static byte[] readBytesFromStream(InputStream in) throws IOException { int i = in.available(); http://git-wip-us.apache.org/repos/asf/cxf/blob/57d1e47f/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java index c88cd71..f6e56ff 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java @@ -191,17 +191,7 @@ public class LoggingInInterceptor extends AbstractLoggingInterceptor { //only copy up to the limit since that's all we need to log //we can stream the rest - byte bytes[] = new byte[2048]; - int i = bis.read(bytes); - int count = 0; - while (count <= limit && i != -1) { - bos.write(bytes, 0, i); - count += i; - i = bis.read(bytes); - } - if (i > 0) { - bos.write(bytes, 0, i); - } + IOUtils.copyAtLeast(bis, bos, limit); bos.flush(); bis = new SequenceInputStream(bos.getInputStream(), bis);
