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