Coverage Report - org.jaudiotagger.audio.generic.Utils
 
Classes in this File Line Coverage Branch Coverage Complexity
Utils
77%
72/94
69%
11/16
1.852
 
 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.generic;
 20  
 
 21  
 import org.jaudiotagger.audio.AudioFile;
 22  
 
 23  
 import java.io.*;
 24  
 import java.nio.ByteBuffer;
 25  
 
 26  
 /**
 27  
  * Contains various frequently used static functions in the different tag
 28  
  * formats
 29  
  *
 30  
  * @author Raphael Slinckx
 31  
  */
 32  0
 public class Utils
 33  
 {
 34  
 
 35  
     /**
 36  
      * Copies the bytes of <code>srd</code> to <code>dst</code> at the
 37  
      * specified offset.
 38  
      *
 39  
      * @param src       The byte to be copied.
 40  
      * @param dst       The array to copy to
 41  
      * @param dstOffset The start offset for the bytes to be copied.
 42  
      */
 43  
     public static void copy(byte[] src, byte[] dst, int dstOffset)
 44  
     {
 45  0
         System.arraycopy(src, 0, dst, dstOffset, src.length);
 46  0
     }
 47  
 
 48  
 
 49  
     /**
 50  
      * Returns {@link String#getBytes()}.<br>
 51  
      *
 52  
      * @param s The String to call, decode bytes using the specfied charset
 53  
      * @return The bytes.
 54  
      */
 55  
     public static byte[] getDefaultBytes(String s, String charSet)
 56  
     {
 57  
         try
 58  
         {
 59  3779
             return s.getBytes(charSet);
 60  
         }
 61  0
         catch (UnsupportedEncodingException uee)
 62  
         {
 63  0
             throw new RuntimeException(uee);
 64  
         }
 65  
 
 66  
     }
 67  
 
 68  
     /*
 69  
       * Returns the extension of the given file.
 70  
       * The extension is empty if there is no extension
 71  
       * The extension is the string after the last "."
 72  
       *
 73  
       * @param f The file whose extension is requested
 74  
       * @return The extension of the given file
 75  
       */
 76  
     public static String getExtension(File f)
 77  
     {
 78  475
         String name = f.getName().toLowerCase();
 79  475
         int i = name.lastIndexOf(".");
 80  475
         if (i == -1)
 81  
         {
 82  0
             return "";
 83  
         }
 84  
 
 85  475
         return name.substring(i + 1);
 86  
     }
 87  
 
 88  
 
 89  
     /*
 90  
     * Computes a number whereby the 1st byte is the least signifcant and the last
 91  
     * byte is the most significant.
 92  
     *
 93  
     * @param b The byte array @param start The starting offset in b
 94  
     * (b[offset]). The less significant byte @param end The end index
 95  
     * (included) in b (b[end]). The most significant byte @return a long number
 96  
     * represented by the byte sequence.
 97  
     *
 98  
     * So if storing a number which only requires one byte it will be stored in the first
 99  
     * byte.
 100  
     */
 101  
     public static long getLongLE(ByteBuffer b, int start, int end)
 102  
     {
 103  13743
         long number = 0;
 104  68637
         for (int i = 0; i < (end - start + 1); i++)
 105  
         {
 106  54894
             number += ((b.get(start + i) & 0xFF) << i * 8);
 107  
         }
 108  
 
 109  13743
         return number;
 110  
     }
 111  
 
 112  
     /*
 113  
      * Computes a number whereby the 1st byte is the most significant and the last
 114  
      * byte is the least significant.
 115  
      *
 116  
      * So if storing a number which only requires one byte it will be stored in the last
 117  
      * byte.
 118  
      */
 119  
     public static long getLongBE(ByteBuffer b, int start, int end)
 120  
     {
 121  30813
         int number = 0;
 122  148487
         for (int i = 0; i < (end - start + 1); i++)
 123  
         {
 124  117674
             number += ((b.get(end - i) & 0xFF) << i * 8);
 125  
         }
 126  
 
 127  30813
         return number;
 128  
     }
 129  
 
 130  
     public static int getIntLE(byte[] b)
 131  
     {
 132  462
         return (int) getLongLE(ByteBuffer.wrap(b), 0, b.length - 1);
 133  
     }
 134  
 
 135  
     /*
 136  
       * same as above, but returns an int instead of a long @param b The byte
 137  
       * array @param start The starting offset in b (b[offset]). The less
 138  
       * significant byte @param end The end index (included) in b (b[end]). The
 139  
       * most significant byte @return a int number represented by the byte
 140  
       * sequence.
 141  
       */
 142  
     public static int getIntLE(byte[] b, int start, int end)
 143  
     {
 144  13281
         return (int) getLongLE(ByteBuffer.wrap(b), start, end);
 145  
     }
 146  
 
 147  
     public static int getIntBE(byte[] b, int start, int end)
 148  
     {
 149  19626
         return (int) getLongBE(ByteBuffer.wrap(b), start, end);
 150  
     }
 151  
 
 152  
     public static int getIntBE(ByteBuffer b, int start, int end)
 153  
     {
 154  11187
         return (int) getLongBE(b, start, end);
 155  
     }
 156  
 
 157  
     public static short getShortBE(ByteBuffer b, int start, int end)
 158  
     {
 159  541
         return (short) getIntBE(b, start, end);
 160  
     }
 161  
 
 162  
     /**
 163  
      * Convert int to byte representation - Big Endian (as used by mp4)
 164  
      *
 165  
      * @param size
 166  
      * @return byte represenetation
 167  
      */
 168  
     public static byte[] getSizeBEInt32(int size)
 169  
     {
 170  7133
         byte[] b = new byte[4];
 171  7133
         b[0] = (byte) ((size >> 24) & 0xFF);
 172  7133
         b[1] = (byte) ((size >> 16) & 0xFF);
 173  7133
         b[2] = (byte) ((size >> 8) & 0xFF);
 174  7133
         b[3] = (byte) (size & 0xFF);
 175  7133
         return b;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Convert short to byte representation - Big Endian (as used by mp4)
 180  
      *
 181  
      * @param size
 182  
      * @return byte represenetation
 183  
      */
 184  
     public static byte[] getSizeBEInt16(short size)
 185  
     {
 186  246
         byte[] b = new byte[2];
 187  246
         b[0] = (byte) ((size >> 8) & 0xFF);
 188  246
         b[1] = (byte) (size & 0xFF);
 189  246
         return b;
 190  
     }
 191  
 
 192  
 
 193  
     /**
 194  
      * Convert int to byte representation - Little Endian (as used by ogg vorbis)
 195  
      *
 196  
      * @param size
 197  
      * @return byte represenetation
 198  
      */
 199  
     public static byte[] getSizeLEInt32(int size)
 200  
     {
 201  78
         byte[] b = new byte[4];
 202  78
         b[0] = (byte) (size & 0xff);
 203  78
         b[1] = (byte) ((size >>> 8) & 0xffL);
 204  78
         b[2] = (byte) ((size >>> 16) & 0xffL);
 205  78
         b[3] = (byte) ((size >>> 24) & 0xffL);
 206  78
         return b;
 207  
     }
 208  
 
 209  
     /**
 210  
      * Create String starting from offset upto length using encoding
 211  
      *
 212  
      * @param b
 213  
      * @param offset
 214  
      * @param length
 215  
      * @param encoding
 216  
      * @return
 217  
      * @throws UnsupportedEncodingException
 218  
      */
 219  
     public static String getString(byte[] b, int offset, int length, String encoding)
 220  
     {
 221  
         try
 222  
         {
 223  20169
             return new String(b, offset, length, encoding);
 224  
         }
 225  0
         catch (UnsupportedEncodingException ue)
 226  
         {
 227  
             //Shouldnt have to worry about this exception as should only be calling with well defined charsets
 228  0
             throw new RuntimeException(ue);
 229  
         }
 230  
     }
 231  
 
 232  
     /**
 233  
      * Create String offset from position by offset upto length using encoding, and position of buffer
 234  
      * is moved to after position + offset + length
 235  
      *
 236  
      * @param buffer
 237  
      * @param offset
 238  
      * @param length
 239  
      * @param encoding
 240  
      * @return
 241  
      */
 242  
     public static String getString(ByteBuffer buffer, int offset, int length, String encoding)
 243  
     {
 244  4964
         byte[] b = new byte[length];
 245  4964
         buffer.position(buffer.position() + offset);
 246  4964
         buffer.get(b);
 247  
         try
 248  
         {
 249  4964
             return new String(b, 0, length, encoding);
 250  
         }
 251  0
         catch (UnsupportedEncodingException uee)
 252  
         {
 253  
             //TODO, will we ever use unsupported encodings
 254  0
             throw new RuntimeException(uee);
 255  
         }
 256  
     }
 257  
 
 258  
     /*
 259  
       * Tries to convert a string into an UTF8 array of bytes If the conversion
 260  
       * fails, return the string converted with the default encoding.
 261  
       *
 262  
       * @param s The string to convert @return The byte array representation of
 263  
       * this string in UTF8 encoding
 264  
       */
 265  
     public static byte[] getUTF8Bytes(String s) throws UnsupportedEncodingException
 266  
     {
 267  76
         return s.getBytes("UTF-8");
 268  
     }
 269  
 
 270  
     /**
 271  
      * Overflow checking since java can't handle unsigned numbers.
 272  
      */
 273  
     public static int readUint32AsInt(DataInput di) throws IOException
 274  
     {
 275  98
         final long l = readUint32(di);
 276  98
         if (l > Integer.MAX_VALUE)
 277  
         {
 278  0
             throw new IOException("uint32 value read overflows int");
 279  
         }
 280  98
         return (int) l;
 281  
     }
 282  
 
 283  
     public static long readUint32(DataInput di) throws IOException
 284  
     {
 285  202
         final byte[] buf8 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 286  202
         di.readFully(buf8, 4, 4);
 287  202
         final long l = ByteBuffer.wrap(buf8).getLong();
 288  202
         return l;
 289  
     }
 290  
 
 291  
     public static int readUint16(DataInput di) throws IOException
 292  
     {
 293  91
         final byte[] buf = {0x00, 0x00, 0x00, 0x00};
 294  91
         di.readFully(buf, 2, 2);
 295  91
         final int i = ByteBuffer.wrap(buf).getInt();
 296  91
         return i;
 297  
     }
 298  
 
 299  
     public static String readString(DataInput di, int charsToRead) throws IOException
 300  
     {
 301  137
         final byte[] buf = new byte[charsToRead];
 302  137
         di.readFully(buf);
 303  137
         return new String(buf);
 304  
     }
 305  
 
 306  
     public static long readUInt64(ByteBuffer b) 
 307  
     {
 308  0
         long result = 0;
 309  0
         result += (readUBEInt32(b) << 32);
 310  0
         result += readUBEInt32(b);
 311  0
         return result;
 312  
     }
 313  
 
 314  
     public static int readUBEInt32(ByteBuffer b)
 315  
     {
 316  64
         int result = 0;
 317  64
         result += readUBEInt16(b) << 16;
 318  64
         result += readUBEInt16(b);
 319  64
         return result;
 320  
     }
 321  
 
 322  
     public static int readUBEInt24(ByteBuffer b)
 323  
     {
 324  0
         int result = 0;
 325  0
         result += readUBEInt16(b) << 16;
 326  0
         result += readUInt8(b);
 327  0
         return result;
 328  
     }
 329  
 
 330  
     public static int readUBEInt16(ByteBuffer b)
 331  
     {
 332  144
         int result = 0;
 333  144
         result += readUInt8(b) << 8;
 334  144
         result += readUInt8(b);
 335  144
         return result;
 336  
     }
 337  
 
 338  
     public static int readUInt8(ByteBuffer b)
 339  
     {
 340  384
         return read(b);
 341  
     }
 342  
       
 343  
 
 344  
     public static int read(ByteBuffer b)
 345  
     {
 346  384
         int result = (b.get() & 0xFF);
 347  384
         return result;
 348  
     }
 349  
 
 350  
      /**
 351  
      *
 352  
      * @param file
 353  
      * @return filename with audioformat seperator stripped of, lengthened to ensure not too small for calid tempfile
 354  
      * creation.
 355  
      */
 356  
     public static String getMinBaseFilenameAllowedForTempFile(File file)
 357  
     {
 358  126
         String s = AudioFile.getBaseFilename(file);
 359  126
         if(s.length()>=3)
 360  
         {
 361  125
             return s;
 362  
         }
 363  1
         if(s.length()==1)
 364  
         {
 365  0
             return s + "000";
 366  
         }
 367  1
         else if(s.length()==1)
 368  
         {
 369  0
             return s + "00";
 370  
         }
 371  1
         else if(s.length()==2)
 372  
         {
 373  1
             return s + "0";
 374  
         }
 375  0
         return s;
 376  
     }
 377  
 }