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

net.minecraftforge.gradle.PredefInputSupplier Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package net.minecraftforge.gradle;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.minecraftforge.srg2source.util.io.InputSupplier;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class PredefInputSupplier implements InputSupplier
{
    private final Map fileMap = Maps.newHashMap();
    private final Map rootMap = Maps.newHashMap();

    @Override
    public void close() throws IOException
    {
        // uh.. no?
    }

    @Override
    public String getRoot(String resource)
    {
        return rootMap.get(sanitize(resource));
    }

    @Override
    public InputStream getInput(String relPath)
    {
        return new ByteArrayInputStream(fileMap.get(sanitize(relPath)));
    }

    @Override
    public List gatherAll(String endFilter)
    {
        LinkedList out = Lists.newLinkedList();
        for (String s : fileMap.keySet())
        {
            if (s.endsWith(endFilter))
            {
                out.add(s);
            }
        }

        return out;
    }

    public void addFile(String path, File root, byte[] data) throws IOException
    {
        path = sanitize(path);
        fileMap.put(path, data);
        rootMap.put(path, sanitize(root.getCanonicalPath()));
    }

    private String sanitize(String in)
    {
        if (in == null)
        {
            return null;
        }
        
        in = in.replace('\\', '/');

        if (in.endsWith("/"))
            in = in.substring(0, in.length() - 1);

        return in;
    }

    public boolean isEmpty()
    {
        return fileMap.isEmpty() && rootMap.isEmpty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy