88 lines
1.7 KiB
Java
88 lines
1.7 KiB
Java
package speiger.src.coreengine.assets.impl;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.Closeable;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import speiger.src.coreengine.assets.AssetLocation;
|
|
import speiger.src.coreengine.assets.IAsset;
|
|
import speiger.src.coreengine.utils.helpers.JsonUtil;
|
|
|
|
public class FileAsset implements IAsset
|
|
{
|
|
File file;
|
|
AssetLocation location;
|
|
List<Closeable> toClose = new ArrayList<Closeable>();
|
|
|
|
public FileAsset(File file, AssetLocation location)
|
|
{
|
|
this.file = file;
|
|
this.location = location;
|
|
}
|
|
|
|
@Override
|
|
public void close() throws IOException
|
|
{
|
|
for(Closeable close : toClose)
|
|
{
|
|
try
|
|
{
|
|
close.close();
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
toClose.clear();
|
|
}
|
|
|
|
@Override
|
|
public AssetLocation getLocation()
|
|
{
|
|
return location;
|
|
}
|
|
|
|
@Override
|
|
public InputStream getStream() throws IOException
|
|
{
|
|
return new BufferedInputStream(markClosed(new FileInputStream(file)));
|
|
}
|
|
|
|
@Override
|
|
public BufferedReader getStringReader() throws IOException
|
|
{
|
|
return new BufferedReader(markClosed(new FileReader(file)));
|
|
}
|
|
|
|
@Override
|
|
public BufferedImage getTexture() throws Exception
|
|
{
|
|
return ImageIO.read(file);
|
|
}
|
|
|
|
@Override
|
|
public JsonObject getJsonObject() throws IOException
|
|
{
|
|
return JsonUtil.loadFile(file);
|
|
}
|
|
|
|
protected <T extends Closeable> T markClosed(T value)
|
|
{
|
|
toClose.add(value);
|
|
return value;
|
|
}
|
|
}
|