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 String fileContent = "";
43 try (InputStream ins = new FileInputStream(fileName);
44 BufferedInputStream bins = new BufferedInputStream(ins)){
46 byte[] contentByte = new byte[ins.available()];
47 int num = bins.read(contentByte);
50 fileContent = new String(contentByte);
52 } catch(FileNotFoundException e) {
53 logger.error(fileName + " is not found!", e);
58 public static String getAppRoot() {
59 String appRoot = System.getProperty("catalina.base");
61 appRoot = getCanonicalPath(appRoot);
66 private static String getCanonicalPath(final String inPath) {
70 final File file = new File(inPath);
71 path = file.getCanonicalPath();
73 } catch(final IOException e) {
74 logger.error("file.getCanonicalPath() IOException:", e);
79 public static byte[] getBytes(String filePath){
81 File file = new File(filePath);
82 try(FileInputStream fis = new FileInputStream(file);
83 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000)){
84 byte[] b = new byte[1000];
86 while ((n = fis.read(b)) != -1) {
89 buffer = bos.toByteArray();
90 } catch (FileNotFoundException e) {
91 logger.error("file " + filePath + " is not found.", e);
92 } catch (IOException e) {
93 logger.error("file " + filePath + " IOException.", e);