fixed XPMFile to use InputStream load method too

This commit is contained in:
Brian Matzon 2005-08-18 13:55:10 +00:00
parent 2bf0d9c9cb
commit b2713156b8
2 changed files with 21 additions and 15 deletions

View File

@ -31,9 +31,8 @@
*/
package org.lwjgl.test;
import java.io.File;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
@ -97,9 +96,8 @@ public class WindowCreationTest {
window_x = window_y = 100;
Display.setLocation(window_x, window_y);
File file = new File(new File("res"), "lwjgl_16x16.xpm");
ByteBuffer size16 = BufferUtils.createByteBuffer(16 * 16 * 4);
XPMFile xpm = XPMFile.load(file.getAbsolutePath());
XPMFile xpm = XPMFile.load(WindowCreationTest.class.getClassLoader().getResourceAsStream("lwjgl_16x16.xpm"));
size16.put(xpm.getBytes());
size16.flip();
@ -111,9 +109,8 @@ public class WindowCreationTest {
// }
// size16.flip();
file = new File(new File("res"), "lwjgl_32x32.xpm");
ByteBuffer size32 = BufferUtils.createByteBuffer(32 * 32 * 4);
xpm = XPMFile.load(file.getAbsolutePath());
xpm = XPMFile.load(WindowCreationTest.class.getClassLoader().getResourceAsStream("lwjgl_32x32.xpm"));
size32.put(xpm.getBytes());
size32.flip();

View File

@ -3,9 +3,9 @@ import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
@ -40,14 +40,24 @@ public class XPMFile {
*
* @param file path to file
* @return XPMFile loaded, or exception
* @throws FileNotFoundException If file isn't found
* @throws IOException If any IO exceptions occurs while reading file
*/
public static XPMFile load(String file) throws FileNotFoundException, IOException {
XPMFile xFile = new XPMFile();
xFile.readImage(file);
return xFile;
public static XPMFile load(String file) throws IOException {
return load(new FileInputStream(new File(file)));
}
/**
* Loads the XPM file
*
* @param is InputStream to read file from
* @return XPMFile loaded, or exception
* @throws IOException If any IO exceptions occurs while reading file
*/
public static XPMFile load(InputStream is) throws IOException {
XPMFile xFile = new XPMFile();
xFile.readImage(is);
return xFile;
}
/**
* @return the height of the image.
@ -73,12 +83,11 @@ public class XPMFile {
/**
* Read the image from the specified file.
*
* @throws FileNotFoundException If file isn't found
* @throws IOException If any IO exceptions occurs while reading file
*/
private void readImage(String filename) throws FileNotFoundException, IOException {
private void readImage(InputStream is) throws IOException {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename))));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
HashMap colors = new HashMap();
String comment = br.readLine();