Legger til en ressurslaster

This commit is contained in:
Kristian Knarvik 2020-02-23 23:17:21 +01:00
parent 4a1e974538
commit e0e676e0fb
2 changed files with 22 additions and 6 deletions

View File

@ -27,12 +27,7 @@ public final class BoardLoaderUtil {
* @throws IOException If the board file cannot be loaded
*/
public static Board loadBoard(String boardFile, List<Robot> robotList) throws IOException {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream fileStream = classloader.getResourceAsStream(boardFile);
if (fileStream == null) {
throw new IllegalArgumentException("Board file could not be loaded.");
}
InputStream fileStream = ResourceUtil.getResourceAsInputStream(boardFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream));
String infoLine = reader.readLine();
String[] infoData = infoLine.split(" ");

View File

@ -0,0 +1,21 @@
package inf112.fiasko.roborally.utility;
import java.io.InputStream;
public class ResourceUtil {
private ResourceUtil() {}
/**
* Gets an input stream for a given resource
* @param resourcePath The relative path from the resources folder to the resource
* @return An input stream
*/
public static InputStream getResourceAsInputStream(String resourcePath) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream resourceStream = classloader.getResourceAsStream(resourcePath);
if (resourceStream == null) {
throw new IllegalArgumentException("Unable to load resource.");
}
return resourceStream;
}
}