Skip to content

Instantly share code, notes, and snippets.

@doriancransac
Created December 3, 2020 17:36
Show Gist options
  • Save doriancransac/5b83d5f70e6c039fe2933e27048b16fc to your computer and use it in GitHub Desktop.
Save doriancransac/5b83d5f70e6c039fe2933e27048b16fc to your computer and use it in GitHub Desktop.
An extension version of FileHandle for Java 9 compatibility in Scene2d's Json Skin loader
package com.projectvisionaries.assets.loader;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.projectvisionaries.assets.binary.InitModule;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
public class ClasspathFileHandle extends FileHandle {
private String filepath;
private String name;
public ClasspathFileHandle(String filepath, String name) {
super(filepath + "/" + name, Files.FileType.Classpath);
this.filepath = filepath;
this.name = name;
}
@Override
public InputStream read () {
String path = file.getPath().replace("\\", "/");
InputStream input = this.getClass().getClassLoader().getResourceAsStream(path);
return input;
}
@Override
public FileHandle child (String name) {
return new ClasspathFileHandle(this.filepath + "/" + this.name, name);
}
@Override public FileHandle sibling (String name) {
return new ClasspathFileHandle(this.filepath, name);
}
@Override
public FileHandle parent () {
String newFilePath;
if(this.filepath.endsWith("/")){
newFilePath = this.filepath.substring(1, this.filepath.length());
}else{
newFilePath = this.filepath;
}
return new ClasspathFileHandle(newFilePath, "");
}
@Override
public boolean exists () {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment