Debian Codegraph @

src/main/java/ezvcard/property/BinaryProperty.java

raw symbols SCIP
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 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * Represents a property whose value contains binary data.
51 * @author Michael Angstadt
52 * @param <T> the class used for representing the content type of the resource
53 */
54 public abstract class BinaryProperty<T extends MediaTypeParameter> extends VCardProperty implements HasAltId {
55 /**
56 * The decoded data.
57 */
58 protected byte[] data;
59
60 /**
61 * The URL to the resource.
62 */
63 protected String url;
64
65 /**
66 * The content type of the resource (for example, a JPEG image).
67 */
68 protected T contentType;
69
70 public BinaryProperty() {
71 //empty
72 }
73
74 /**
75 * Creates a binary property.
76 * @param url the URL to the resource
77 * @param type the content type
78 */
79 public BinaryProperty(String url, T type) {
80 setUrl(url, type);
81 }
82
83 /**
84 * Creates a binary property.
85 * @param data the binary data
86 * @param type the content type
87 */
88 public BinaryProperty(byte[] data, T type) {
89 setData(data, type);
90 }
91
92 /**
93 * Creates a binary property.
94 * @param in an input stream to the binary data (will be closed)
95 * @param type the content type
96 * @throws IOException if there is a problem reading from the input stream
97 */
98 public BinaryProperty(InputStream in, T type) throws IOException {
99 this(new Gobble(in).asByteArray(), type);
100 }
101
102 /**
103 * Creates a binary property.
104 * @param file the file containing the binary data
105 * @param type the content type
106 * @throws IOException if there is a problem reading from the file
107 */
108 public BinaryProperty(Path file, T type) throws IOException {
109 this(new BufferedInputStream(Files.newInputStream(file)), type);
110 }
111
112 /**
113 * Copy constructor.
114 * @param original the property to make a copy of
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 * Gets the binary data of the resource.
125 * @return the binary data or null if there is none
126 */
127 public byte[] getData() {
128 return data;
129 }
130
131 /**
132 * Sets the binary data of the resource.
133 * @param data the binary data
134 * @param type the content type (e.g. "JPEG image")
135 */
136 public void setData(byte[] data, T type) {
137 this.url = null;
138 this.data = data;
139 setContentType(type);
140 }
141
142 /**
143 * Gets the URL to the resource
144 * @return the URL or null if there is none
145 */
146 public String getUrl() {
147 return url;
148 }
149
150 /**
151 * Sets the URL to the resource.
152 * @param url the URL
153 * @param type the content type (e.g. "JPEG image")
154 */
155 public void setUrl(String url, T type) {
156 this.url = url;
157 this.data = null;
158 setContentType(type);
159 }
160
161 /**
162 * Gets the content type of the resource.
163 * @return the content type (e.g. "JPEG image")
164 */
165 public T getContentType() {
166 return contentType;
167 }
168
169 /**
170 * Sets the content type of the resource.
171 * @param contentType the content type (e.g. "JPEG image")
172 */
173 public void setContentType(T contentType) {
174 this.contentType = contentType;
175 }
176
177 /**
178 * Gets the vCard 4.0 TYPE parameter. This should NOT be used to get the
179 * TYPE parameter for 2.1/3.0 vCards. Use {@link #getContentType} instead.
180 * <p>
181 * <b>Supported versions:</b> {@code 4.0}
182 * </p>
183 * @return the TYPE value (typically, this will be either "work" or "home")
184 * or null if it doesn't exist
185 */
186 public String getType() {
187 return parameters.getType();
188 }
189
190 /**
191 * Sets the vCard 4.0 TYPE parameter. This should NOT be used to set the
192 * TYPE parameter for 2.1/3.0 vCards. Use {@link #setContentType} instead.
193 * <p>
194 * <b>Supported versions:</b> {@code 4.0}
195 * </p>
196 * @param type the TYPE value (should be either "work" or "home") or null to
197 * remove
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 //@Override
219 public String getAltId() {
220 return parameters.getAltId();
221 }
222
223 //@Override
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 }