diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java | 68 |
1 files changed, 46 insertions, 22 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java index 01af7fd8..7701eae9 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/resources/TextFileUtils.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +26,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and write @@ -33,13 +40,9 @@ import java.nio.file.Files; * * @author Liam Fallon (liam.fallon@est.tech) */ -public abstract class TextFileUtils { - private static final String UTF_8 = "UTF-8"; - private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096; - - private TextFileUtils() { - // This class cannot be initialized - } +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TextFileUtils { + private static final Logger logger = LoggerFactory.getLogger(TextFileUtils.class); /** * Method to return the contents of a text file as a string. @@ -49,8 +52,8 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getTextFileAsString(final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); - return new String(Files.readAllBytes(textFile.toPath()), UTF_8); + final var textFile = new File(textFilePath); + return Files.readString(textFile.toPath()); } /** @@ -61,7 +64,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); + final var textFile = new File(textFilePath); if (!textFile.getParentFile().exists()) { textFile.getParentFile().mkdirs(); } @@ -77,7 +80,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsFile(final String outString, final File textFile) throws IOException { - Files.write(textFile.toPath(), outString.getBytes(UTF_8)); + Files.writeString(textFile.toPath(), outString); } /** @@ -88,7 +91,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getStreamAsString(final InputStream textStream) throws IOException { - return getReaderAsString(new InputStreamReader(textStream, UTF_8)); + return getReaderAsString(new InputStreamReader(textStream, StandardCharsets.UTF_8)); } /** @@ -99,16 +102,37 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getReaderAsString(final Reader textReader) throws IOException { - final StringBuilder builder = new StringBuilder(); - int charsRead = -1; - final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096]; - do { - charsRead = textReader.read(chars); - if (charsRead > 0) { - builder.append(chars, 0, charsRead); - } + return IOUtils.toString(textReader); + } + + /** + * Creates a temporary file, only accessible by the owner. + * + * @param prefix file name prefix + * @param suffix file name suffix + * @return a new, temporary file + * @throws IOException if an error occurs + */ + public static File createTempFile(String prefix, String suffix) throws IOException { + /* + * Disabling sonar, because setDefaultPermissions() will set the permissions of + * the file. + */ + var file = File.createTempFile(prefix, suffix); // NOSONAR + + setDefaultPermissions(file); + + return file; + } + + /** + * Sets permissions on a file or directory so that only the owner can access it. + * + * @param file file or directory on which permissions are to be set + */ + public static void setDefaultPermissions(File file) { + if (!file.setReadable(true, true) || !file.setWritable(true, true) || !file.setExecutable(true, true)) { + logger.warn("cannot set permissions for {}", file); } - while (charsRead > 0); - return builder.toString(); } } |