feat: asset manager
This commit is contained in:
38
app/src/main/java/chess/view/AssetManager.java
Normal file
38
app/src/main/java/chess/view/AssetManager.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package chess.view;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AssetManager {
|
||||
|
||||
private static final String gradleBase = "app/src/main/resources/";
|
||||
|
||||
public static InputStream getResource(String name) {
|
||||
// we first search it in files
|
||||
InputStream inputStream = getFileInputStream(name);
|
||||
if (inputStream != null)
|
||||
return inputStream;
|
||||
|
||||
inputStream = getFileInputStream(gradleBase + name);
|
||||
if (inputStream != null)
|
||||
return inputStream;
|
||||
// then in the jar
|
||||
return ClassLoader.getSystemResourceAsStream(name);
|
||||
}
|
||||
|
||||
private static InputStream getFileInputStream(String path) {
|
||||
File f = new File(path);
|
||||
if (f.exists()) {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(f);
|
||||
return fis;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user