You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2015/07/17 17:25:40 UTC
cxf git commit: Use IOUtils methods in CachedStreamTestBase to handle
stream reading
Repository: cxf
Updated Branches:
refs/heads/master 20539c027 -> 17f140eb2
Use IOUtils methods in CachedStreamTestBase to handle stream reading
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/17f140eb
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/17f140eb
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/17f140eb
Branch: refs/heads/master
Commit: 17f140eb2bf93444e7f8e028b45a371d14688cda
Parents: 20539c0
Author: Colm O hEigeartaigh <co...@apache.org>
Authored: Fri Jul 17 16:25:07 2015 +0100
Committer: Colm O hEigeartaigh <co...@apache.org>
Committed: Fri Jul 17 16:25:34 2015 +0100
----------------------------------------------------------------------
.../java/org/apache/cxf/helpers/IOUtils.java | 23 +++++-
.../org/apache/cxf/io/CachedStreamTestBase.java | 86 +++++---------------
2 files changed, 40 insertions(+), 69 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf/blob/17f140eb/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 3aff6d5..82c5535 100644
--- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -228,13 +228,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/17f140eb/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 c62ad71..8daa350 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,85 +245,37 @@ 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 {
- StringBuilder buf = new StringBuilder();
- try {
- char[] b = new char[100];
- for (;;) {
- int n = is.read(b, 0, b.length);
- if (n < 0) {
- break;
- }
- buf.append(b, 0, n);
- }
- } finally {
- is.close();
+ try (StringWriter writer = new StringWriter()) {
+ IOUtils.copyAndCloseInput(is, writer);
+ return writer.toString();
}
- return buf.toString();
}
protected static String readPartiallyFromReader(Reader is, int len) throws IOException {
- StringBuilder buf = new StringBuilder();
- char[] b = new char[len];
- int rn = 0;
- for (;;) {
- int n = is.read(b, 0, b.length);
- if (n < 0) {
- break;
- }
- buf.append(b, 0, n);
- rn += n;
- if (len <= rn) {
- break;
- }
+ try (StringWriter writer = new StringWriter()) {
+ IOUtils.copyAtLeast(is, writer, len);
+ return writer.toString();
}
- return buf.toString();
}
private static String initTestData(int packetSize) {