2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util;
19 import java.io.BufferedInputStream;
20 import java.io.ByteArrayOutputStream;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class CommonUtil {
31 private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
33 public static String getJsonStrFromFile(String filePath) throws IOException {
34 InputStream ins = null;
35 BufferedInputStream bins = null;
36 String fileContent = "";
37 String fileName = getAppRoot() + filePath;
40 ins = new FileInputStream(fileName);
41 bins = new BufferedInputStream(ins);
43 byte[] contentByte = new byte[ins.available()];
44 int num = bins.read(contentByte);
47 fileContent = new String(contentByte);
49 } catch(FileNotFoundException e) {
50 logger.error(fileName + " is not found!", e);
62 public static String getAppRoot() {
63 String appRoot = System.getProperty("catalina.base");
65 appRoot = getCanonicalPath(appRoot);
70 private static String getCanonicalPath(final String inPath) {
74 final File file = new File(inPath);
75 path = file.getCanonicalPath();
77 } catch(final IOException e) {
78 logger.error("file.getCanonicalPath() IOException:", e);
83 public static byte[] getBytes(String filePath){
86 File file = new File(filePath);
87 FileInputStream fis = new FileInputStream(file);
88 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
89 byte[] b = new byte[1000];
91 while ((n = fis.read(b)) != -1) {
96 buffer = bos.toByteArray();
97 } catch (FileNotFoundException e) {
98 logger.error("file " + filePath + " is not found.", e);
99 } catch (IOException e) {
100 logger.error("file " + filePath + " IOException.", e);