Repository: cxf Updated Branches: refs/heads/2.7.x-fixes 29336a2d1 -> 8459bc9b4
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/d2578fef Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/d2578fef Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/d2578fef Branch: refs/heads/2.7.x-fixes Commit: d2578feffa8a0e1ae1272535aa411cd4715e1c01 Parents: 29336a2 Author: Daniel Kulp <[email protected]> Authored: Tue Mar 25 12:53:26 2014 -0400 Committer: Daniel Kulp <[email protected]> Committed: Tue Mar 25 13:31:59 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/d2578fef/api/src/main/java/org/apache/cxf/helpers/IOUtils.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/cxf/helpers/IOUtils.java b/api/src/main/java/org/apache/cxf/helpers/IOUtils.java index 140fca2..56c26c0 100644 --- a/api/src/main/java/org/apache/cxf/helpers/IOUtils.java +++ b/api/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/d2578fef/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java b/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java index e487d9a..7f224ba 100644 --- a/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java +++ b/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java @@ -190,17 +190,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);
