cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [1/3] git commit: Move the code for copy a specific amount to IOUtils
Date Tue, 25 Mar 2014 17:45:55 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.6.x-fixes 1e73a27ab -> 6c42790b1


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/a0b48255
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/a0b48255
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/a0b48255

Branch: refs/heads/2.6.x-fixes
Commit: a0b48255b42182a9bfc22da8c2d7119061cdf6f2
Parents: 1e73a27
Author: Daniel Kulp <dkulp@apache.org>
Authored: Tue Mar 25 12:53:26 2014 -0400
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Tue Mar 25 13:45:12 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/a0b48255/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 f4f1e6e..b4cb701 100644
--- a/api/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/api/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -144,6 +144,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 {
@@ -259,6 +288,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/a0b48255/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 e363fb2..ac88ff6 100644
--- a/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
+++ b/api/src/main/java/org/apache/cxf/interceptor/LoggingInInterceptor.java
@@ -167,17 +167,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);
             


Mime
View raw message