Coverage Report - org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
MetadataBlockHeader
97%
39/40
83%
10/12
1.6
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 RaphaĆ«l Slinckx <raphael@slinckx.net>
 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.flac.metadatablock;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.RandomAccessFile;
 23  
 import java.nio.ByteBuffer;
 24  
 
 25  
 /**
 26  
  * Metadata Block Header
 27  
  */
 28  
 public class MetadataBlockHeader
 29  
 {
 30  
     public static final int HEADER_LENGTH = 4;
 31  
 
 32  
     private boolean isLastBlock;
 33  
     private int dataLength;
 34  
     private byte[] bytes;
 35  
     private BlockType blockType;
 36  
 
 37  
     /**
 38  
      * Create header by reading from file
 39  
      *
 40  
      * @param raf
 41  
      * @return
 42  
      * @throws IOException
 43  
      */
 44  
     public static MetadataBlockHeader readHeader(RandomAccessFile raf) throws IOException
 45  
     {
 46  1211
         ByteBuffer rawdata = ByteBuffer.allocate(HEADER_LENGTH);
 47  1211
         int bytesRead = raf.getChannel().read(rawdata);
 48  1211
         if (bytesRead < HEADER_LENGTH)
 49  
         {
 50  0
             throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + HEADER_LENGTH);
 51  
         }
 52  1211
         rawdata.rewind();
 53  1211
         return new MetadataBlockHeader(rawdata);
 54  
     }
 55  
 
 56  
     public String toString()
 57  
     {
 58  425
         return "BlockType:"+blockType + " DataLength:"+dataLength + " isLastBlock:"+isLastBlock;
 59  
     }
 60  
 
 61  
     /**
 62  
      * Construct header by reading bytes
 63  
      *
 64  
      * @param rawdata
 65  
      */
 66  
     public MetadataBlockHeader(ByteBuffer rawdata)
 67  1211
     {
 68  1211
         isLastBlock = ((rawdata.get(0) & 0x80) >>> 7) == 1;
 69  
 
 70  1211
         int type = rawdata.get(0) & 0x7F;
 71  1211
         if (type < BlockType.values().length)
 72  
         {
 73  1211
             blockType = BlockType.values()[type];
 74  
         }
 75  
 
 76  
 
 77  1211
         dataLength = (u(rawdata.get(1)) << 16) + (u(rawdata.get(2)) << 8) + (u(rawdata.get(3)));
 78  
 
 79  1211
         bytes = new byte[HEADER_LENGTH];
 80  6055
         for (int i = 0; i < HEADER_LENGTH; i++)
 81  
         {
 82  4844
             bytes[i] = rawdata.get(i);
 83  
         }
 84  1211
         }
 85  
 
 86  
     /**
 87  
      * Construct a new header in order to write metadatablock to file
 88  
      *
 89  
      * @param isLastBlock
 90  
      * @param blockType
 91  
      * @param dataLength
 92  
      */
 93  
     public MetadataBlockHeader(boolean isLastBlock, BlockType blockType, int dataLength)
 94  180
     {
 95  180
         ByteBuffer rawdata = ByteBuffer.allocate(HEADER_LENGTH);
 96  180
         this.blockType = blockType;
 97  180
         this.isLastBlock = isLastBlock;
 98  180
         this.dataLength = dataLength;
 99  
 
 100  
         byte type;
 101  180
         if (isLastBlock)
 102  
         {
 103  68
             type = (byte) (0x80 | blockType.getId());
 104  
         }
 105  
         else
 106  
         {
 107  112
             type = (byte) blockType.getId();
 108  
         }
 109  180
         rawdata.put(type);
 110  
 
 111  
         //Size is 3Byte BigEndian int
 112  180
         rawdata.put((byte) ((dataLength & 0xFF0000) >>> 16));
 113  180
         rawdata.put((byte) ((dataLength & 0xFF00) >>> 8));
 114  180
         rawdata.put((byte) (dataLength & 0xFF));
 115  
 
 116  180
         bytes = new byte[HEADER_LENGTH];
 117  900
         for (int i = 0; i < HEADER_LENGTH; i++)
 118  
         {
 119  720
             bytes[i] = rawdata.get(i);
 120  
         }
 121  180
     }
 122  
 
 123  
     private int u(int i)
 124  
     {
 125  3633
         return i & 0xFF;
 126  
     }
 127  
 
 128  
     public int getDataLength()
 129  
     {
 130  1694
         return dataLength;
 131  
     }
 132  
 
 133  
     public BlockType getBlockType()
 134  
     {
 135  1485
         return blockType;
 136  
     }
 137  
 
 138  
     public boolean isLastBlock()
 139  
     {
 140  1211
         return isLastBlock;
 141  
     }
 142  
 
 143  
     public byte[] getBytesWithoutIsLastBlockFlag()
 144  
     {
 145  42
         bytes[0] = (byte) (bytes[0] & 0x7F);
 146  42
         return bytes;
 147  
     }
 148  
 
 149  
     public byte[] getBytes()
 150  
     {
 151  180
         return bytes;
 152  
     }
 153  
 }