2ebb44a64428f0d4bdc8b55b144129e2657c83b0
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / mock / FileUtil.java
1 /*
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Licensed under the Apache License, Version 2.0 (the "License"); 
6  * you may not use this file except in compliance with the License. 
7  * You may obtain a copy of the License at 
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0 
10  * 
11  * Unless required by applicable law or agreed to in writing, software 
12  * distributed under the License is distributed on an "AS IS" BASIS, 
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  * See the License for the specific language governing permissions and 
15  * limitations under the License. 
16  * ============LICENSE_END========================================================= 
17  */ 
18
19 package org.openecomp.mso.bpmn.mock;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import org.openecomp.mso.logger.MsoLogger;
24
25 /**
26  * 
27  * File utility class.<br/>
28  * <p>
29  * </p>
30  * 
31  * @author
32  * @version     ONAP  Sep 15, 2017
33  */
34 public class FileUtil {
35
36     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
37     
38     private FileUtil() {
39         /**
40          * Constructor.
41          */
42     }
43         /**
44          * Read the specified resource file and return the contents as a String.
45          * 
46          * @param fileName Name of the resource file
47          * @return the contents of the resource file as a String
48          * @throws IOException if there is a problem reading the file
49          */
50         public static String readResourceFile(String fileName) {
51                 InputStream stream;
52                 try {
53                         stream = getResourceAsStream(fileName);
54                         byte[] bytes;
55                         bytes = new byte[stream.available()];
56                         stream.read(bytes);
57                         stream.close();
58                         return new String(bytes);
59                 } catch (IOException e) {
60                     LOGGER.debug("Exception:", e);
61                         return "";
62                 }
63         }
64         
65         /**
66          * Get an InputStream for the resource specified.
67          * 
68          * @param resourceName Name of resource for which to get InputStream.
69          * @return an InputStream for the resource specified.
70          * @throws IOException If we can't get the InputStream for whatever reason.
71          */
72         private static InputStream getResourceAsStream(String resourceName) throws IOException {
73                 InputStream stream =
74                                 FileUtil.class.getClassLoader().getResourceAsStream(resourceName);
75                 if (stream == null) {
76                         throw new IOException("Can't access resource '" + resourceName + "'");
77                 }
78                 return stream;
79         }               
80 }