Run apex-pdp in Java 11: base changes
[policy/apex-pdp.git] / model / utilities / src / main / java / org / onap / policy / apex / model / utilities / TextFileUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.utilities;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.nio.file.Files;
30
31 /**
32  * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and wrtie
33  * text files to strings and strings are provided.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public abstract class TextFileUtils {
38     private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096;
39
40     private TextFileUtils() {
41         // This class cannot be initialized
42     }
43
44     /**
45      * Method to return the contents of a text file as a string.
46      *
47      * @param textFilePath The path to the file as a string
48      * @return A string containing the contents of the file
49      * @throws IOException on errors reading text from the file
50      */
51     public static String getTextFileAsString(final String textFilePath) throws IOException {
52         final File textFile = new File(textFilePath);
53         return new String(Files.readAllBytes(textFile.toPath()));
54     }
55
56     /**
57      * Method to write contents of a string to a text file.
58      *
59      * @param outString The string to write
60      * @param textFilePath The path to the file as a string
61      * @throws IOException on errors reading text from the file
62      */
63     public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException {
64         final File textFile = new File(textFilePath);
65         if (!textFile.getParentFile().exists()) {
66             textFile.getParentFile().mkdirs();
67         }
68
69         putStringAsFile(outString, textFile);
70     }
71
72     /**
73      * Method to write contents of a string to a text file.
74      *
75      * @param outString The string to write
76      * @param textFile The file to write the string to
77      * @throws IOException on errors reading text from the file
78      */
79     public static void putStringAsFile(final String outString, final File textFile) throws IOException {
80         Files.write(textFile.toPath(), outString.getBytes());
81     }
82
83     /**
84      * Method to return the contents of a text steam as a string.
85      *
86      * @param textStream The stream
87      * @return A string containing the output of the stream as text
88      * @throws IOException on errors reading text from the file
89      */
90     public static String getStreamAsString(final InputStream textStream) throws IOException {
91         return getReaderAsString(new BufferedReader(new InputStreamReader(textStream)));
92     }
93
94     /**
95      * Method to return the contents of a reader steam as a string. This closes the reader after use
96      *
97      * @param textReader The reader
98      * @return A string containing the output of the reader as text
99      * @throws IOException on errors reading text from the file
100      */
101     public static String getReaderAsString(final BufferedReader textReader) throws IOException {
102
103         final StringBuilder builder = new StringBuilder();
104         int charsRead = -1;
105         final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096];
106         do {
107             charsRead = textReader.read(chars, 0, chars.length);
108             if (charsRead > 0) {
109                 builder.append(chars, 0, charsRead);
110             }
111         }
112         while (charsRead > 0);
113         return builder.toString();
114     }
115 }