cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [1/3] cxf git commit: Use IOUtils methods in CachedStreamTestBase to handle stream reading
Date Fri, 17 Jul 2015 15:35:50 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes dd618ce58 -> c7b9638cf


Use IOUtils methods in CachedStreamTestBase to handle stream reading

Conflicts:
	core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/17fef200
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/17fef200
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/17fef200

Branch: refs/heads/3.0.x-fixes
Commit: 17fef20055ca5ae3495e6fe923a9048dab0ac3a7
Parents: dd618ce
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Fri Jul 17 16:25:07 2015 +0100
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Fri Jul 17 16:26:55 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/cxf/helpers/IOUtils.java    | 23 ++++++-
 .../org/apache/cxf/io/CachedStreamTestBase.java | 67 ++++++++------------
 2 files changed, 46 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/17fef200/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 aab3ab9..5fab355 100644
--- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -229,13 +229,32 @@ public final class IOUtils {
             n = input.read(buffer, 0, n);
         }
     }
+    
+    public static void copyAtLeast(final Reader input, 
+                                   final Writer output,
+                                   int atLeast) throws IOException {
+        final char[] buffer = new char[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 Reader.read(char[])");
+            }
+            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 {
         final char[] buffer = new char[bufferSize];
-        int n = 0;
-        n = input.read(buffer);
+        int n = input.read(buffer);
         while (-1 != n) {
             output.write(buffer, 0, n);
             n = input.read(buffer);

http://git-wip-us.apache.org/repos/asf/cxf/blob/17fef200/core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java b/core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java
index 56e35c1..222705c 100755
--- a/core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java
+++ b/core/src/test/java/org/apache/cxf/io/CachedStreamTestBase.java
@@ -19,17 +19,19 @@
 package org.apache.cxf.io;
 
 import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
+import java.io.StringWriter;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
+import org.apache.cxf.helpers.IOUtils;
 import org.easymock.EasyMock;
 import org.easymock.IMocksControl;
-
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -171,10 +173,8 @@ public abstract class CachedStreamTestBase extends Assert {
         assertEquals("text is not decoded correctly", text, dectext);
 
         // close the partially read stream
-        if (fin instanceof InputStream) {
-            ((InputStream)fin).close();
-        } else if (fin instanceof Reader) {
-            ((Reader)fin).close();
+        if (fin instanceof Closeable) {
+            ((Closeable)fin).close();
         }
 
         // the file is deleted when cos is closed while all the associated inputs are closed
@@ -245,53 +245,27 @@ public abstract class CachedStreamTestBase extends Assert {
     }
     
     private static void close(Object obj) throws IOException {
-        if (obj instanceof CachedOutputStream) {
-            ((CachedOutputStream)obj).close();
-        } else if (obj instanceof CachedWriter) {
-            ((CachedWriter)obj).close();
-        } else if (obj instanceof InputStream) {
-            ((InputStream)obj).close();
-        } else if (obj instanceof Reader) {
-            ((Reader)obj).close();
+        if (obj instanceof Closeable) {
+            ((Closeable)obj).close();
         }
     }
 
     protected static String readFromStream(InputStream is) throws IOException {
-        ByteArrayOutputStream buf = new ByteArrayOutputStream();
-        try {
-            byte[] b = new byte[100];
-            for (;;) {
-                int n = is.read(b, 0, b.length);
-                if (n < 0) {
-                    break;
-                }
-                buf.write(b, 0, n);
-            }
-        } finally {
-            is.close();
+        try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
+            IOUtils.copyAndCloseInput(is, buf);
+            return new String(buf.toByteArray(), "UTF-8");
         }
-        return new String(buf.toByteArray(), "UTF-8");
     }
 
     protected static String readPartiallyFromStream(InputStream is, int len) throws IOException
{
-        ByteArrayOutputStream buf = new ByteArrayOutputStream();
-        byte[] b = new byte[len];
-        int rn = 0;
-        for (;;) {
-            int n = is.read(b, 0, b.length);
-            if (n < 0) {
-                break;
-            }
-            buf.write(b, 0, n);
-            rn += n;
-            if (len <= rn) {
-                break;
-            }
+        try (ByteArrayOutputStream buf = new ByteArrayOutputStream()) {
+            IOUtils.copyAtLeast(is, buf, len);
+            return new String(buf.toByteArray(), "UTF-8");
         }
-        return new String(buf.toByteArray(), "UTF-8");
     }
  
     protected static String readFromReader(Reader is) throws IOException {
+<<<<<<< HEAD
         StringBuffer buf = new StringBuffer();
         try {
             char[] b = new char[100];
@@ -304,11 +278,16 @@ public abstract class CachedStreamTestBase extends Assert {
             }
         } finally {
             is.close();
+=======
+        try (StringWriter writer = new StringWriter()) {
+            IOUtils.copyAndCloseInput(is, writer);
+            return writer.toString();
+>>>>>>> 17f140e... Use IOUtils methods in CachedStreamTestBase to handle
stream reading
         }
-        return buf.toString();
     }
     
     protected static String readPartiallyFromReader(Reader is, int len) throws IOException
{
+<<<<<<< HEAD
         StringBuffer buf = new StringBuffer();
         char[] b = new char[len];
         int rn = 0;
@@ -322,8 +301,12 @@ public abstract class CachedStreamTestBase extends Assert {
             if (len <= rn) {
                 break;
             }
+=======
+        try (StringWriter writer = new StringWriter()) {
+            IOUtils.copyAtLeast(is, writer, len);
+            return writer.toString();
+>>>>>>> 17f140e... Use IOUtils methods in CachedStreamTestBase to handle
stream reading
         }
-        return buf.toString();
     }
     
     private static String initTestData(int packetSize) {


Mime
View raw message