Debian Codegraph @

src/main/java/ezvcard/util/DataUri.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.Arrays;
5
6 import ezvcard.Messages;
7 import ezvcard.util.org.apache.commons.codec.binary.Base64;
8
9 /*
10 Copyright (c) 2012-2023, Michael Angstadt
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15
16 1. Redistributions of source code must retain the above copyright notice, this
17 list of conditions and the following disclaimer.
18 2. Redistributions in binary form must reproduce the above copyright notice,
19 this list of conditions and the following disclaimer in the documentation
20 and/or other materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 The views and conclusions contained in the software and documentation are those
34 of the authors and should not be interpreted as representing official policies,
35 either expressed or implied, of the FreeBSD Project.
36 */
37
38 /**
39 * <p>
40 * Represents a data URI.
41 * </p>
42 * <p>
43 * Example: {@code data:image/jpeg;base64,[base64 string]}
44 * </p>
45 * @author Michael Angstadt
46 */
47 public final class DataUri {
48 private final byte[] data;
49 private final String text;
50 private final String contentType;
51
52 /**
53 * Creates a data URI.
54 * @param contentType the content type of the data (e.g. "image/png")
55 * @param data the data
56 */
57 public DataUri(String contentType, byte[] data) {
58 this(contentType, data, null);
59 }
60
61 /**
62 * Creates a data URI.
63 * @param contentType the content type of the text (e.g. "text/html")
64 * @param text the text
65 */
66 public DataUri(String contentType, String text) {
67 this(contentType, null, text);
68 }
69
70 /**
71 * Creates a data URI with a content type of "text/plain".
72 * @param text the text
73 */
74 public DataUri(String text) {
75 this("text/plain", text);
76 }
77
78 /**
79 * Copies a data URI.
80 * @param original the data URI to copy
81 */
82 public DataUri(DataUri original) {
83 this(original.contentType, (original.data == null) ? null : original.data.clone(), original.text);
84 }
85
86 private DataUri(String contentType, byte[] data, String text) {
87 this.contentType = (contentType == null) ? "" : contentType.toLowerCase();
88 this.data = data;
89 this.text = text;
90 }
91
92 /**
93 * Parses a data URI string.
94 * @param uri the URI string (e.g. "data:image/jpeg;base64,[base64 string]")
95 * @return the parsed data URI
96 * @throws IllegalArgumentException if the string is not a valid data URI or
97 * it cannot be parsed
98 */
99 public static DataUri parse(String uri) {
100 //Syntax: data:[<media type>][;charset=<character set>][;base64],<data>
101
102 String scheme = "data:";
103 if (uri.length() < scheme.length() || !uri.substring(0, scheme.length()).equalsIgnoreCase(scheme)) {
104 //not a data URI
105 throw Messages.INSTANCE.getIllegalArgumentException(18, scheme);
106 }
107
108 String contentType = null;
109 String charset = null;
110 boolean base64 = false;
111 String dataStr = null;
112 int tokenStart = scheme.length();
113 for (int i = scheme.length(); i < uri.length(); i++) {
114 char c = uri.charAt(i);
115
116 if (c == ';') {
117 String token = uri.substring(tokenStart, i);
118 if (contentType == null) {
119 contentType = token.toLowerCase();
120 } else {
121 if (token.toLowerCase().startsWith("charset=")) {
122 int equals = token.indexOf('=');
123 charset = token.substring(equals + 1);
124 } else if ("base64".equalsIgnoreCase(token)) {
125 base64 = true;
126 }
127 }
128 tokenStart = i + 1;
129 continue;
130 }
131
132 if (c == ',') {
133 String token = uri.substring(tokenStart, i);
134 if (contentType == null) {
135 contentType = token.toLowerCase();
136 } else {
137 if (token.toLowerCase().startsWith("charset=")) {
138 int equals = token.indexOf('=');
139 charset = token.substring(equals + 1);
140 } else if ("base64".equalsIgnoreCase(token)) {
141 base64 = true;
142 }
143 }
144
145 dataStr = uri.substring(i + 1);
146 break;
147 }
148 }
149
150 if (dataStr == null) {
151 throw Messages.INSTANCE.getIllegalArgumentException(20);
152 }
153
154 String text = null;
155 byte[] data = null;
156 if (base64) {
157 dataStr = dataStr.replaceAll("\\s", "");
158 data = Base64.decodeBase64(dataStr);
159 if (charset != null) {
160 try {
161 text = new String(data, charset);
162 } catch (UnsupportedEncodingException e) {
163 throw new IllegalArgumentException(Messages.INSTANCE.getExceptionMessage(43, charset), e);
164 }
165 data = null;
166 }
167 } else {
168 text = dataStr;
169 }
170
171 return new DataUri(contentType, data, text);
172 }
173
174 /**
175 * Gets the binary data.
176 * @return the binary data or null if the value was text
177 */
178 public byte[] getData() {
179 return data;
180 }
181
182 /**
183 * Gets the content type.
184 * @return the content type (e.g. "image/png")
185 */
186 public String getContentType() {
187 return contentType;
188 }
189
190 /**
191 * Gets the text value.
192 * @return the text value or null if the value was binary
193 */
194 public String getText() {
195 return text;
196 }
197
198 /**
199 * Creates a data URI string.
200 * @return the data URI (e.g. "data:image/jpeg;base64,[base64 string]")
201 */
202 @Override
203 public String toString() {
204 return toString(null);
205 }
206
207 /**
208 * Creates a data URI string.
209 * @param charset only applicable if the data URI's value is text. Defines
210 * the character set to encode the text in, or null not to specify a
211 * character set
212 * @return the data URI (e.g. "data:image/jpeg;base64,[base64 string]")
213 * @throws IllegalArgumentException if the given character set is not
214 * supported by this JVM
215 */
216 public String toString(String charset) {
217 StringBuilder sb = new StringBuilder();
218 sb.append("data:");
219 sb.append(contentType);
220
221 if (data != null) {
222 sb.append(";base64,");
223 sb.append(Base64.encodeBase64String(data));
224 } else if (text != null) {
225 if (charset == null) {
226 sb.append(',').append(text);
227 } else {
228 byte[] data;
229 try {
230 data = text.getBytes(charset);
231 } catch (UnsupportedEncodingException e) {
232 throw new IllegalArgumentException(Messages.INSTANCE.getExceptionMessage(44, charset), e);
233 }
234
235 sb.append(";charset=").append(charset);
236 sb.append(";base64,");
237 sb.append(Base64.encodeBase64String(data));
238 }
239 } else {
240 sb.append(',');
241 }
242
243 return sb.toString();
244 }
245
246 @Override
247 public int hashCode() {
248 final int prime = 31;
249 int result = 1;
250 result = prime * result + contentType.hashCode();
251 result = prime * result + Arrays.hashCode(data);
252 result = prime * result + ((text == null) ? 0 : text.hashCode());
253 return result;
254 }
255
256 @Override
257 public boolean equals(Object obj) {
258 if (this == obj) return true;
259 if (obj == null) return false;
260 if (getClass() != obj.getClass()) return false;
261 DataUri other = (DataUri) obj;
262 if (!contentType.equals(other.contentType)) return false;
263 if (!Arrays.equals(data, other.data)) return false;
264 if (text == null) {
265 if (other.text != null) return false;
266 } else if (!text.equals(other.text)) return false;
267 return true;
268 }
269 }