| 1 | package ezvcard.property; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.nio.file.Path; |
| 6 | |
| 7 | import ezvcard.parameter.SoundType; |
| 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 | * Defines a sound, such as the correct pronunciation of the person's name. |
| 41 | * </p> |
| 42 | * |
| 43 | * <p> |
| 44 | * <b>Code sample (creating)</b> |
| 45 | * </p> |
| 46 | * |
| 47 | * <pre class="brush:java"> |
| 48 | * VCard vcard = new VCard(); |
| 49 | * |
| 50 | * //URL |
| 51 | * Sound sound = new Sound("http://www.mywebsite.com/my-name.ogg", SoundType.OGG); |
| 52 | * vcard.addSound(sound); |
| 53 | * |
| 54 | * //binary data |
| 55 | * byte data[] = ... |
| 56 | * sound = new Sound(data, SoundType.OGG); |
| 57 | * vcard.addSound(sound); |
| 58 | * </pre> |
| 59 | * |
| 60 | * <p> |
| 61 | * <b>Code sample (retrieving)</b> |
| 62 | * </p> |
| 63 | * |
| 64 | * <pre class="brush:java"> |
| 65 | * VCard vcard = ... |
| 66 | * for (Sound sound : vcard.getSounds()) { |
| 67 | * SoundType contentType = sound.getContentType(); //e.g. "audio/ogg" |
| 68 | * |
| 69 | * String url = sound.getUrl(); |
| 70 | * if (url != null) { |
| 71 | * //property value is a URL |
| 72 | * continue; |
| 73 | * } |
| 74 | * |
| 75 | * byte[] data = sound.getData(); |
| 76 | * if (data != null) { |
| 77 | * //property value is binary data |
| 78 | * continue; |
| 79 | * } |
| 80 | * } |
| 81 | * </pre> |
| 82 | * |
| 83 | * <p> |
| 84 | * <b>Property name:</b> {@code SOUND} |
| 85 | * </p> |
| 86 | * <p> |
| 87 | * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0} |
| 88 | * </p> |
| 89 | * @author Michael Angstadt |
| 90 | * @see <a href="http://tools.ietf.org/html/rfc6350#page-45">RFC 6350 p.45</a> |
| 91 | * @see <a href="http://tools.ietf.org/html/rfc2426#page-23">RFC 2426 p.23</a> |
| 92 | * @see <a href="http://www.imc.org/pdi/vcard-21.doc">vCard 2.1 p.20</a> |
| 93 | */ |
| 94 | public class Sound extends BinaryProperty<SoundType> { |
| 95 | /** |
| 96 | * Creates a sound property. |
| 97 | * @param url the URL to the sound file |
| 98 | * @param type the content type (e.g. OGG) |
| 99 | */ |
| 100 | public Sound(String url, SoundType type) { |
| 101 | super(url, type); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates a sound property. |
| 106 | * @param data the binary data of the sound file |
| 107 | * @param type the content type (e.g. OGG) |
| 108 | */ |
| 109 | public Sound(byte[] data, SoundType type) { |
| 110 | super(data, type); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a sound property. |
| 115 | * @param in an input stream to the binary data (will be closed) |
| 116 | * @param type the content type (e.g. OGG) |
| 117 | * @throws IOException if there's a problem reading from the input stream |
| 118 | */ |
| 119 | public Sound(InputStream in, SoundType type) throws IOException { |
| 120 | super(in, type); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a sound property. |
| 125 | * @param file the sound file |
| 126 | * @param type the content type (e.g. OGG) |
| 127 | * @throws IOException if there's a problem reading from the file |
| 128 | */ |
| 129 | public Sound(Path file, SoundType type) throws IOException { |
| 130 | super(file, type); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Copy constructor. |
| 135 | * @param original the property to make a copy of |
| 136 | */ |
| 137 | public Sound(Sound original) { |
| 138 | super(original); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public String getLanguage() { |
| 143 | return super.getLanguage(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void setLanguage(String language) { |
| 148 | super.setLanguage(language); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Sound copy() { |
| 153 | return new Sound(this); |
| 154 | } |
| 155 | } |