Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 739B010773 for ; Tue, 25 Mar 2014 17:31:18 +0000 (UTC) Received: (qmail 68111 invoked by uid 500); 25 Mar 2014 17:31:15 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 67908 invoked by uid 500); 25 Mar 2014 17:31:11 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 67743 invoked by uid 99); 25 Mar 2014 17:31:08 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Mar 2014 17:31:08 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id C5021919B5F; Tue, 25 Mar 2014 17:31:07 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: dkulp@apache.org To: commits@cxf.apache.org Date: Tue, 25 Mar 2014 17:31:08 -0000 Message-Id: <599ca9dea595469fae9d57a2359b3daa@git.apache.org> In-Reply-To: <55c1ef2e15b043789e69935439a9622f@git.apache.org> References: <55c1ef2e15b043789e69935439a9622f@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] git commit: Move the code for copy a specific amount to IOUtils 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 Authored: Tue Mar 25 12:53:26 2014 -0400 Committer: Daniel Kulp 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);