package emergencylanding.k.library.lwjgl.tex;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import org.lwjgl.LWJGLUtil;
import org.newdawn.slick.opengl.PNGDecoder;
import org.newdawn.slick.opengl.PNGDecoder.Format;

import emergencylanding.k.library.util.LUtils;

public class InputStreamTexture extends ELTexture {
    private InputStream tex = null;

    private static final String endswith = "^(.+)" + File.separator + "$",
            startswith = "^" + File.separator + "(.+)$";

    public InputStreamTexture(String parentDir, String name) {
        if (parentDir == null) {
            parentDir = System
                    .getProperty(
                            "user.home",
                            (LWJGLUtil.getPlatform() == LWJGLUtil.PLATFORM_WINDOWS ? "C:"
                                    : "")
                                    + File.separator);
        }
        if (name == null) {
            LUtils.print("Creating a null src!");
            return;
        }
        try {
            tex = LUtils
                    .getInputStream(parentDir
                            + ((parentDir.matches(endswith) || name
                                    .matches(startswith)) ? "" : File.separator)
                            + name);
        } catch (IOException e) {
            throw new RuntimeException("Error retriving stream", e);
        }
        super.init();
    }

    @Override
    public void setup() {
        try {
            // Open the PNG file as an InputStream
            InputStream in = tex;
            // Link the PNG decoder to this stream
            PNGDecoder decoder = new PNGDecoder(in);

            // Get the width and height of the texture
            dim = new Dimension(decoder.getWidth(), decoder.getHeight());

            // Decode the PNG file in a ByteBuffer
            buf = ByteBuffer.allocateDirect(4 * decoder.getWidth()
                    * decoder.getHeight());
            decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
            buf.flip();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    @Override
    public boolean isLookAlike(ELTexture t) {
        if (t instanceof InputStreamTexture) {
            return tex.equals(((InputStreamTexture) t).tex);
        } else {
            return super.isLookAlike(t);
        }
    }

    @Override
    protected BufferedImage toBufferedImageAbstract() {
        try {
            return ImageIO.read(tex);
        } catch (IOException e) {
            throw new RuntimeException("Error reading image", e);
        }
    }

    @Override
    protected void onDestruction() {
        try {
            tex.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

@@ -502,16 +502,111 @@
  null;%0A%0A
+    private static final String regexdsep = File.separator%0A            .replace(%22%5C%5C%22, %22%5C%5C%5C%5C%22);%0A
     priv
@@ -646,38 +646,33 @@
 = %22%5E(.+)%22 + 
-File.separator
+regexdsep
  + %22$%22,%0A    
@@ -698,30 +698,25 @@
 = %22%5E%22 + 
-File.separator
+regexdsep
  + %22(.+)

Fix regex