2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.util;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Scanner;
31 * Utility methods supporting resources accesses.
33 public final class ResourceFileUtils {
36 * getResourceAsStram supports the "file:" prefix as they use URL.
37 * So here we want to eliminate classpath: prefix, so that this class can get
38 * files from jar resource or file system.
41 private static final String CLASSPATH_PREFIX = "classpath:";
44 * Private constructor to avoid creating instances of util class.
46 private ResourceFileUtils() {
50 * Method to access a file from the jar resource folder or file system.
51 * Give the prefix "classpath:" so that it accesses the jar resource folder (default case)
52 * or the prefix "file:" so that it accesses the file system.
54 * @param fileName The path of the resource (no prefix it will be a classpath access,
55 * "classpath:/myfilename" or "file:/myfilename")
56 * @return The file as inputStream
58 public static InputStream getResourceAsStream(String fileName) {
59 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(
60 fileName.startsWith(CLASSPATH_PREFIX) ? fileName.replaceFirst(CLASSPATH_PREFIX, "") : fileName);
62 throw new IllegalArgumentException("Unable to find resource: " + fileName);
68 * Method to access a resource file as a string.
69 * Give the prefix "classpath:" so that it accesses the jar resource folder (default case)
70 * or the prefix "file:" so that it accesses the file system.
72 * @param fileName The path of the resource (no prefix it will be a classpath access,
73 * "classpath:/myfilename" or "file:/myfilename")
74 * @return The file as String
75 * @throws IOException In case of failure to find the file.
77 public static String getResourceAsString(String fileName) throws IOException {
78 try (InputStream is = getResourceAsStream(fileName)) {
79 return streamToString(is);
83 private static String streamToString(InputStream inputStream) {
84 try (Scanner scanner = new Scanner(inputStream)) {
85 Scanner delimitedScanner = scanner.useDelimiter("\\A");
86 return delimitedScanner.hasNext() ? delimitedScanner.next() : "";