| 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 |
|
| 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 |
public final class DataUri { |
| 48 |
private final byte[] data; |
| 49 |
private final String text; |
| 50 |
private final String contentType; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public DataUri(String contentType, byte[] data) { |
| 58 |
this(contentType, data, null); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public DataUri(String contentType, String text) { |
| 67 |
this(contentType, null, text); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public DataUri(String text) { |
| 75 |
this("text/plain", text); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static DataUri parse(String uri) { |
| 100 |
|
| 101 |
|
| 102 |
String scheme = "data:"; |
| 103 |
if (uri.length() < scheme.length() || !uri.substring(0, scheme.length()).equalsIgnoreCase(scheme)) { |
| 104 |
|
| 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 |
|
| 176 |
|
| 177 |
|
| 178 |
public byte[] getData() { |
| 179 |
return data; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public String getContentType() { |
| 187 |
return contentType; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public String getText() { |
| 195 |
return text; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
@Override |
| 203 |
public String toString() { |
| 204 |
return toString(null); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 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 |
} |