View Javadoc

1   /*
2    * Entagged Audio Tag library
3    * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
4    * 
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *  
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package entagged.audioformats.asf.data;
20  
21  import java.util.Arrays;
22  
23  import entagged.audioformats.asf.util.Utils;
24  
25  /***
26   * This class is used for representation of GUIDs and as a reference list of all
27   * Known GUIDs. <br>
28   * 
29   * @author Christian Laireiter
30   */
31  public class GUID {
32  
33  	/***
34  	 * This constant defines the GUID for stream chunks describing audio
35  	 * streams, indicating the the audio stream has no error concealment. <br>
36  	 */
37  	public final static GUID GUID_AUDIO_ERROR_CONCEALEMENT_ABSENT = new GUID(
38  			new int[] { 0x40, 0xA4, 0xF1, 0x49, 0xCE, 0x4E, 0xD0, 0x11, 0xA3,
39  					0xAC, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
40  			"Audio error concealment absent.");
41  
42  	/***
43  	 * This constant defines the GUID for stream chunks describing audio
44  	 * streams, indicating the the audio stream has interleaved error
45  	 * concealment. <br>
46  	 */
47  	public final static GUID GUID_AUDIO_ERROR_CONCEALEMENT_INTERLEAVED = new GUID(
48  			new int[] { 0x40, 0xA4, 0xF1, 0x49, 0xCE, 0x4E, 0xD0, 0x11, 0xA3,
49  					0xAC, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
50  			"Interleaved audio error concealment.");
51  
52  	/***
53  	 * This constant stores the GUID indicating that stream type is audio.
54  	 */
55  	public final static GUID GUID_AUDIOSTREAM = new GUID(new int[] { 0x40,
56  			0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80,
57  			0x5F, 0x5C, 0x44, 0x2B }, " Audio stream");
58  
59  	/***
60  	 * This constant represents the guid for a chunk which contains Title,
61  	 * author, copyright, description and rating.
62  	 */
63  	public final static GUID GUID_CONTENTDESCRIPTION = new GUID(new int[] {
64  			0x33, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00,
65  			0xAA, 0x00, 0x62, 0xCE, 0x6C }, "Content Description");
66  
67  	/***
68  	 * This constant stores the GUID for Encoding-Info chunks.
69  	 */
70  	public final static GUID GUID_ENCODING = new GUID(new int[] { 0x40, 0x52,
71  			0xD1, 0x86, 0x1D, 0x31, 0xD0, 0x11, 0xA3, 0xA4, 0x00, 0xA0, 0xC9,
72  			0x03, 0x48, 0xF6 }, "Encoding description");
73  
74  	/***
75  	 * This constant defines the GUID for a WMA "Extended Content Description"
76  	 * chunk. <br>
77  	 */
78  	public final static GUID GUID_EXTENDED_CONTENT_DESCRIPTION = new GUID(
79  			new int[] { 0x40, 0xA4, 0xD0, 0xD2, 0x07, 0xE3, 0xD2, 0x11, 0x97,
80  					0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50 },
81  			"Extended Content Description");
82  
83  	/***
84  	 * GUID of ASF file header.
85  	 */
86  	public final static GUID GUID_FILE = new GUID(new int[] { 0xA1, 0xDC, 0xAB,
87  			0x8C, 0x47, 0xA9, 0xCF, 0x11, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20,
88  			0x53, 0x65 }, "File header");
89  
90  	/***
91  	 * This constant defines the GUID of a asf header chunk.
92  	 */
93  	public final static GUID GUID_HEADER = new GUID(new int[] { 0x30, 0x26,
94  			0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00,
95  			0x62, 0xce, 0x6c }, "Asf header");
96  
97  	/***
98  	 * This constant stores the length of GUIDs used with ASF streams. <br>
99  	 */
100 	public final static int GUID_LENGTH = 16;
101 
102 	/***
103 	 * This constant stores the GUID indicating a stream object.
104 	 */
105 	public final static GUID GUID_STREAM = new GUID(new int[] { 0x91, 0x07,
106 			0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C,
107 			0x20, 0x53, 0x65 }, "Stream");
108 
109 	/***
110 	 * This constant stores a GUID whose functionality is unknown.
111 	 */
112 	public final static GUID GUID_HEADER_EXTENSION = new GUID(new int[] { 0xB5, 0x03,
113 			0xBF, 0x5F, 0x2E, 0xA9, 0xCF, 0x11, 0x8E, 0xE3, 0x00, 0xC0, 0x0C,
114 			0x20, 0x53, 0x65 }, "Header Extension");
115 
116 	/***
117 	 * This constant stores a GUID indicating a "stream bitrate properties"
118 	 * chunk.
119 	 */
120 	public final static GUID GUID_STREAM_BITRATE_PROPERTIES = new GUID(
121 			new int[] { 0xCE, 0x75, 0xF8, 0x7B, 0x8D, 0x46, 0xD1, 0x11, 0x8D,
122 					0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 },
123 			"Stream bitrate properties");
124 
125 	/***
126 	 * This constant stores the GUID indicating that stream type is video.
127 	 */
128 	public final static GUID GUID_VIDEOSTREAM = new GUID(new int[] { 0xC0,
129 			0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80,
130 			0x5F, 0x5C, 0x44, 0x2B }, "Video stream");
131 
132 	/***
133 	 * This field stores all knwon GUIDs.
134 	 */
135 	public final static GUID[] KNOWN_GUIDS = new GUID[] {
136 			GUID_AUDIO_ERROR_CONCEALEMENT_ABSENT,
137 			GUID_AUDIO_ERROR_CONCEALEMENT_INTERLEAVED, GUID_CONTENTDESCRIPTION,
138 			GUID_AUDIOSTREAM, GUID_ENCODING, GUID_FILE, GUID_HEADER,
139 			GUID_STREAM, GUID_EXTENDED_CONTENT_DESCRIPTION, GUID_VIDEOSTREAM,
140 			GUID_HEADER_EXTENSION, GUID_STREAM_BITRATE_PROPERTIES };
141 
142 	/***
143 	 * This method checks if the given <code>value</code> is matching the GUID
144 	 * specification of ASF streams. <br>
145 	 * 
146 	 * @param value
147 	 *            possible GUID.
148 	 * @return <code>true</code> if <code>value</code> matches the
149 	 *         specification of a GUID.
150 	 */
151 	public static boolean assertGUID(int[] value) {
152 		boolean result = false;
153 		if (value != null) {
154 			if (value.length == GUID.GUID_LENGTH) {
155 				result = true;
156 			}
157 		}
158 		return result;
159 	}
160 
161 	/***
162 	 * This method searches a GUID in {@link #KNOWN_GUIDS}which is equal to the
163 	 * given <code>guid</code> and returns its description. <br>
164 	 * This method is useful if a guid was read out of a file and no
165 	 * identification has been done yet.
166 	 * 
167 	 * @param guid
168 	 *            guid, which description is needed.
169 	 * @return description of the guid if found. Else <code>null</code>
170 	 */
171 	public static String getGuidDescription(GUID guid) {
172 		String result = null;
173 		if (guid == null) {
174 			throw new IllegalArgumentException("Argument must not be null.");
175 		}
176 		for (int i = 0; i < KNOWN_GUIDS.length; i++) {
177 			if (KNOWN_GUIDS[i].equals(guid)) {
178 				result = KNOWN_GUIDS[i].getDescription();
179 			}
180 		}
181 		return result;
182 	}
183 
184 	/***
185 	 * Stores an optionally description of the GUID.
186 	 */
187 	private String description = "";
188 
189 	/***
190 	 * An isntance of this class stores the value of the wrapped GUID in this
191 	 * field. <br>
192 	 */
193 	private int[] guid = null;
194 
195 
196 	/***
197 	 * Creates an instance and assigns given <code>guid</code>.<br>
198 	 * 
199 	 * @param value
200 	 *            Guid, which should be assigned.
201 	 */
202 	public GUID(int[] value) {
203 		setGUID(value);
204 	}
205 
206 	/***
207 	 * Creates an instance like {@link #GUID(int[])}and sets the optional
208 	 * description. <br>
209 	 * 
210 	 * @param value
211 	 *            Guid, which should be assigned.
212 	 * @param desc
213 	 *            Description for the guid.
214 	 */
215 	public GUID(int[] value, String desc) {
216 		this(value);
217 		if (desc == null) {
218 			throw new IllegalArgumentException("Argument must not be null.");
219 		}
220 		this.description = desc;
221 	}
222 
223 	/***
224 	 * This method compares two objects. If the given Object is a {@link  GUID},
225 	 * the stored GUID values are compared. <br>
226 	 * 
227 	 * @see java.lang.Object#equals(java.lang.Object)
228 	 */
229 	public boolean equals(Object obj) {
230 		boolean result = false;
231 		if (obj instanceof GUID) {
232 			GUID other = (GUID) obj;
233 			result = Arrays.equals(this.getGUID(), other.getGUID());
234 		} else {
235 			result = super.equals(obj);
236 		}
237 		return result;
238 	}
239 
240 	/***
241 	 * This method returns the guid as an array of bytes. <br>
242 	 * 
243 	 * @see #getGUID()
244 	 * @return The guid as a byte array.
245 	 */
246 	public byte[] getBytes() {
247 		byte[] result = new byte[this.guid.length];
248 		for (int i = 0; i < result.length; i++) {
249 			result[i] = (byte) (this.guid[i] & 0xFF);
250 		}
251 		return result;
252 	}
253 
254 	/***
255 	 * @return Returns the description.
256 	 */
257 	public String getDescription() {
258 		return description;
259 	}
260 
261 	/***
262 	 * This method returns the GUID of this object. <br>
263 	 * 
264 	 * @return stored GUID.
265 	 */
266 	public int[] getGUID() {
267 		int[] copy = new int[this.guid.length];
268 		System.arraycopy(this.guid, 0, copy, 0, this.guid.length);
269 		return copy;
270 	}
271 
272 	/***
273 	 * This method checks if the currently stored GUID ({@link #guid}) is
274 	 * correctly filled. <br>
275 	 * 
276 	 * @return <code>true</code> if it is.
277 	 */
278 	public boolean isValid() {
279 		return assertGUID(getGUID());
280 	}
281 
282 	/***
283 	 * This method saves a copy of the given <code>value</code> as the
284 	 * represented value of this object. <br>
285 	 * The given value is checked with {@link #assertGUID(int[])}.<br>
286 	 * 
287 	 * @param value
288 	 *            GUID to assign.
289 	 */
290 	private void setGUID(int[] value) {
291 		if (assertGUID(value)) {
292 			this.guid = new int[GUID_LENGTH];
293 			System.arraycopy(value, 0, this.guid, 0, GUID_LENGTH);
294 		} else {
295 			throw new IllegalArgumentException(
296 					"The given guid doesn't match the GUID specification.");
297 		}
298 	}
299 
300 	/***
301 	 * This method gives a hex formatted representation of {@link #getGUID()}
302 	 * 
303 	 * @see java.lang.Object#toString()
304 	 */
305 	public String toString() {
306 		StringBuffer result = new StringBuffer();
307 		if (getDescription().trim().length() > 0) {
308 			result.append("Description: " + getDescription()
309 					+ Utils.LINE_SEPARATOR + "   ");
310 		}
311 		for (int i = 0; i < guid.length; i++) {
312 			String tmp = Integer.toHexString(guid[i]);
313 			if (tmp.length() < 2)
314 				tmp = "0" + tmp;
315 			if (i > 0)
316 				result.append(", ");
317 			result.append("0x" + tmp);
318 		}
319 		return result.toString();
320 	}
321 }