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.
18 package org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util;
20 import java.io.BufferedInputStream;
21 import java.io.ByteArrayOutputStream;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class CommonUtil {
32 private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
34 public static String getJsonStrFromFile(String filePath) throws IOException {
35 String fileName = getAppRoot() + filePath;
36 String fileContent = getJsonStrFromFilePath(fileName);
40 public static String getJsonStrFromFilePath(String fileName) throws IOException {
41 InputStream ins = null;
42 BufferedInputStream bins = null;
43 String fileContent = "";
46 ins = new FileInputStream(fileName);
47 bins = new BufferedInputStream(ins);
49 byte[] contentByte = new byte[ins.available()];
50 int num = bins.read(contentByte);
53 fileContent = new String(contentByte);
55 } catch(FileNotFoundException e) {
56 logger.error(fileName + " is not found!", e);
68 public static String getAppRoot() {
69 String appRoot = System.getProperty("catalina.base");
71 appRoot = getCanonicalPath(appRoot);
76 private static String getCanonicalPath(final String inPath) {
80 final File file = new File(inPath);
81 path = file.getCanonicalPath();
83 } catch(final IOException e) {
84 logger.error("file.getCanonicalPath() IOException:", e);
89 public static byte[] getBytes(String filePath){
92 File file = new File(filePath);
93 FileInputStream fis = new FileInputStream(file);
94 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
95 byte[] b = new byte[1000];
97 while ((n = fis.read(b)) != -1) {
102 buffer = bos.toByteArray();
103 } catch (FileNotFoundException e) {
104 logger.error("file " + filePath + " is not found.", e);
105 } catch (IOException e) {
106 logger.error("file " + filePath + " IOException.", e);