Coverage Report - org.jaudiotagger.audio.asf.data.Chunk
 
Classes in this File Line Coverage Branch Coverage Complexity
Chunk
58%
18/31
50%
7/14
2.111
 
 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  
 
 25  
 /**
 26  
  * This class represents a chunk within ASF streams. <br>
 27  
  * Each chunk starts with a 16byte GUID identifying the type. After that a
 28  
  * number (represented by 8 bytes) follows which shows the size in bytes of the
 29  
  * chunk. Finally there is the data of the chunk.
 30  
  *
 31  
  * @author Christian Laireiter
 32  
  */
 33  
 public class Chunk
 34  
 {
 35  
 
 36  
     /**
 37  
      * The length of current chunk. <br>
 38  
      */
 39  
     protected final BigInteger chunkLength;
 40  
 
 41  
     /**
 42  
      * The guid of represented chunk header.
 43  
      */
 44  
     protected final GUID guid;
 45  
 
 46  
     /**
 47  
      * The position of current header object within file or stream.
 48  
      */
 49  
     protected long position;
 50  
 
 51  
     /**
 52  
      * Creates an instance
 53  
      *
 54  
      * @param headerGuid The GUID of header object.
 55  
      * @param chunkLen   Length of current chunk.
 56  
      */
 57  
     public Chunk(GUID headerGuid, BigInteger chunkLen)
 58  203
     {
 59  203
         if (headerGuid == null)
 60  
         {
 61  0
             throw new IllegalArgumentException("GUID must not be null nor anything else than " + GUID.GUID_LENGTH + " entries long.");
 62  
         }
 63  203
         if (chunkLen == null || chunkLen.compareTo(BigInteger.ZERO) < 0)
 64  
         {
 65  0
             throw new IllegalArgumentException("chunkLen must not be null nor negative.");
 66  
         }
 67  203
         this.guid = headerGuid;
 68  203
         this.chunkLength = chunkLen;
 69  203
     }
 70  
 
 71  
     /**
 72  
      * Creates an instance
 73  
      *
 74  
      * @param headerGuid The GUID of header object.
 75  
      * @param pos        Position of header object within stream or file.
 76  
      * @param chunkLen   Length of current chunk.
 77  
      */
 78  
     public Chunk(GUID headerGuid, long pos, BigInteger chunkLen)
 79  814
     {
 80  814
         if (headerGuid == null)
 81  
         {
 82  0
             throw new IllegalArgumentException("GUID must not be null nor anything else than " + GUID.GUID_LENGTH + " entries long.");
 83  
         }
 84  814
         if (pos < 0)
 85  
         {
 86  0
             throw new IllegalArgumentException("Position of header can't be negative.");
 87  
         }
 88  814
         if (chunkLen == null || chunkLen.compareTo(BigInteger.ZERO) < 0)
 89  
         {
 90  0
             throw new IllegalArgumentException("chunkLen must not be null nor negative.");
 91  
         }
 92  814
         this.guid = headerGuid;
 93  814
         this.position = pos;
 94  814
         this.chunkLength = chunkLen;
 95  814
     }
 96  
 
 97  
     /**
 98  
      * This method returns the End of the current chunk introduced by current
 99  
      * header object.
 100  
      *
 101  
      * @return Position after current chunk.
 102  
      */
 103  
     public long getChunckEnd()
 104  
     {
 105  1974
         return position + chunkLength.longValue();
 106  
     }
 107  
 
 108  
     /**
 109  
      * @return Returns the chunkLength.
 110  
      */
 111  
     public BigInteger getChunkLength()
 112  
     {
 113  0
         return chunkLength;
 114  
     }
 115  
 
 116  
     /**
 117  
      * @return Returns the guid.
 118  
      */
 119  
     public GUID getGuid()
 120  
     {
 121  477
         return guid;
 122  
     }
 123  
 
 124  
     /**
 125  
      * @return Returns the position.
 126  
      */
 127  
     public long getPosition()
 128  
     {
 129  0
         return position;
 130  
     }
 131  
 
 132  
     /**
 133  
      * This method creates a String containing useful information prepared to
 134  
      * be printed on STD-OUT. <br>
 135  
      * This method is intended to be overwritten by inheriting classes.
 136  
      *
 137  
      * @param prefix each line gets this string prepended.
 138  
      *
 139  
      * @return Information of current Chunk Object.
 140  
      */
 141  
     public String prettyPrint(final String prefix)
 142  
     {
 143  0
         StringBuffer result = new StringBuffer();
 144  0
         result.append(prefix + "-> GUID: " + GUID.getGuidDescription(guid) + Utils.LINE_SEPARATOR);
 145  0
         result.append(prefix + "  | : Starts at position: " + getPosition() + Utils.LINE_SEPARATOR);
 146  0
         result.append(prefix + "  | : Last byte at: " + (getChunckEnd() - 1) + Utils.LINE_SEPARATOR);
 147  0
         return result.toString();
 148  
     }
 149  
 
 150  
     /**
 151  
      * Sets the position. 
 152  
      * 
 153  
      * @param pos position to set.
 154  
      */
 155  
     public void setPosition(long pos)
 156  
     {
 157  190
         this.position = pos;
 158  190
     }
 159  
 
 160  
     /**
 161  
      * (overridden)
 162  
      *
 163  
      * @see java.lang.Object#toString()
 164  
      */
 165  
     public String toString()
 166  
     {
 167  0
         return prettyPrint("");
 168  
     }
 169  
 
 170  
 }