blob: 63026e69bfb3a66ea9abd3d2d23c99743ed0312f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package io.polyfrost.oneconfig.lwjgl;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
public final class IOUtil {
private IOUtil() {
}
/**
* Taken from legui under MIT License
* <a href="https://github.com/SpinyOwl/legui/blob/develop/LICENSE">https://github.com/SpinyOwl/legui/blob/develop/LICENSE</a>
*/
public static ByteBuffer resourceToByteBuffer(String path) throws IOException {
byte[] bytes;
path = path.trim();
if (path.startsWith("http")) {
bytes = IOUtils.toByteArray(new URL(path));
} else {
InputStream stream;
File file = new File(path);
if (file.exists() && file.isFile()) {
stream = Files.newInputStream(file.toPath());
} else {
stream = IOUtil.class.getResourceAsStream(path);
}
if (stream == null) {
throw new FileNotFoundException(path);
}
bytes = IOUtils.toByteArray(stream);
}
ByteBuffer data = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder())
.put(bytes);
((Buffer) data).flip();
return data;
}
public static ByteBuffer resourceToByteBufferNullable(String path) {
try {
return resourceToByteBuffer(path);
} catch (Exception ignored) {
return null;
}
}
}
|