|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package com.sun.javafx.util; |
| 27 | + |
| 28 | +import java.net.URLDecoder; |
| 29 | +import java.nio.charset.Charset; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Base64; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Objects; |
| 36 | + |
| 37 | +public class DataURI { |
| 38 | + |
| 39 | + /** |
| 40 | + * Determines whether the specified URI uses the "data" scheme. |
| 41 | + */ |
| 42 | + public static boolean matchScheme(String uri) { |
| 43 | + if (uri == null || uri.length() < 6) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + uri = uri.stripLeading(); |
| 48 | + |
| 49 | + return uri.length() > 5 && "data:".equalsIgnoreCase(uri.substring(0, 5)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Parses the specified URI if it uses the "data" scheme. |
| 54 | + * |
| 55 | + * @return a {@link DataURI} instance if {@code uri} uses the "data" scheme, {@code null} otherwise |
| 56 | + * @throws IllegalArgumentException if the URI is malformed |
| 57 | + */ |
| 58 | + public static DataURI tryParse(String uri) { |
| 59 | + if (!matchScheme(uri)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + uri = uri.trim(); |
| 64 | + |
| 65 | + int dataSeparator = uri.indexOf(',', 5); |
| 66 | + if (dataSeparator < 0) { |
| 67 | + throw new IllegalArgumentException("Invalid URI: " + uri); |
| 68 | + } |
| 69 | + |
| 70 | + String mimeType = "text", mimeSubtype = "plain"; |
| 71 | + boolean base64 = false; |
| 72 | + |
| 73 | + String[] headers = uri.substring(5, dataSeparator).split(";"); |
| 74 | + Map<String, String> nameValuePairs = Collections.emptyMap(); |
| 75 | + |
| 76 | + if (headers.length > 0) { |
| 77 | + int start = 0; |
| 78 | + |
| 79 | + int mimeSeparator = headers[0].indexOf('/'); |
| 80 | + if (mimeSeparator > 0) { |
| 81 | + mimeType = headers[0].substring(0, mimeSeparator); |
| 82 | + mimeSubtype = headers[0].substring(mimeSeparator + 1); |
| 83 | + start = 1; |
| 84 | + } |
| 85 | + |
| 86 | + for (int i = start; i < headers.length; ++i) { |
| 87 | + String header = headers[i]; |
| 88 | + int separator = header.indexOf('='); |
| 89 | + if (separator < 0) { |
| 90 | + if (i < headers.length - 1) { |
| 91 | + throw new IllegalArgumentException("Invalid URI: " + uri); |
| 92 | + } |
| 93 | + |
| 94 | + base64 = "base64".equalsIgnoreCase(headers[headers.length - 1]); |
| 95 | + } else { |
| 96 | + if (nameValuePairs.isEmpty()) { |
| 97 | + nameValuePairs = new HashMap<>(); |
| 98 | + } |
| 99 | + |
| 100 | + nameValuePairs.put(header.substring(0, separator).toLowerCase(), header.substring(separator + 1)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + String data = uri.substring(dataSeparator + 1); |
| 106 | + Charset charset = Charset.defaultCharset(); |
| 107 | + |
| 108 | + return new DataURI( |
| 109 | + uri, |
| 110 | + data, |
| 111 | + mimeType, |
| 112 | + mimeSubtype, |
| 113 | + nameValuePairs, |
| 114 | + base64, |
| 115 | + base64 ? |
| 116 | + Base64.getDecoder().decode(data) : |
| 117 | + URLDecoder.decode(data.replace("+", "%2B"), charset).getBytes(charset)); |
| 118 | + } |
| 119 | + |
| 120 | + private final String originalUri; |
| 121 | + private final String originalData; |
| 122 | + private final String mimeType, mimeSubtype; |
| 123 | + private final Map<String, String> parameters; |
| 124 | + private final boolean base64; |
| 125 | + private final byte[] data; |
| 126 | + |
| 127 | + private DataURI( |
| 128 | + String originalUri, |
| 129 | + String originalData, |
| 130 | + String mimeType, |
| 131 | + String mimeSubtype, |
| 132 | + Map<String, String> parameters, |
| 133 | + boolean base64, |
| 134 | + byte[] decodedData) { |
| 135 | + this.originalUri = originalUri; |
| 136 | + this.originalData = originalData; |
| 137 | + this.mimeType = mimeType; |
| 138 | + this.mimeSubtype = mimeSubtype; |
| 139 | + this.parameters = parameters; |
| 140 | + this.base64 = base64; |
| 141 | + this.data = decodedData; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns the MIME type that was specified in the URI. |
| 146 | + * If no MIME type was specified, returns "text". |
| 147 | + */ |
| 148 | + public String getMimeType() { |
| 149 | + return mimeType; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns the MIME subtype that was specified in the URI. |
| 154 | + * If no MIME subtype was specified, returns "plain". |
| 155 | + */ |
| 156 | + public String getMimeSubtype() { |
| 157 | + return mimeSubtype; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the key-value parameter pairs that were specified in the URI. |
| 162 | + */ |
| 163 | + public Map<String, String> getParameters() { |
| 164 | + return parameters; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns whether the data in the URI is Base64-encoded. |
| 169 | + * If {@code false}, the data is implied to be URL-encoded. |
| 170 | + */ |
| 171 | + public boolean isBase64() { |
| 172 | + return base64; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Returns the data that is encoded in this URI. |
| 177 | + * <p>Note that repeated calls to this method will return the same array instance. |
| 178 | + */ |
| 179 | + public byte[] getData() { |
| 180 | + return data; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + if (originalData.length() < 32) { |
| 186 | + return originalUri; |
| 187 | + } |
| 188 | + |
| 189 | + return originalUri.substring(0, originalUri.length() - originalData.length()) |
| 190 | + + originalData.substring(0, 14) + "..." + originalData.substring(originalData.length() - 14); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public boolean equals(Object o) { |
| 195 | + if (this == o) return true; |
| 196 | + if (!(o instanceof DataURI)) return false; |
| 197 | + DataURI dataURI = (DataURI)o; |
| 198 | + return base64 == dataURI.base64 |
| 199 | + && Objects.equals(mimeType, dataURI.mimeType) |
| 200 | + && Objects.equals(mimeSubtype, dataURI.mimeSubtype) |
| 201 | + && Arrays.equals(data, dataURI.data); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int hashCode() { |
| 206 | + int result = Objects.hash(mimeType, mimeSubtype, base64); |
| 207 | + result = 31 * result + Arrays.hashCode(data); |
| 208 | + return result; |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments