12231c9ee528903efe966bcf9ae364467f540443
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / FileUtil.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.openecomp.mso.bpmn.mock;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 public class FileUtil {
27
28         /**
29          * Read the specified resource file and return the contents as a String.
30          * 
31          * @param fileName Name of the resource file
32          * @return the contents of the resource file as a String
33          * @throws IOException if there is a problem reading the file
34          */
35         public static String readResourceFile(String fileName) {
36                 InputStream stream;
37                 try {
38                         stream = getResourceAsStream(fileName);
39                         byte[] bytes;
40                         bytes = new byte[stream.available()];
41                         stream.read(bytes);
42                         stream.close();
43                         return new String(bytes);
44                 } catch (IOException e) {
45                         return "";
46                 }
47         }
48         
49         /**
50          * Get an InputStream for the resource specified.
51          * 
52          * @param resourceName Name of resource for which to get InputStream.
53          * @return an InputStream for the resource specified.
54          * @throws IOException If we can't get the InputStream for whatever reason.
55          */
56         private static InputStream getResourceAsStream(String resourceName) throws IOException {
57                 InputStream stream =
58                                 FileUtil.class.getClassLoader().getResourceAsStream(resourceName);
59                 if (stream == null) {
60                         throw new IOException("Can't access resource '" + resourceName + "'");
61                 }
62                 return stream;
63         }               
64 }