-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ResourceUtil.java
202 lines (189 loc) · 7.22 KB
/
ResourceUtil.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2024 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.util;
import com.falsepattern.lib.StableAPI;
import com.falsepattern.lib.internal.FPLog;
import lombok.experimental.UtilityClass;
import lombok.val;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A utility class for reading resources in many ways.
*/
@UtilityClass
@StableAPI(since = "0.6.0")
public final class ResourceUtil {
/**
* Reads a resource as a string, using the {@link StandardCharsets#UTF_8} charset, from a specific jar file. See
* {@link #getResourceFromJar(String, Class)}.
*
* @param resourcePath The resource to read
* @param referenceClass The class, from whose jar to get the resource from
*
* @return The string from the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static String getResourceStringFromJar(String resourcePath, Class<?> referenceClass) throws IOException {
return getResourceStringFromJar(resourcePath, referenceClass, StandardCharsets.UTF_8);
}
/**
* Reads a resource as a string from a specific jar file. See {@link #getResourceFromJar(String, Class)}.
*
* @param resourcePath The resource to read
* @param referenceClass The class, from whose jar to get the resource from
* @param charset The charset to decode the raw bytes with
*
* @return The string from the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static String getResourceStringFromJar(String resourcePath, Class<?> referenceClass, Charset charset)
throws IOException {
return new String(getResourceBytesFromJar(resourcePath, referenceClass), charset);
}
/**
* Reads the raw bytes of a resource in a specific jar file. See {@link #getResourceFromJar(String, Class)}.
*
* @param resourcePath The resource to read
* @param referenceClass The class, from whose jar to get the resource from
*
* @return The bytes of the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static byte[] getResourceBytesFromJar(String resourcePath, Class<?> referenceClass) throws IOException {
return readBytesSafe(getResourceFromJar(resourcePath, referenceClass), resourcePath);
}
private static byte[] readBytesSafe(InputStream stream, String resourcePath) throws IOException {
if (stream == null) {
throw new FileNotFoundException("Could not find resource at " + resourcePath);
}
return readBytes(stream);
}
/**
* Retrieves a resource from the jar file of a specific class. Falls back to
* {@link Class#getResourceAsStream(String)} if the resource is not found in the jar, or the class is not from a jar
* file.
*
* @param resourcePath The resource to retrieve
* @param referenceClass The class, from whose jar to get the resource from
*
* @return The resource, or null if it was not found.
*/
@StableAPI.Expose
public static InputStream getResourceFromJar(String resourcePath, Class<?> referenceClass) {
URL classFile = referenceClass.getResource('/' + referenceClass.getName().replace('.', '/') + ".class");
lookup:
{
if (classFile == null) {
break lookup;
}
String file = classFile.getFile();
int id = file.indexOf("!");
if (!classFile.getProtocol().equals("jar") || id < 0) {
break lookup;
}
//Loading from a jar
try {
URL resource = new URL("jar:" + file.substring(0, id) + "!" + resourcePath);
return resource.openStream();
} catch (IOException e) {
FPLog.LOG.error("Failed to load resource " + resourcePath + " from jar " + file.substring(0, id), e);
}
}
//Fallback logic
FPLog.LOG.warn("Using fallback resource loading logic for "
+ resourcePath
+ " with reference to "
+ referenceClass.getName());
return referenceClass.getResourceAsStream(resourcePath);
}
/**
* Fully reads a stream into a byte array.
*
* @param stream The stream to read
*
* @return The data from the stream
*
* @throws IOException From {@link InputStream#read(byte[])}
*/
@StableAPI.Expose
public static byte[] readBytes(InputStream stream) throws IOException {
val out = new ByteArrayOutputStream();
val buf = new byte[4096];
int read;
while ((read = stream.read(buf)) >= 0) {
out.write(buf, 0, read);
}
return out.toByteArray();
}
/**
* Reads a resource as a string, using the {@link StandardCharsets#UTF_8} charset.
*
* @param resourcePath The resource to read
*
* @return The string from the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static String getResourceString(String resourcePath) throws IOException {
return getResourceString(resourcePath, StandardCharsets.UTF_8);
}
/**
* Reads a resource as a string.
*
* @param resourcePath The resource to read
* @param charset The charset to decode the raw bytes with
*
* @return The string from the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static String getResourceString(String resourcePath, Charset charset) throws IOException {
return new String(getResourceBytes(resourcePath), charset);
}
/**
* Reads the raw bytes of a resource.
*
* @param resourcePath The resource to read
*
* @return The bytes of the resource
*
* @throws IOException From {@link #readBytes(InputStream)}
*/
@StableAPI.Expose
public static byte[] getResourceBytes(String resourcePath) throws IOException {
return readBytesSafe(ResourceUtil.class.getResourceAsStream(resourcePath), resourcePath);
}
}