You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/10/27 14:29:33 UTC
[cxf] branch master updated: CXF-8610:Add null check to IOUtils to
avoid null CCL (#867)
This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/master by this push:
new cab0b68 CXF-8610:Add null check to IOUtils to avoid null CCL (#867)
cab0b68 is described below
commit cab0b68e62d44cb670780dcc00e5bc0e1404214e
Author: ppanda2021 <93...@users.noreply.github.com>
AuthorDate: Wed Oct 27 22:29:27 2021 +0800
CXF-8610:Add null check to IOUtils to avoid null CCL (#867)
* CXF-8610:Add null check to IOUtils to avoid setting null CCL
* CXF-8610: use Objects::requireNonNull from the standard library.
Co-authored-by: z00453111 <zo...@huawei.com>
---
.../main/java/org/apache/cxf/helpers/IOUtils.java | 20 +++++++++++++++++---
.../java/org/apache/cxf/helpers/IOUtilsTest.java | 17 +++++++++++++++++
2 files changed, 34 insertions(+), 3 deletions(-)
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 74af28d..be05fe9 100644
--- a/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
+++ b/core/src/main/java/org/apache/cxf/helpers/IOUtils.java
@@ -32,6 +32,7 @@ import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
+import java.util.Objects;
import org.apache.cxf.io.CopyingOutputStream;
import org.apache.cxf.io.Transferable;
@@ -208,6 +209,8 @@ public final class IOUtils {
public static int copy(final InputStream input, final OutputStream output,
int bufferSize) throws IOException {
+ Objects.requireNonNull(input, "The inputStream is required but null value was provided");
+ Objects.requireNonNull(output, "The outputStream is required but null value was provided");
int avail = input.available();
if (avail > 262144) {
avail = 262144;
@@ -240,6 +243,8 @@ public final class IOUtils {
public static void copyAtLeast(final InputStream input,
final OutputStream output,
int atLeast) throws IOException {
+ Objects.requireNonNull(input, "The inputStream is required but null value was provided");
+ Objects.requireNonNull(output, "The outputStream is required but null value was provided");
final byte[] buffer = new byte[4096];
int n = atLeast > buffer.length ? buffer.length : atLeast;
n = input.read(buffer, 0, n);
@@ -260,6 +265,8 @@ public final class IOUtils {
public static void copyAtLeast(final Reader input,
final Writer output,
int atLeast) throws IOException {
+ Objects.requireNonNull(input, "The reader is required but null value was provided");
+ Objects.requireNonNull(output, "The writer is required but null value was provided");
final char[] buffer = new char[4096];
int n = atLeast > buffer.length ? buffer.length : atLeast;
n = input.read(buffer, 0, n);
@@ -280,6 +287,8 @@ public final class IOUtils {
public static void copy(final Reader input, final Writer output,
final int bufferSize) throws IOException {
+ Objects.requireNonNull(input, "The reader is required but null value was provided");
+ Objects.requireNonNull(output, "The writer is required but null value was provided");
final char[] buffer = new char[bufferSize];
int n = input.read(buffer);
while (-1 != n) {
@@ -289,6 +298,8 @@ public final class IOUtils {
}
public static void transferTo(InputStream inputStream, File destinationFile) throws IOException {
+ Objects.requireNonNull(inputStream, "The inputStream is required but null value was provided");
+ Objects.requireNonNull(destinationFile, "The destinationFile is required but null value was provided");
if (Transferable.class.isAssignableFrom(inputStream.getClass())) {
((Transferable)inputStream).transferTo(destinationFile);
} else {
@@ -311,8 +322,7 @@ public final class IOUtils {
}
public static String toString(final InputStream input, int bufferSize, String charset)
throws IOException {
-
-
+ Objects.requireNonNull(input, "The inputStream is required but null value was provided");
int avail = input.available();
if (avail > bufferSize) {
bufferSize = avail;
@@ -326,7 +336,7 @@ public final class IOUtils {
return toString(input, DEFAULT_BUFFER_SIZE);
}
public static String toString(final Reader input, int bufSize) throws IOException {
-
+ Objects.requireNonNull(input, "The reader is required but null value was provided");
StringBuilder buf = new StringBuilder();
final char[] buffer = new char[bufSize];
try (Reader r = input) {
@@ -356,6 +366,7 @@ public final class IOUtils {
*/
public static ByteArrayInputStream loadIntoBAIS(InputStream in)
throws IOException {
+ Objects.requireNonNull(in, "The inputStream is required but null value was provided");
int i = in.available();
if (i < DEFAULT_BUFFER_SIZE) {
i = DEFAULT_BUFFER_SIZE;
@@ -367,6 +378,7 @@ public final class IOUtils {
}
public static void consume(InputStream in) throws IOException {
+ Objects.requireNonNull(in, "The inputStream is required but null value was provided");
int i = in.available();
if (i == 0) {
//if i is 0, then we MAY have already hit the end of the stream
@@ -398,6 +410,7 @@ public final class IOUtils {
*/
public static void consume(final InputStream input,
int atLeast) throws IOException {
+ Objects.requireNonNull(input, "The inputStream is required but null value was provided");
final byte[] buffer = new byte[4096];
int n = atLeast > buffer.length ? buffer.length : atLeast;
n = input.read(buffer, 0, n);
@@ -415,6 +428,7 @@ public final class IOUtils {
}
public static byte[] readBytesFromStream(InputStream in) throws IOException {
+ Objects.requireNonNull(in, "The inputStream is required but null value was provided");
int i = in.available();
if (i < DEFAULT_BUFFER_SIZE) {
i = DEFAULT_BUFFER_SIZE;
diff --git a/core/src/test/java/org/apache/cxf/helpers/IOUtilsTest.java b/core/src/test/java/org/apache/cxf/helpers/IOUtilsTest.java
index ccd965d..2bc3859 100644
--- a/core/src/test/java/org/apache/cxf/helpers/IOUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/helpers/IOUtilsTest.java
@@ -21,6 +21,7 @@ package org.apache.cxf.helpers;
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.io.InputStream;
import java.io.PushbackInputStream;
@@ -28,6 +29,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class IOUtilsTest {
@@ -57,4 +59,19 @@ public class IOUtilsTest {
assertFalse(IOUtils.isEmpty(is));
assertEquals(data, IOUtils.toString(is));
}
+
+ /**
+ * add a new test when destinationFile is null
+ */
+ @Test
+ public void testTransferToWhenEmptyFile() throws Exception {
+ InputStream is = new ByteArrayInputStream("Hello".getBytes());
+ File destinationFile = null;
+ Exception exception = assertThrows(Exception.class, () -> {
+ IOUtils.transferTo(is, destinationFile);
+ });
+ String expectedMessage = "The destinationFile is required";
+ String actualMessage = exception.getMessage();
+ assertTrue(actualMessage.contains(expectedMessage));
+ }
}
\ No newline at end of file