All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.scheduler.FileWriteTask Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.scheduler;

import cn.nukkit.utils.Utils;
import lombok.extern.log4j.Log4j2;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author MagicDroidX (Nukkit Project)
 */
@Log4j2
public class FileWriteTask extends AsyncTask {
    private final File file;
    private final InputStream contents;

    public FileWriteTask(String path, String contents) {
        this(new File(path), contents);
    }

    public FileWriteTask(String path, byte[] contents) {
        this(new File(path), contents);
    }

    public FileWriteTask(String path, InputStream contents) {
        this.file = new File(path);
        this.contents = contents;
    }

    public FileWriteTask(File file, String contents) {
        this.file = file;
        this.contents = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
    }

    public FileWriteTask(File file, byte[] contents) {
        this.file = file;
        this.contents = new ByteArrayInputStream(contents);
    }

    public FileWriteTask(File file, InputStream contents) {
        this.file = file;
        this.contents = contents;
    }

    @Override
    public void onRun() {
        try {
            Utils.writeFile(file, contents);
        } catch (IOException e) {
            log.fatal("An error occurred while writing the file {}", file, e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy