co.marcin.novaguilds.util.IOUtils.java Source code

Java tutorial

Introduction

Here is the source code for co.marcin.novaguilds.util.IOUtils.java

Source

/*
 *     NovaGuilds - Bukkit plugin
 *     Copyright (C) 2016 Marcin (CTRL) Wieczorek
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

package co.marcin.novaguilds.util;

import com.google.common.io.CharStreams;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public final class IOUtils {
    private IOUtils() {
    }

    public static String inputStreamToString(InputStream inputStream) throws IOException {
        return CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
    }

    public static void saveInputStreamToFile(InputStream inputStream, File file) throws IOException {
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);

            int read;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public static List<String> getFilesWithoutExtension(File directory) {
        List<String> list = new ArrayList<>();
        File[] filesList = directory.listFiles();

        if (filesList != null) {
            for (File file : filesList) {
                if (file.isFile()) {
                    String name = file.getName();
                    if (org.apache.commons.lang.StringUtils.contains(name, '.')) {
                        name = org.apache.commons.lang.StringUtils.split(name, '.')[0];
                        list.add(name);
                    }
                }
            }
        }

        return list;
    }

    public static String toString(InputStream in, String encoding) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buf = new byte[8192];
        int len;

        while ((len = in.read(buf)) != -1) {
            byteArrayOutputStream.write(buf, 0, len);
        }

        return new String(byteArrayOutputStream.toByteArray(), encoding);
    }

    public static String read(File file) throws IOException {
        return inputStreamToString(new FileInputStream(file));
    }

    public static void write(File file, String string) {
        try (PrintWriter out = new PrintWriter(file)) {
            out.println(string);
        } catch (FileNotFoundException e) {
            LoggerUtils.exception(e);
        }
    }
}