From 4970625eaa858c0ab84d04e0306882fb8c8ba743 Mon Sep 17 00:00:00 2001 From: Gary Wu Date: Fri, 14 Apr 2017 13:28:20 -0700 Subject: Replace FileUtils.toByteArray() with IOUtils The in-house implementation of FileUtils.toByteArray() appears to be a literal copy-and-paste of IOUtils.toByteArray(). This change replaces the in-house implementation with a call to IOUtils. Change-Id: Ib33b64c4c0050acb0787915dd9df190e284b8cda Signed-off-by: Gary Wu --- .../openecomp-utilities-lib/pom.xml | 6 +++ .../openecomp/core/utilities/file/FileUtils.java | 44 +++------------------- .../impl/ToscaFileOutputServiceCsarImpl.java | 5 ++- 3 files changed, 15 insertions(+), 40 deletions(-) (limited to 'openecomp-be') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/pom.xml b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/pom.xml index 76d6ff6b74..490bcf742a 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/pom.xml +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/pom.xml @@ -23,6 +23,12 @@ snakeyaml ${snakeyaml.version} + + + commons-io + commons-io + ${commons.io.version} + org.apache.commons commons-lang3 diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java index 08d9bd8a41..87ce8d37c3 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java @@ -20,14 +20,13 @@ package org.openecomp.core.utilities.file; +import org.apache.commons.io.IOUtils; import org.openecomp.core.utilities.json.JsonUtil; import org.openecomp.sdc.tosca.services.YamlUtil; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -179,48 +178,15 @@ public class FileUtils { * @return the byte [ ] */ public static byte[] toByteArray(InputStream input) { - ByteArrayOutputStream output = new ByteArrayOutputStream(); + if (input == null) { + return new byte[0]; + } try { - copy(input, output); + return IOUtils.toByteArray(input); } catch (IOException exception) { throw new RuntimeException( "error will convertion input stream to byte array:" + exception.getMessage()); } - return output.toByteArray(); - } - - /** - * Copy int. - * - * @param input the input - * @param output the output - * @return the int - * @throws IOException the io exception - */ - public static int copy(InputStream input, OutputStream output) throws IOException { - long count = copyLarge(input, output); - return count > 2147483647L ? -1 : (int) count; - } - - private static long copyLarge(InputStream input, OutputStream output) throws IOException { - return copyLarge(input, output, new byte[4096]); - } - - private static long copyLarge(InputStream input, OutputStream output, byte[] buffer) - throws IOException { - long count = 0L; - - int n1; - if (input == null) { - return count; - } - for (; -1 != (n1 = input.read(buffer)); count += (long) n1) { - output.write(buffer, 0, n1); - } - - return count; - - } /** diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java index b518057e35..9a3ed8e73f 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.tosca.services.impl; +import org.apache.commons.io.IOUtils; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.common.errors.CoreException; @@ -176,6 +177,8 @@ public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService { } private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException { - FileUtils.copy(is, zos); + if(is != null){ + IOUtils.copy(is, zos); + } } } -- cgit 1.2.3-korg