| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.io.UnsupportedEncodingException; |
| 26 |
import java.net.URLDecoder; |
| 27 |
import java.nio.charset.Charset; |
| 28 |
import java.nio.charset.StandardCharsets; |
| 29 |
import java.util.*; |
| 30 |
import java.util.regex.Pattern; |
| 31 |
|
| 32 |
import org.jetbrains.annotations.NotNull; |
| 33 |
import org.jetbrains.annotations.Nullable; |
| 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 |
final class DataUri { |
| 60 |
private static final String CHARSET_OPTION_NAME = "charset"; |
| 61 |
private static final String FILENAME_OPTION_NAME = "filename"; |
| 62 |
private static final String CONTENT_DISPOSITION_OPTION_NAME = "content-disposition"; |
| 63 |
|
| 64 |
private final @NotNull String mime; |
| 65 |
private final @Nullable Charset charset; |
| 66 |
private final @Nullable String filename; |
| 67 |
private final @Nullable String contentDisposition; |
| 68 |
private final byte @NotNull [] data; |
| 69 |
|
| 70 |
public DataUri(String mime, Charset charset, byte[] data) { |
| 71 |
this(mime, charset, null, null, data); |
| 72 |
} |
| 73 |
|
| 74 |
public DataUri(@NotNull String mime, @Nullable Charset charset, @Nullable String filename, |
| 75 |
@Nullable String contentDisposition, byte @NotNull [] data) { |
| 76 |
this.mime = mime; |
| 77 |
this.charset = charset; |
| 78 |
this.filename = filename; |
| 79 |
this.contentDisposition = contentDisposition; |
| 80 |
this.data = data; |
| 81 |
} |
| 82 |
|
| 83 |
public @NotNull String mime() { |
| 84 |
return mime; |
| 85 |
} |
| 86 |
|
| 87 |
public byte @NotNull [] data() { |
| 88 |
return data; |
| 89 |
} |
| 90 |
|
| 91 |
public @Nullable Charset charset() { |
| 92 |
return charset; |
| 93 |
} |
| 94 |
|
| 95 |
public @Nullable String contentDisposition() { |
| 96 |
return contentDisposition; |
| 97 |
} |
| 98 |
|
| 99 |
public @Nullable String filename() { |
| 100 |
return filename; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public boolean equals(Object o) { |
| 105 |
if (this == o) return true; |
| 106 |
if (!(o instanceof DataUri)) return false; |
| 107 |
DataUri dataUri = (DataUri) o; |
| 108 |
return mime.equals(dataUri.mime) |
| 109 |
&& Objects.equals(charset, dataUri.charset) |
| 110 |
&& Objects.equals(filename, dataUri.filename) |
| 111 |
&& Objects.equals(contentDisposition, dataUri.contentDisposition) |
| 112 |
&& Arrays.equals(data, dataUri.data); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public int hashCode() { |
| 117 |
int result = Objects.hash(mime, charset, filename, contentDisposition); |
| 118 |
result = 31 * result + Arrays.hashCode(data); |
| 119 |
return result; |
| 120 |
} |
| 121 |
|
| 122 |
static final class MalformedDataUriException extends IOException { |
| 123 |
MalformedDataUriException(@NotNull String reason) { |
| 124 |
super(reason); |
| 125 |
} |
| 126 |
|
| 127 |
MalformedDataUriException(@NotNull Exception reason) { |
| 128 |
super(reason); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static DataUri parse(@NotNull String uri, Charset charset) throws MalformedDataUriException { |
| 144 |
|
| 145 |
|
| 146 |
if (!uri.toLowerCase(Locale.ENGLISH).startsWith("data:")) |
| 147 |
throw new MalformedDataUriException("URI must start with a case-insensitive `data:'"); |
| 148 |
|
| 149 |
|
| 150 |
if (-1 == uri.indexOf(',')) throw new MalformedDataUriException("URI must contain a `,'"); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
Collection<String> supportedContentEncodings = Collections.singletonList("base64"); |
| 155 |
|
| 156 |
|
| 157 |
String mimeType = "text/plain"; |
| 158 |
|
| 159 |
|
| 160 |
String contentEncoding = ""; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
boolean contentEncodingAlreadySet = false; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
final Map<String, String> supportedValues = new HashMap<>(); |
| 173 |
supportedValues.put(CHARSET_OPTION_NAME, ""); |
| 174 |
supportedValues.put(FILENAME_OPTION_NAME, ""); |
| 175 |
supportedValues.put(CONTENT_DISPOSITION_OPTION_NAME, ""); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
final Map<String, Boolean> supportedValueSetBits = new HashMap<>(); |
| 181 |
for (String key : supportedValues.keySet()) { |
| 182 |
supportedValueSetBits.put(key, false); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
int comma = uri.indexOf(','); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
String temp = uri.substring("data:".length(), comma); |
| 191 |
|
| 192 |
|
| 193 |
String[] headers = temp.split(";"); |
| 194 |
|
| 195 |
|
| 196 |
for (int header = 0; header < headers.length; ++header) { |
| 197 |
String s = headers[header]; |
| 198 |
|
| 199 |
|
| 200 |
s = s.toLowerCase(Locale.ENGLISH); |
| 201 |
|
| 202 |
|
| 203 |
int eq = s.indexOf('='); |
| 204 |
|
| 205 |
|
| 206 |
String name; |
| 207 |
String value = ""; |
| 208 |
|
| 209 |
|
| 210 |
if (-1 == eq) { |
| 211 |
|
| 212 |
|
| 213 |
name = percentDecode(s, charset); |
| 214 |
|
| 215 |
|
| 216 |
name = name.trim(); |
| 217 |
|
| 218 |
|
| 219 |
} else { |
| 220 |
|
| 221 |
|
| 222 |
name = s.substring(0, eq); |
| 223 |
|
| 224 |
|
| 225 |
name = percentDecode(name, charset); |
| 226 |
|
| 227 |
|
| 228 |
name = name.trim(); |
| 229 |
|
| 230 |
|
| 231 |
value = s.substring(eq + 1); |
| 232 |
|
| 233 |
|
| 234 |
value = percentDecode(value, charset); |
| 235 |
|
| 236 |
|
| 237 |
value = value.trim(); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if ((0 == header) && (-1 == eq) && !name.isEmpty()) { |
| 243 |
|
| 244 |
|
| 245 |
mimeType = name; |
| 246 |
|
| 247 |
|
| 248 |
} else { |
| 249 |
|
| 250 |
|
| 251 |
if (-1 == eq) { |
| 252 |
|
| 253 |
|
| 254 |
final String nameCaseInsensitive = name.toLowerCase(Locale.ENGLISH); |
| 255 |
|
| 256 |
if (supportedContentEncodings.contains(nameCaseInsensitive)) { |
| 257 |
|
| 258 |
|
| 259 |
if (!contentEncodingAlreadySet) { |
| 260 |
|
| 261 |
|
| 262 |
contentEncoding = name; |
| 263 |
|
| 264 |
|
| 265 |
contentEncodingAlreadySet = true; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
} else { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
final String nameCaseInsensitive = name.toLowerCase(Locale.ENGLISH); |
| 275 |
|
| 276 |
if (!value.isEmpty() && supportedValues.containsKey(nameCaseInsensitive)) { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
boolean valueSet = supportedValueSetBits.get(nameCaseInsensitive); |
| 281 |
|
| 282 |
if (!valueSet) { |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
supportedValues.put(nameCaseInsensitive, value); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
supportedValueSetBits.put(nameCaseInsensitive, true); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
String data = uri.substring(comma + 1); |
| 300 |
|
| 301 |
|
| 302 |
data = percentDecode(data, charset); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
final String finalMimeType = mimeType; |
| 307 |
final Charset finalCharset = supportedValues.get(CHARSET_OPTION_NAME).isEmpty() |
| 308 |
? null |
| 309 |
: Charset.forName(supportedValues.get(CHARSET_OPTION_NAME)); |
| 310 |
final String finalFilename = supportedValues.get(FILENAME_OPTION_NAME).isEmpty() |
| 311 |
? null |
| 312 |
: supportedValues.get(FILENAME_OPTION_NAME); |
| 313 |
final String finalContentDisposition = supportedValues.get(CONTENT_DISPOSITION_OPTION_NAME).isEmpty() |
| 314 |
? null |
| 315 |
: supportedValues.get(CONTENT_DISPOSITION_OPTION_NAME); |
| 316 |
|
| 317 |
final byte[] finalData; |
| 318 |
try { |
| 319 |
finalData = "base64".equalsIgnoreCase(contentEncoding) |
| 320 |
? Base64.getMimeDecoder().decode(data) |
| 321 |
: data.getBytes(charset); |
| 322 |
} catch (RuntimeException e) { |
| 323 |
throw new MalformedDataUriException(e); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
return new DataUri( |
| 328 |
finalMimeType, |
| 329 |
finalCharset, |
| 330 |
finalFilename, |
| 331 |
finalContentDisposition, |
| 332 |
finalData); |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public String toString() { |
| 337 |
StringBuilder s = new StringBuilder(); |
| 338 |
s.append("data:").append(this.mime()).append(";"); |
| 339 |
|
| 340 |
if (this.charset != null) s.append(CHARSET_OPTION_NAME + "=").append(this.charset.name()).append(";"); |
| 341 |
if (this.contentDisposition != null) |
| 342 |
s.append(CONTENT_DISPOSITION_OPTION_NAME + "=").append(this.contentDisposition).append(";"); |
| 343 |
if (this.filename != null) s.append(FILENAME_OPTION_NAME + "=").append(this.filename).append(";"); |
| 344 |
|
| 345 |
s.append("base64,").append(new String(Base64.getEncoder().encode(this.data()), StandardCharsets.UTF_8)); |
| 346 |
|
| 347 |
return s.toString(); |
| 348 |
} |
| 349 |
|
| 350 |
private static final Pattern PLUS = Pattern.compile("+", Pattern.LITERAL); |
| 351 |
|
| 352 |
private static String percentDecode(String s, Charset cs) { |
| 353 |
try { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
s = PLUS.matcher(s).replaceAll("%2B"); |
| 359 |
|
| 360 |
return URLDecoder.decode(s, cs.name()); |
| 361 |
} catch (UnsupportedEncodingException e) { |
| 362 |
throw new IllegalStateException("Charset `" + cs.name() + "' not supported", e); |
| 363 |
} |
| 364 |
} |
| 365 |
} |