mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Moved isLoaded to BaseResource. Loading and unloading will still be overridden if necessary. Added some subclass/super safe load and unload functions.
This commit is contained in:
@@ -16,33 +16,38 @@ public abstract class BaseResource {
|
||||
return this.isLoaded;
|
||||
}
|
||||
|
||||
public void load() throws FileNotFoundException{
|
||||
public void load() throws FileNotFoundException {
|
||||
this.loadRawData();
|
||||
isLoaded = true;
|
||||
}
|
||||
|
||||
private void loadRawData() throws FileNotFoundException{
|
||||
try {
|
||||
this.rawData = Files.readAllBytes(file.toPath());
|
||||
isLoaded = true;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
public void unload(){
|
||||
this.unloadRawData();
|
||||
isLoaded = false;
|
||||
}
|
||||
|
||||
private void unloadRawData() {
|
||||
this.rawData = null;
|
||||
this.isLoaded = false;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
public InputStream getInputStream() throws FileNotFoundException {
|
||||
if (!isLoaded){
|
||||
try {
|
||||
this.load();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// Manually load the data, makes sure it doesn't call subclass load()
|
||||
loadRawData();
|
||||
isLoaded = true;
|
||||
}
|
||||
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import java.io.FileNotFoundException;
|
||||
|
||||
public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
private Image image;
|
||||
private boolean isLoaded = false;
|
||||
|
||||
public ImageAsset(final File file) throws FileNotFoundException {
|
||||
super(file);
|
||||
@@ -14,25 +13,20 @@ public class ImageAsset extends BaseResource implements LoadableResource {
|
||||
|
||||
@Override
|
||||
public void load() throws FileNotFoundException {
|
||||
if (!this.isLoaded) {
|
||||
if (!this.isLoaded()) {
|
||||
super.load(); // Make sure that base class (byte[]) is loaded
|
||||
this.image = new Image(this.getInputStream());
|
||||
this.isLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.image = null;
|
||||
this.isLoaded = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoaded() {
|
||||
return this.isLoaded;
|
||||
super.unload();
|
||||
}
|
||||
|
||||
public Image getImage() {
|
||||
if (!this.isLoaded) try {
|
||||
if (!this.isLoaded()) try {
|
||||
load();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
Reference in New Issue
Block a user