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

com.iohao.game.common.kit.adapter.HuFileResource Maven / Gradle / Ivy

There is a newer version: 21.20
Show newest version
package com.iohao.game.common.kit.adapter;


import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author 渔民小镇
 * @date 2023-01-19
 */
class HuFileResource implements HuResource, Serializable {

    private final File file;
    private final String name;

    public HuFileResource(String path) {
        this(HuFileUtil.file(path));
    }

    public HuFileResource(File file) {
        this(file, null);
    }

    public HuFileResource(File file, String fileName) {
        Objects.requireNonNull(file);
        Objects.requireNonNull(file);
        this.file = file;
        this.name = defaultIfNull(fileName, file::getName);
    }

    private static  T defaultIfNull(T source, Supplier defaultValueSupplier) {
        if (Objects.isNull(source)) {
            return defaultValueSupplier.get();
        }
        return source;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public URL getUrl() {
        return HuUrlUtil.getURL(this.file);
    }

    @Override
    public InputStream getStream() throws HuNoResourceException {
        return HuFileUtil.getInputStream(this.file);
    }

    public File getFile() {
        return this.file;
    }

    @Override
    public String toString() {
        return this.file.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy