diff options
author | Jim Hahn <jrh3@att.com> | 2021-09-02 12:18:49 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-09-02 17:55:50 -0400 |
commit | 3495931d999bd8022724c6fea0166baf01b5e113 (patch) | |
tree | c0385152a6f3c23d3b84824123cce6cf36ccff6b /utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java | |
parent | 32ab41fa6a6998ff88cd9dbef31f7c65f39ee0e1 (diff) |
Create methods for creating temp files
Created utility methods to create temp files and set default
permissions. These can be used in other repos to satisfy sonar.
Also added code to close streams and clean up temp files created
by some junit tests.
Modified to use IOUtils to read a stream.
Issue-ID: POLICY-3289
Change-Id: Ic83e4f4d7745be7b37ebb42bf1d81e303d8dadc9
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java index 83d41fb3..001c9f06 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/ResourceUtils.java @@ -23,11 +23,11 @@ package org.onap.policy.common.utils.resources; import com.google.re2j.Pattern; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Enumeration; import java.util.Set; @@ -36,6 +36,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,9 +51,6 @@ public final class ResourceUtils { private static final Pattern SLASH_PAT = Pattern.compile("/"); - // The length of byte buffers used to read resources into strings - private static final int BYTE_BUFFER_LENGH = 1024; - // Resource types private static final String FILE_PROTOCOL = "file"; private static final String JAR_PROTOCOL = "jar"; @@ -90,20 +88,13 @@ public final class ResourceUtils { return null; } - // Read the stream contents in to an output stream - final var resourceOutputStreamBuffer = new ByteArrayOutputStream(); - final var resourceBuffer = new byte[BYTE_BUFFER_LENGH]; - int length; - try { - while ((length = resourceStream.read(resourceBuffer)) != -1) { - resourceOutputStreamBuffer.write(resourceBuffer, 0, length); - } + // Read the stream contents, closing when done + try (var streamCloser = resourceStream) { + return IOUtils.toString(resourceStream, StandardCharsets.UTF_8); } catch (final IOException e) { LOGGER.debug("error reading resource stream {}", resourceName, e); return null; } - - return resourceOutputStreamBuffer.toString(); } /** |