SimpleJavaEngine/src/main/java/speiger/src/coreengine/assets/impl/ZipAsset.java

76 lines
1.5 KiB
Java

package speiger.src.coreengine.assets.impl;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
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 ZipAsset implements IAsset
{
ZipFile file;
ZipEntry entry;
AssetLocation location;
Runnable runnable;
public ZipAsset(ZipFile file, ZipEntry entry, AssetLocation location, Runnable runnable)
{
this.file = file;
this.entry = entry;
this.location = location;
this.runnable = runnable;
}
@Override
public void close() throws IOException
{
if(runnable != null)
{
runnable.run();
runnable = null;
}
}
@Override
public AssetLocation getLocation()
{
return location;
}
@Override
public InputStream getStream() throws IOException
{
return new BufferedInputStream(file.getInputStream(entry));
}
@Override
public BufferedImage getTexture() throws Exception
{
return ImageIO.read(file.getInputStream(entry));
}
@Override
public BufferedReader getStringReader() throws IOException
{
return new BufferedReader(new InputStreamReader(file.getInputStream(entry)));
}
@Override
public JsonObject getJsonObject() throws IOException
{
return JsonUtil.loadFile(file.getInputStream(entry));
}
}