com.refreshsf.contrib.client.tests.ResourceUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.refreshsf.contrib.client.tests.ResourceUtil.java

Source

/*
 * refresh-sf client
 *
 * Copyright 2015 dherges <david@spektrakel.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.refreshsf.contrib.client.tests;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public final class ResourceUtil {

    private ResourceUtil() {
    }

    public static InputStream getResourceInputStream(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    }

    public static URL getResource(String fileName) {
        return Thread.currentThread().getContextClassLoader().getResource(fileName);
    }

    public static String readResourceFile(String fileName, String encoding) throws IOException {
        return IOUtils.toString(getResourceInputStream(fileName), encoding);
    }

    public static String readResourceFile(String fileName) throws IOException {
        return readResourceFile(fileName, "UTF-8");
    }
}