acromusashi.kafka.log.producer.util.YamlReadUtil.java Source code

Java tutorial

Introduction

Here is the source code for acromusashi.kafka.log.producer.util.YamlReadUtil.java

Source

/**
* Copyright (c) Acroquest Technology Co, Ltd. All Rights Reserved.
* Please read the associated COPYRIGHTS file for more details.
*
* THE SOFTWARE IS PROVIDED BY Acroquest Technolog Co., Ltd.,
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDER BE LIABLE FOR ANY
* CLAIM, DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package acromusashi.kafka.log.producer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.yaml.snakeyaml.Yaml;

/**
 * Yaml??
 * 
 * @author kimura
 */
public class YamlReadUtil {
    /**
     * ????
     */
    private YamlReadUtil() {
    }

    /**
     * ???Yaml????Yaml?
     * 
     * @param filePath 
     * @return ???
     * @throws IOException 
     */
    @SuppressWarnings({ "unchecked" })
    public static Map<String, Object> readYaml(String filePath) throws IOException {
        Map<String, Object> configObject = null;
        Yaml yaml = new Yaml();

        InputStream inputStream = null;
        InputStreamReader steamReader = null;
        File file = new File(filePath);

        try {
            inputStream = new FileInputStream(file.getAbsolutePath());
            steamReader = new InputStreamReader(inputStream, "UTF-8");
            configObject = (Map<String, Object>) yaml.load(steamReader);
        } catch (Exception ex) {
            // ScannerException/IOException?????
            // 2???????????????Exception?????
            throw new IOException(ex);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        return configObject;
    }
}