Coverage Report - org.jaudiotagger.audio.asf.data.AsfHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
AsfHeader
83%
30/36
75%
12/16
1.571
 
 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.math.BigInteger;
 24  
 import java.nio.charset.Charset;
 25  
 import java.util.HashSet;
 26  
 import java.util.List;
 27  
 import java.util.Set;
 28  
 
 29  
 /**
 30  
  * Each ASF file starts with a so called header. <br>
 31  
  * This header contains other chunks. Each chunk starts with a 16 byte GUID
 32  
  * followed by the length (in bytes) of the chunk (including GUID). The length
 33  
  * number takes 8 bytes and is unsigned. Finally the chunk's data appears. <br>
 34  
  * 
 35  
  * @author Christian Laireiter
 36  
  */
 37  
 public final class AsfHeader extends ChunkContainer {
 38  
     /**
 39  
      * The charset &quot;UTF-16LE&quot; is mandatory for ASF handling.
 40  
      */
 41  4
     public final static Charset ASF_CHARSET = Charset.forName("UTF-16LE"); //$NON-NLS-1$
 42  
 
 43  
     /**
 44  
      * Byte sequence representing the zero term character.
 45  
      */
 46  4
     public final static byte[] ZERO_TERM = { 0, 0 };
 47  
 
 48  
     static {
 49  4
         Set<GUID> MULTI_CHUNKS = new HashSet<GUID>();
 50  4
         MULTI_CHUNKS.add(GUID.GUID_STREAM);
 51  4
     }
 52  
 
 53  
     /**
 54  
      * An ASF header contains multiple chunks. <br>
 55  
      * The count of those is stored here.
 56  
      */
 57  
     private final long chunkCount;
 58  
 
 59  
     /**
 60  
      * Creates an instance.
 61  
      * 
 62  
      * @param pos
 63  
      *            see {@link Chunk#position}
 64  
      * @param chunkLen
 65  
      *            see {@link Chunk#chunkLength}
 66  
      * @param chunkCnt
 67  
      */
 68  
     public AsfHeader(final long pos, final BigInteger chunkLen,
 69  
             final long chunkCnt) {
 70  411
         super(GUID.GUID_HEADER, pos, chunkLen);
 71  411
         this.chunkCount = chunkCnt;
 72  411
     }
 73  
 
 74  
     /**
 75  
      * This method looks for an content description object in this header
 76  
      * instance, if not found there, it tries to get one from a contained ASF
 77  
      * header extension object.
 78  
      * 
 79  
      * @return content description if found, <code>null</code> otherwise.
 80  
      */
 81  
     public ContentDescription findContentDescription() {
 82  20
         ContentDescription result = getContentDescription();
 83  20
         if (result == null && getExtendedHeader() != null) {
 84  8
             result = getExtendedHeader().getContentDescription();
 85  
         }
 86  20
         return result;
 87  
     }
 88  
 
 89  
     /**
 90  
      * This method looks for an extended content description object in this
 91  
      * header instance, if not found there, it tries to get one from a contained
 92  
      * ASF header extension object.
 93  
      * 
 94  
      * @return extended content description if found, <code>null</code>
 95  
      *         otherwise.
 96  
      */
 97  
     public MetadataContainer findExtendedContentDescription() {
 98  218
         MetadataContainer result = getExtendedContentDescription();
 99  218
         if (result == null && getExtendedHeader() != null) {
 100  60
             result = getExtendedHeader().getExtendedContentDescription();
 101  
         }
 102  218
         return result;
 103  
     }
 104  
 
 105  
     /**
 106  
      * This method searches for a metadata container of the given type.<br>
 107  
      * 
 108  
      * @param type
 109  
      *            the type of the container to look up.
 110  
      * @return a container of specified type, of <code>null</code> if not
 111  
      *         contained.
 112  
      */
 113  
     public MetadataContainer findMetadataContainer(final ContainerType type) {
 114  1118
         MetadataContainer result = (MetadataContainer) getFirst(type
 115  
                 .getContainerGUID(), MetadataContainer.class);
 116  1118
         if (result == null) {
 117  790
             result = (MetadataContainer) getExtendedHeader().getFirst(
 118  
                     type.getContainerGUID(), MetadataContainer.class);
 119  
         }
 120  1118
         return result;
 121  
     }
 122  
 
 123  
     /**
 124  
      * This method returns the first audio stream chunk found in the asf file or
 125  
      * stream.
 126  
      * 
 127  
      * @return Returns the audioStreamChunk.
 128  
      */
 129  
     public AudioStreamChunk getAudioStreamChunk() {
 130  1188
         AudioStreamChunk result = null;
 131  1188
         final List<Chunk> streamChunks = assertChunkList(GUID.GUID_STREAM);
 132  2376
         for (int i = 0; i < streamChunks.size() && result == null; i++) {
 133  1188
             if (streamChunks.get(i) instanceof AudioStreamChunk) {
 134  1188
                 result = (AudioStreamChunk) streamChunks.get(i);
 135  
             }
 136  
         }
 137  1188
         return result;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Returns the amount of chunks, when this instance was created.<br>
 142  
      * If chunks have been added, this won't be reflected with this call.<br>
 143  
      * For that use {@link #getChunks()}.
 144  
      * 
 145  
      * @return Chunkcount at instance creation.
 146  
      */
 147  
     public long getChunkCount() {
 148  0
         return this.chunkCount;
 149  
     }
 150  
 
 151  
     /**
 152  
      * @return Returns the contentDescription.
 153  
      */
 154  
     public ContentDescription getContentDescription() {
 155  84
         return (ContentDescription) getFirst(GUID.GUID_CONTENTDESCRIPTION,
 156  
                 ContentDescription.class);
 157  
     }
 158  
 
 159  
     /**
 160  
      * @return Returns the encodingChunk.
 161  
      */
 162  
     public EncodingChunk getEncodingChunk() {
 163  0
         return (EncodingChunk) getFirst(GUID.GUID_ENCODING, EncodingChunk.class);
 164  
     }
 165  
 
 166  
     /**
 167  
      * @return Returns the encodingChunk.
 168  
      */
 169  
     public EncryptionChunk getEncryptionChunk() {
 170  0
         return (EncryptionChunk) getFirst(GUID.GUID_CONTENT_ENCRYPTION,
 171  
                 EncryptionChunk.class);
 172  
     }
 173  
 
 174  
     /**
 175  
      * @return Returns the tagHeader.
 176  
      */
 177  
     public MetadataContainer getExtendedContentDescription() {
 178  278
         return (MetadataContainer) getFirst(
 179  
                 GUID.GUID_EXTENDED_CONTENT_DESCRIPTION, MetadataContainer.class);
 180  
     }
 181  
 
 182  
     /**
 183  
      * @return Returns the extended header.
 184  
      */
 185  
     public AsfExtendedHeader getExtendedHeader() {
 186  1139
         return (AsfExtendedHeader) getFirst(GUID.GUID_HEADER_EXTENSION,
 187  
                 AsfExtendedHeader.class);
 188  
     }
 189  
 
 190  
     /**
 191  
      * @return Returns the fileHeader.
 192  
      */
 193  
     public FileHeader getFileHeader() {
 194  796
         return (FileHeader) getFirst(GUID.GUID_FILE, FileHeader.class);
 195  
     }
 196  
 
 197  
     /**
 198  
      * @return Returns the streamBitratePropertiesChunk.
 199  
      */
 200  
     public StreamBitratePropertiesChunk getStreamBitratePropertiesChunk() {
 201  0
         return (StreamBitratePropertiesChunk) getFirst(
 202  
                 GUID.GUID_STREAM_BITRATE_PROPERTIES,
 203  
                 StreamBitratePropertiesChunk.class);
 204  
     }
 205  
 
 206  
     /**
 207  
      * 
 208  
      * {@inheritDoc}
 209  
      */
 210  
     @Override
 211  
     public String prettyPrint(final String prefix) {
 212  0
         final StringBuilder result = new StringBuilder(super.prettyPrint(prefix,
 213  
                 prefix + "  | : Contains: \"" + getChunkCount() + "\" chunks"
 214  
                         + Utils.LINE_SEPARATOR));
 215  0
         return result.toString();
 216  
     }
 217  
 }