URLUtil { @Nullable public static byte[] getBytesFromDataUri(@NotNull String dataUrl) { Matcher matcher = DATA_URI_PATTERN.matcher(StringUtil.unquoteString(dataUrl)); if (matcher.matches()) { try { String content = matcher.group(4); return ";base64".equalsIgnoreCase(matcher.group(3)) ? Base64Converter.decode(content.getBytes(CharsetToolkit.UTF8_CHARSET)) : content.getBytes(CharsetToolkit.UTF8_CHARSET); } catch (IllegalArgumentException e) { return null; } } return null; } private  URLUtil(); @NotNull static InputStream openStream(@NotNull URL url); @NotNull static InputStream openResourceStream(@NotNull URL url); @Nullable static Pair<String, String> splitJarUrl(@NotNull String url); @NotNull static String unescapePercentSequences(@NotNull String s); static boolean containsScheme(@NotNull String url); static boolean isDataUri(@NotNull String value); @Nullable static byte[] getBytesFromDataUri(@NotNull String dataUrl); @NotNull static String parseHostFromSshUrl(@NotNull String sshUrl); static final String SCHEME_SEPARATOR; static final String FILE_PROTOCOL; static final String HTTP_PROTOCOL; static final String JAR_PROTOCOL; static final String JAR_SEPARATOR; static final Pattern DATA_URI_PATTERN; static final Pattern URL_PATTERN; }
@Test public void testDataUri() { byte[] test = "test".getBytes(CharsetToolkit.UTF8_CHARSET); assertThat(URLUtil.getBytesFromDataUri("data:text/plain;charset=utf-8;base64,dGVzdA==")).isEqualTo(test); assertThat(URLUtil.getBytesFromDataUri("data:text/plain;charset:utf-8;base64,dGVzdA==")).isEqualTo(test); }