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 16:44:13 UTC

[cxf] branch 3.3.x-fixes 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 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.3.x-fixes by this push:
     new 6bc2c82  CXF-8610:Add null check to IOUtils to avoid null CCL (#867)
6bc2c82 is described below

commit 6bc2c823e6f190dd2907069ae8f063fab098ec2e
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 efa8ded..170a7ec 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;
@@ -176,6 +177,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;
@@ -208,6 +211,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);
@@ -228,6 +233,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);
@@ -248,6 +255,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) {
@@ -257,6 +266,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 {
@@ -279,8 +290,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;
@@ -294,7 +304,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) {
@@ -324,6 +334,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;
@@ -335,6 +346,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
@@ -366,6 +378,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);
@@ -383,6 +396,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