Coverage Report - org.jaudiotagger.audio.asf.data.GUID
 
Classes in this File Line Coverage Branch Coverage Complexity
GUID
62%
54/87
22%
8/36
0
 
 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 org.jaudiotagger.audio.asf.data;
 20  
 
 21  
 import org.jaudiotagger.audio.asf.util.Utils;
 22  
 
 23  
 import java.util.Arrays;
 24  
 import java.util.HashMap;
 25  
 
 26  
 /**
 27  
  * This class is used for representation of GUIDs and as a reference list of all
 28  
  * Known GUIDs. <br>
 29  
  *
 30  
  * @author Christian Laireiter
 31  
  */
 32  44
 public final class GUID
 33  
 {
 34  
 
 35  
     /**
 36  
      * This constant defines the GUID for stream chunks describing audio
 37  
      * streams, indicating the the audio stream has no error concealment. <br>
 38  
      */
 39  44
     public final static GUID GUID_AUDIO_ERROR_CONCEALEMENT_ABSENT = new GUID(new int[]{0x40, 0xA4, 0xF1, 0x49, 0xCE, 0x4E, 0xD0, 0x11, 0xA3, 0xAC, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}, "Audio error concealment absent.");
 40  
 
 41  
     /**
 42  
     * This constant defines the GUID for stream chunks describing audio
 43  
     * streams, indicating the the audio stream has interleaved error
 44  
     * concealment. <br>
 45  
     */
 46  44
     public final static GUID GUID_AUDIO_ERROR_CONCEALEMENT_INTERLEAVED = new GUID(new int[]{0x40, 0xA4, 0xF1, 0x49, 0xCE, 0x4E, 0xD0, 0x11, 0xA3, 0xAC, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}, "Interleaved audio error concealment.");
 47  
 
 48  
     /**
 49  
      * This constant stores the GUID indicating that stream type is audio.
 50  
      */
 51  44
     public final static GUID GUID_AUDIOSTREAM = new GUID(new int[]{0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}, " Audio stream");
 52  
 
 53  
     /**
 54  
      * This constant stores the GUID indicating a content branding object.
 55  
      */
 56  44
     public final static GUID GUID_CONTENT_BRANDING = new GUID(new int[]{0xFA, 0xB3, 0x11, 0x22, 0x23, 0xBD, 0xD2, 0x11, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E});
 57  
 
 58  
     /**
 59  
      * This is for the Content Encryption Object 2211B3FB-BD23-11D2-B4B7-00A0C955FC6E,
 60  
      * needs to be little-endian.
 61  
      */
 62  44
     public final static GUID GUID_CONTENT_ENCRYPTION = new GUID(new int[]{0xfb, 0xb3, 0x11, 0x22, 0x23, 0xbd, 0xd2, 0x11, 0xb4, 0xb7, 0x00, 0xa0, 0xc9, 0x55, 0xfc, 0x6e}, "Content Encryption Object");
 63  
 
 64  
     /**
 65  
      * This constant represents the guid for a chunk which contains Title,
 66  
      * author, copyright, description and rating.
 67  
      */
 68  44
     public final static GUID GUID_CONTENTDESCRIPTION = new GUID(new int[]{0x33, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C}, "Content Description");
 69  
 
 70  
     /**
 71  
      * This constant stores the GUID for Encoding-Info chunks.
 72  
      */
 73  44
     public final static GUID GUID_ENCODING = new GUID(new int[]{0x40, 0x52, 0xD1, 0x86, 0x1D, 0x31, 0xD0, 0x11, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6}, "Encoding description");
 74  
 
 75  
     /**
 76  
      * This constant defines the GUID for a WMA "Extended Content Description"
 77  
      * chunk. <br>
 78  
      */
 79  44
     public final static GUID GUID_EXTENDED_CONTENT_DESCRIPTION = new GUID(new int[]{0x40, 0xA4, 0xD0, 0xD2, 0x07, 0xE3, 0xD2, 0x11, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50}, "Extended Content Description");
 80  
 
 81  
     /**
 82  
      * GUID of ASF file header.
 83  
      */
 84  44
     public final static GUID GUID_FILE = new GUID(new int[]{0xA1, 0xDC, 0xAB, 0x8C, 0x47, 0xA9, 0xCF, 0x11, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}, "File header");
 85  
 
 86  
     /**
 87  
      * This constant defines the GUID of a asf header chunk.
 88  
      */
 89  44
     public final static GUID GUID_HEADER = new GUID(new int[]{0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c}, "Asf header");
 90  
 
 91  
     /**
 92  
      * This constant stores a GUID whose functionality is unknown.
 93  
      */
 94  44
     public final static GUID GUID_HEADER_EXTENSION = new GUID(new int[]{0xB5, 0x03, 0xBF, 0x5F, 0x2E, 0xA9, 0xCF, 0x11, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}, "Header Extension");
 95  
 
 96  
     /**
 97  
      * This constant stores the length of GUIDs used with ASF streams. <br>
 98  
      */
 99  
     public final static int GUID_LENGTH = 16;
 100  
 
 101  
     /**
 102  
      * This constant stores the GUID indicating a stream object.
 103  
      */
 104  44
     public final static GUID GUID_STREAM = new GUID(new int[]{0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65}, "Stream");
 105  
 
 106  
     /**
 107  
      * This constant stores a GUID indicating a "stream bitrate properties"
 108  
      * chunk.
 109  
      */
 110  44
     public final static GUID GUID_STREAM_BITRATE_PROPERTIES = new GUID(new int[]{0xCE, 0x75, 0xF8, 0x7B, 0x8D, 0x46, 0xD1, 0x11, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2}, "Stream bitrate properties");
 111  
 
 112  
     /**
 113  
      * This map is used, to get the description of a GUID instance, which has been created by
 114  
      * reading.<br>
 115  
      * The map comparison is done against the {@link GUID#guid} field. But only
 116  
      * the {@link #KNOWN_GUIDS} have a description set.  
 117  
      */
 118  
     private final static HashMap<GUID, GUID> GUID_TO_CONFIGURED;
 119  
 
 120  
     /**
 121  
         * This constant represents a GUID implementation which can be used for generic implementations, which have
 122  
         * to provide a GUID, but do not really require a specific GUID to work.
 123  
         */
 124  44
     public final static GUID GUID_UNSPECIFIED = new GUID(new int[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "Unspecified");
 125  
 
 126  
     /**
 127  
      * This constant stores the GUID indicating that stream type is video.
 128  
      */
 129  44
     public final static GUID GUID_VIDEOSTREAM = new GUID(new int[]{0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B}, "Video stream");
 130  
 
 131  
     /**
 132  
      * This field stores all known GUIDs.
 133  
      */
 134  
     public final static GUID[] KNOWN_GUIDS;
 135  
 
 136  
     /**
 137  
      * This constant stores the GUID for a &quot;script command object&quot;.<br>
 138  
      */
 139  44
     public final static GUID SCRIPT_COMMAND_OBJECT = new GUID(new int[]{0x30, 0x1a, 0xfb, 0x1e, 0x62, 0x0b, 0xd0, 0x11, 0xa3, 0x9b, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6}, "Script Command Object");
 140  
 
 141  
     static
 142  
     {
 143  44
         KNOWN_GUIDS = new GUID[]{GUID_AUDIO_ERROR_CONCEALEMENT_ABSENT, GUID_AUDIO_ERROR_CONCEALEMENT_INTERLEAVED, GUID_CONTENTDESCRIPTION, GUID_AUDIOSTREAM, GUID_ENCODING, GUID_FILE, GUID_HEADER, GUID_STREAM, GUID_EXTENDED_CONTENT_DESCRIPTION, GUID_VIDEOSTREAM, GUID_HEADER_EXTENSION, GUID_STREAM_BITRATE_PROPERTIES, GUID_HEADER_EXTENSION, GUID_STREAM_BITRATE_PROPERTIES, SCRIPT_COMMAND_OBJECT, GUID_CONTENT_ENCRYPTION, GUID_CONTENT_BRANDING, GUID_UNSPECIFIED};
 144  44
         GUID_TO_CONFIGURED = new HashMap<GUID, GUID>(KNOWN_GUIDS.length);
 145  836
         for (final GUID curr : KNOWN_GUIDS)
 146  
         {
 147  792
             GUID_TO_CONFIGURED.put(curr, curr);
 148  
         }
 149  44
     }
 150  
 
 151  
     /**
 152  
      * This method checks if the given <code>value</code> is matching the GUID
 153  
      * specification of ASF streams. <br>
 154  
      *
 155  
      * @param value possible GUID.
 156  
      * @return <code>true</code> if <code>value</code> matches the
 157  
      *         specification of a GUID.
 158  
      */
 159  
     public static boolean assertGUID(int[] value)
 160  
     {
 161  2153
         boolean result = false;
 162  2153
         if (value != null)
 163  
         {
 164  2153
             if (value.length == GUID.GUID_LENGTH)
 165  
             {
 166  2153
                 result = true;
 167  
             }
 168  
         }
 169  2153
         return result;
 170  
     }
 171  
 
 172  
     /**
 173  
      * This method looks up a GUID instance from {@link #KNOWN_GUIDS} which matches
 174  
      * the value of the given GUID.
 175  
      * @param orig GUID to look up.
 176  
      * @return a GUID instance from {@link #KNOWN_GUIDS} if available. <code>null</code> else.
 177  
      */
 178  
     public static GUID getConfigured(final GUID orig)
 179  
     {
 180  0
         assert orig != null;
 181  0
         GUID result = null;
 182  0
         if (orig != null)
 183  
         {
 184  0
             result = GUID_TO_CONFIGURED.get(orig);
 185  
         }
 186  0
         return result;
 187  
     }
 188  
 
 189  
     /**
 190  
      * This method searches a GUID in {@link #KNOWN_GUIDS}which is equal to the
 191  
      * given <code>guid</code> and returns its description. <br>
 192  
      * This method is useful if a guid was read out of a file and no
 193  
      * identification has been done yet.
 194  
      *
 195  
      * @param guid guid, which description is needed.
 196  
      * @return description of the guid if found. Else <code>null</code>
 197  
      */
 198  
     public static String getGuidDescription(GUID guid)
 199  
     {
 200  0
         String result = null;
 201  0
         if (guid == null)
 202  
         {
 203  0
             throw new IllegalArgumentException("Argument must not be null.");
 204  
         }
 205  0
         if (getConfigured(guid) != null)
 206  
         {
 207  0
             result = getConfigured(guid).getDescription();
 208  
         }
 209  0
         return result;
 210  
     }
 211  
 
 212  
     /**
 213  
      * Stores an optionally description of the GUID.
 214  
      */
 215  2153
     private String description = "";
 216  
 
 217  
     /**
 218  
      * An isntance of this class stores the value of the wrapped GUID in this
 219  
      * field. <br>
 220  
      */
 221  2153
     private int[] guid = null;
 222  
 
 223  
 
 224  
     /**
 225  
      * Stores the hash code of the object.<br>
 226  
      * <code>&quot;-1&quot;</code> if not determined yet.
 227  
      */
 228  
     private int hashCode;
 229  
 
 230  
     /**
 231  
      * Creates an instance and assigns given <code>guid</code>.<br>
 232  
      *
 233  
      * @param value GUID, which should be assigned.
 234  
      */
 235  
     public GUID(int[] value)
 236  2153
     {
 237  2153
         setGUID(value);
 238  2153
     }
 239  
 
 240  
     /**
 241  
      * Creates an instance like {@link #GUID(int[])}and sets the optional
 242  
      * description. <br>
 243  
      *
 244  
      * @param value Guid, which should be assigned.
 245  
      * @param desc  Description for the guid.
 246  
      */
 247  
     public GUID(int[] value, String desc)
 248  
     {
 249  660
         this(value);
 250  660
         if (desc == null)
 251  
         {
 252  0
             throw new IllegalArgumentException("Argument must not be null.");
 253  
         }
 254  660
         this.description = desc;
 255  660
     }
 256  
 
 257  
     /**
 258  
      * This method compares two objects. If the given Object is a {@link  GUID},
 259  
      * the stored GUID values are compared. <br>
 260  
      *
 261  
      * @see java.lang.Object#equals(java.lang.Object)
 262  
      */
 263  
     public boolean equals(Object obj)
 264  
     {
 265  16213
         boolean result = false;
 266  16213
         if (obj instanceof GUID)
 267  
         {
 268  16213
             GUID other = (GUID) obj;
 269  16213
             result = Arrays.equals(this.getGUID(), other.getGUID());
 270  
         }
 271  16213
         return result;
 272  
     }
 273  
 
 274  
     /**
 275  
      * This method returns the guid as an array of bytes. <br>
 276  
      *
 277  
      * @return The guid as a byte array.
 278  
      * @see #getGUID()
 279  
      */
 280  
     public byte[] getBytes()
 281  
     {
 282  275
         byte[] result = new byte[this.guid.length];
 283  4675
         for (int i = 0; i < result.length; i++)
 284  
         {
 285  4400
             result[i] = (byte) (this.guid[i] & 0xFF);
 286  
         }
 287  275
         return result;
 288  
     }
 289  
 
 290  
     /**
 291  
      * @return Returns the description.
 292  
      */
 293  
     public String getDescription()
 294  
     {
 295  0
         return description;
 296  
     }
 297  
 
 298  
     /**
 299  
      * This method returns the GUID of this object. <br>
 300  
      *
 301  
      * @return stored GUID.
 302  
      */
 303  
     public int[] getGUID()
 304  
     {
 305  32426
         int[] copy = new int[this.guid.length];
 306  32426
         System.arraycopy(this.guid, 0, copy, 0, this.guid.length);
 307  32426
         return copy;
 308  
     }
 309  
 
 310  
     /**
 311  
      * {@inheritDoc}
 312  
      */
 313  
     @Override
 314  
     public int hashCode()
 315  
     {
 316  5636
         if (this.hashCode == -1)
 317  
         {
 318  0
             int tmp = 0;
 319  0
             for (int curr : getGUID())
 320  
             {
 321  0
                 tmp = tmp * 31 + curr;
 322  
             }
 323  0
             this.hashCode = tmp;
 324  
         }
 325  5636
         return this.hashCode;
 326  
     }
 327  
 
 328  
     /**
 329  
      * This method checks if the currently stored GUID ({@link #guid}) is
 330  
      * correctly filled. <br>
 331  
      *
 332  
      * @return <code>true</code> if it is.
 333  
      */
 334  
     public boolean isValid()
 335  
     {
 336  0
         return assertGUID(getGUID());
 337  
     }
 338  
 
 339  
     /**
 340  
      * This method saves a copy of the given <code>value</code> as the
 341  
      * represented value of this object. <br>
 342  
      * The given value is checked with {@link #assertGUID(int[])}.<br>
 343  
      *
 344  
      * @param value GUID to assign.
 345  
      */
 346  
     private void setGUID(int[] value)
 347  
     {
 348  2153
         if (assertGUID(value))
 349  
         {
 350  2153
             this.guid = new int[GUID_LENGTH];
 351  2153
             System.arraycopy(value, 0, this.guid, 0, GUID_LENGTH);
 352  
         }
 353  
         else
 354  
         {
 355  0
             throw new IllegalArgumentException("The given guid doesn't match the GUID specification.");
 356  
         }
 357  2153
     }
 358  
 
 359  
     /**
 360  
      * This method gives a hex formatted representation of {@link #getGUID()}
 361  
      *
 362  
      * @see java.lang.Object#toString()
 363  
      */
 364  
     public String toString()
 365  
     {
 366  0
         StringBuffer result = new StringBuffer();
 367  0
         String descr = getDescription();
 368  0
         if (Utils.isBlank(descr))
 369  
         {
 370  0
             descr = getGuidDescription(this);
 371  
         }
 372  0
         if (!Utils.isBlank(descr))
 373  
         {
 374  0
             result.append("Description: " + descr + Utils.LINE_SEPARATOR + "   ");
 375  
         } 
 376  0
         for (int i = 0; i < guid.length; i++)
 377  
         {
 378  0
             String tmp = Integer.toHexString(guid[i]);
 379  0
             if (tmp.length() < 2)
 380  
             {
 381  0
                 tmp = "0" + tmp;
 382  
             }
 383  0
             if (i > 0)
 384  
             {
 385  0
                 result.append(", ");
 386  
             }
 387  0
             result.append("0x" + tmp);
 388  
         }
 389  0
         return result.toString();
 390  
     }
 391  
 }