Coverage Report - org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
MetadataBlockHeader
97%
38/39
83%
10/12
1.667
 
 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  328
         ByteBuffer rawdata = ByteBuffer.allocate(HEADER_LENGTH);
 47  328
         int bytesRead = raf.getChannel().read(rawdata);
 48  328
         if (bytesRead < HEADER_LENGTH)
 49  
         {
 50  0
             throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + HEADER_LENGTH);
 51  
         }
 52  328
         rawdata.rewind();
 53  328
         return new MetadataBlockHeader(rawdata);
 54  
     }
 55  
 
 56  
     /**
 57  
      * Construct header by reading bytes
 58  
      *
 59  
      * @param rawdata
 60  
      */
 61  
     public MetadataBlockHeader(ByteBuffer rawdata)
 62  328
     {
 63  328
         isLastBlock = ((rawdata.get(0) & 0x80) >>> 7) == 1;
 64  
 
 65  328
         int type = rawdata.get(0) & 0x7F;
 66  328
         if (type < BlockType.values().length)
 67  
         {
 68  328
             blockType = BlockType.values()[type];
 69  
         }
 70  
 
 71  
 
 72  328
         dataLength = (u(rawdata.get(1)) << 16) + (u(rawdata.get(2)) << 8) + (u(rawdata.get(3)));
 73  
 
 74  328
         bytes = new byte[HEADER_LENGTH];
 75  1640
         for (int i = 0; i < HEADER_LENGTH; i++)
 76  
         {
 77  1312
             bytes[i] = rawdata.get(i);
 78  
         }
 79  
         ;
 80  328
     }
 81  
 
 82  
     /**
 83  
      * Construct a new header in order to write metadatablock to file
 84  
      *
 85  
      * @param isLastBlock
 86  
      * @param blockType
 87  
      * @param dataLength
 88  
      */
 89  
     public MetadataBlockHeader(boolean isLastBlock, BlockType blockType, int dataLength)
 90  48
     {
 91  48
         ByteBuffer rawdata = ByteBuffer.allocate(HEADER_LENGTH);
 92  48
         this.blockType = blockType;
 93  48
         this.isLastBlock = isLastBlock;
 94  48
         this.dataLength = dataLength;
 95  
 
 96  
         byte type;
 97  48
         if (isLastBlock)
 98  
         {
 99  18
             type = (byte) (0x80 | blockType.getId());
 100  
         }
 101  
         else
 102  
         {
 103  30
             type = (byte) blockType.getId();
 104  
         }
 105  48
         rawdata.put(type);
 106  
 
 107  
         //Size is 3Byte BigEndian int
 108  48
         rawdata.put((byte) ((dataLength & 0xFF0000) >>> 16));
 109  48
         rawdata.put((byte) ((dataLength & 0xFF00) >>> 8));
 110  48
         rawdata.put((byte) (dataLength & 0xFF));
 111  
 
 112  48
         bytes = new byte[HEADER_LENGTH];
 113  240
         for (int i = 0; i < HEADER_LENGTH; i++)
 114  
         {
 115  192
             bytes[i] = rawdata.get(i);
 116  
         }
 117  48
     }
 118  
 
 119  
     private int u(int i)
 120  
     {
 121  984
         return i & 0xFF;
 122  
     }
 123  
 
 124  
     public int getDataLength()
 125  
     {
 126  448
         return dataLength;
 127  
     }
 128  
 
 129  
     public BlockType getBlockType()
 130  
     {
 131  328
         return blockType;
 132  
     }
 133  
 
 134  
     public boolean isLastBlock()
 135  
     {
 136  328
         return isLastBlock;
 137  
     }
 138  
 
 139  
     public byte[] getBytesWithoutIsLastBlockFlag()
 140  
     {
 141  11
         bytes[0] = (byte) (bytes[0] & 0x7F);
 142  11
         return bytes;
 143  
     }
 144  
 
 145  
     public byte[] getBytes()
 146  
     {
 147  48
         return bytes;
 148  
     }
 149  
 }