| 1 |
package ezvcard.property; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.nio.file.Files; |
| 7 |
import java.nio.file.Path; |
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.LinkedHashMap; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
|
| 13 |
import ezvcard.VCard; |
| 14 |
import ezvcard.VCardVersion; |
| 15 |
import ezvcard.ValidationWarning; |
| 16 |
import ezvcard.parameter.MediaTypeParameter; |
| 17 |
import ezvcard.parameter.Pid; |
| 18 |
import ezvcard.util.Gobble; |
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public abstract class BinaryProperty<T extends MediaTypeParameter> extends VCardProperty implements HasAltId { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected byte[] data; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected String url; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected T contentType; |
| 69 |
|
| 70 |
public BinaryProperty() { |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public BinaryProperty(String url, T type) { |
| 80 |
setUrl(url, type); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public BinaryProperty(byte[] data, T type) { |
| 89 |
setData(data, type); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public BinaryProperty(InputStream in, T type) throws IOException { |
| 99 |
this(new Gobble(in).asByteArray(), type); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public BinaryProperty(Path file, T type) throws IOException { |
| 109 |
this(new BufferedInputStream(Files.newInputStream(file)), type); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public BinaryProperty(BinaryProperty<T> original) { |
| 117 |
super(original); |
| 118 |
data = (original.data == null) ? null : original.data.clone(); |
| 119 |
url = original.url; |
| 120 |
contentType = original.contentType; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public byte[] getData() { |
| 128 |
return data; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public void setData(byte[] data, T type) { |
| 137 |
this.url = null; |
| 138 |
this.data = data; |
| 139 |
setContentType(type); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public String getUrl() { |
| 147 |
return url; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void setUrl(String url, T type) { |
| 156 |
this.url = url; |
| 157 |
this.data = null; |
| 158 |
setContentType(type); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public T getContentType() { |
| 166 |
return contentType; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public void setContentType(T contentType) { |
| 174 |
this.contentType = contentType; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public String getType() { |
| 187 |
return parameters.getType(); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void setType(String type) { |
| 200 |
parameters.setType(type); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public List<Pid> getPids() { |
| 205 |
return super.getPids(); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public Integer getPref() { |
| 210 |
return super.getPref(); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void setPref(Integer pref) { |
| 215 |
super.setPref(pref); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
public String getAltId() { |
| 220 |
return parameters.getAltId(); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
public void setAltId(String altId) { |
| 225 |
parameters.setAltId(altId); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 230 |
if (url == null && data == null) { |
| 231 |
warnings.add(new ValidationWarning(8)); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
protected Map<String, Object> toStringValues() { |
| 237 |
Map<String, Object> values = new LinkedHashMap<>(); |
| 238 |
values.put("data", (data == null) ? "null" : "length: " + data.length); |
| 239 |
values.put("url", url); |
| 240 |
values.put("contentType", contentType); |
| 241 |
return values; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public int hashCode() { |
| 246 |
final int prime = 31; |
| 247 |
int result = super.hashCode(); |
| 248 |
result = prime * result + ((contentType == null) ? 0 : contentType.hashCode()); |
| 249 |
result = prime * result + Arrays.hashCode(data); |
| 250 |
result = prime * result + ((url == null) ? 0 : url.hashCode()); |
| 251 |
return result; |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public boolean equals(Object obj) { |
| 256 |
if (this == obj) return true; |
| 257 |
if (!super.equals(obj)) return false; |
| 258 |
BinaryProperty<?> other = (BinaryProperty<?>) obj; |
| 259 |
if (contentType == null) { |
| 260 |
if (other.contentType != null) return false; |
| 261 |
} else if (!contentType.equals(other.contentType)) return false; |
| 262 |
if (!Arrays.equals(data, other.data)) return false; |
| 263 |
if (url == null) { |
| 264 |
if (other.url != null) return false; |
| 265 |
} else if (!url.equals(other.url)) return false; |
| 266 |
return true; |
| 267 |
} |
| 268 |
} |