Coverage Report - org.jaudiotagger.test.MergeID3AndMP3Files
 
Classes in this File Line Coverage Branch Coverage Complexity
MergeID3AndMP3Files
0%
0/79
0%
0/24
0
MergeID3AndMP3Files$DirFilter
0%
0/3
N/A
0
MergeID3AndMP3Files$MP3FileFilter
0%
0/7
0%
0/6
0
 
 1  
 package org.jaudiotagger.test;
 2  
 
 3  
 import java.io.*;
 4  
 import java.text.DateFormat;
 5  
 import java.util.Date;
 6  
 
 7  
 /**
 8  
  * Simple class that will attempt to recusively read all files within a directory ending in .mp3 (but
 9  
  * only actually expected to contain id3 tags), merge them with a given mp3 and write them to the provided
 10  
  * output folder (which must already exist)
 11  
  */
 12  0
 public class MergeID3AndMP3Files
 13  
 {
 14  
 
 15  
     public static void main(final String[] args)
 16  
     {
 17  0
         MergeID3AndMP3Files test = new MergeID3AndMP3Files();
 18  
 
 19  0
         if (args.length == 0)
 20  
         {
 21  0
             System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File");
 22  0
             System.err.println("      You must enter the from dir,outputdir and the mp3file to append");
 23  0
             System.exit(1);
 24  
         }
 25  0
         else if (args.length != 3)
 26  
         {
 27  0
             System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File");
 28  0
             System.err.println("      Only three parameters accepted");
 29  0
             System.exit(1);
 30  
         }
 31  0
         File rootDir = new File(args[0]);
 32  0
         if (!rootDir.isDirectory())
 33  
         {
 34  0
             System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File");
 35  0
             System.err.println("      Directory " + args[0] + " could not be found");
 36  0
             System.exit(1);
 37  
         }
 38  
 
 39  
 
 40  0
         File toDir = new File(args[1]);
 41  0
         if (!rootDir.isDirectory())
 42  
         {
 43  0
             System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File");
 44  0
             System.err.println("      Directory " + args[1] + " could not be found");
 45  0
             System.exit(1);
 46  
         }
 47  
 
 48  0
         File mp3File = new File(args[2]);
 49  0
         if (!mp3File.isFile())
 50  
         {
 51  0
             System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File");
 52  0
             System.err.println("      Mp3File " + args[2] + " could not be found");
 53  0
             System.exit(1);
 54  
         }
 55  
 
 56  0
         Date start = new Date();
 57  0
         System.out.println("Started to merge from:" + rootDir.getPath() + " at " + DateFormat.getTimeInstance().format(start));
 58  0
         test.scanSingleDir(rootDir, toDir, mp3File);
 59  0
         Date finish = new Date();
 60  0
         System.out.println("Finished to merge from:" + rootDir.getPath() + DateFormat.getTimeInstance().format(finish));
 61  0
         System.out.println("Attempted  to merge:" + MergeID3AndMP3Files.count);
 62  0
         System.out.println("Successful to merge:" + (MergeID3AndMP3Files.count - MergeID3AndMP3Files.failed));
 63  0
         System.out.println("Failed     to merge:" + MergeID3AndMP3Files.failed);
 64  
 
 65  0
     }
 66  
 
 67  
 
 68  0
     private static int count = 0;
 69  0
     private static int failed = 0;
 70  
 
 71  
     /**
 72  
      * Recursive function to scan directory
 73  
      */
 74  
     private void scanSingleDir(final File fromDir, final File toDir, final File mp3File)
 75  
     {
 76  
 
 77  0
         final File[] audioFiles = fromDir.listFiles(new MergeID3AndMP3Files.MP3FileFilter());
 78  0
         if (audioFiles.length > 0)
 79  
         {
 80  0
             for (File audioFile : audioFiles)
 81  
             {
 82  0
                 MergeID3AndMP3Files.count++;
 83  
 
 84  
                 try
 85  
                 {
 86  0
                     copyAudioToTmp(toDir, audioFile, mp3File);
 87  
                 }
 88  0
                 catch (Throwable t)
 89  
                 {
 90  0
                     System.err.println("Unable to merge record:" + MergeID3AndMP3Files.count + ":" + mp3File.getPath());
 91  0
                     MergeID3AndMP3Files.failed++;
 92  0
                     t.printStackTrace();
 93  0
                 }
 94  
             }
 95  
         }
 96  
 
 97  0
         final File[] audioFileDirs = fromDir.listFiles(new MergeID3AndMP3Files.DirFilter());
 98  0
         if (audioFileDirs.length > 0)
 99  
         {
 100  0
             for (File audioFileDir : audioFileDirs)
 101  
             {
 102  0
                 scanSingleDir(audioFileDir, new File(toDir, audioFileDir.getName()), mp3File);
 103  
             }
 104  
         }
 105  0
     }
 106  
 
 107  
     final class MP3FileFilter extends javax.swing.filechooser.FileFilter implements java.io.FileFilter
 108  
     {
 109  
 
 110  
         /**
 111  
          * allows Directories
 112  
          */
 113  
         private final boolean allowDirectories;
 114  
 
 115  
         /**
 116  
          * Create a default MP3FileFilter.  The allowDirectories field will
 117  
          * default to false.
 118  
          */
 119  
         public MP3FileFilter()
 120  
         {
 121  0
             this(false);
 122  0
         }
 123  
 
 124  
         /**
 125  
          * Create an MP3FileFilter.  If allowDirectories is true, then this filter
 126  
          * will accept directories as well as mp3 files.  If it is false then
 127  
          * only mp3 files will be accepted.
 128  
          *
 129  
          * @param allowDirectories whether or not to accept directories
 130  
          */
 131  
         private MP3FileFilter(final boolean allowDirectories)
 132  0
         {
 133  0
             this.allowDirectories = allowDirectories;
 134  0
         }
 135  
 
 136  
         /**
 137  
          * Determines whether or not the file is an mp3 file.  If the file is
 138  
          * a directory, whether or not is accepted depends upon the
 139  
          * allowDirectories flag passed to the constructor.
 140  
          *
 141  
          * @param file the file to test
 142  
          * @return true if this file or directory should be accepted
 143  
          */
 144  
         public final boolean accept(final File file)
 145  
         {
 146  0
             return (((file.getName()).toLowerCase().endsWith(".mp3")) || (file.isDirectory() && (this.allowDirectories == true)));
 147  
         }
 148  
 
 149  
         /**
 150  
          * Returns the Name of the Filter for use in the Chooser Dialog
 151  
          *
 152  
          * @return The Description of the Filter
 153  
          */
 154  
         public final String getDescription()
 155  
         {
 156  0
             return new String(".mp3 Files");
 157  
         }
 158  
     }
 159  
 
 160  0
     public final class DirFilter implements java.io.FileFilter
 161  
     {
 162  
         public DirFilter()
 163  0
         {
 164  
 
 165  0
         }
 166  
 
 167  
 
 168  
         /**
 169  
          * Determines whether or not the file is an mp3 file.  If the file is
 170  
          * a directory, whether or not is accepted depends upon the
 171  
          * allowDirectories flag passed to the constructor.
 172  
          *
 173  
          * @param file the file to test
 174  
          * @return true if this file or directory should be accepted
 175  
          */
 176  
         public final boolean accept(final File file)
 177  
         {
 178  0
             return file.isDirectory();
 179  
         }
 180  
 
 181  
         public static final String IDENT = "$Id: MergeID3AndMP3Files.java,v 1.4 2008/07/21 10:46:30 paultaylor Exp $";
 182  
     }
 183  
 
 184  
     public static File copyAudioToTmp(File toDir, File tagFile, File mp3File)
 185  
     {
 186  0
         File outputFile = new File(toDir.getPath(), tagFile.getName());
 187  0
         boolean result = append(tagFile, mp3File, outputFile);
 188  0
         return outputFile;
 189  
     }
 190  
 
 191  
     private static boolean append(File fromFile1, File fromFile2, File toFile)
 192  
     {
 193  
         try
 194  
         {
 195  0
             FileInputStream in = new FileInputStream(fromFile1);
 196  0
             FileInputStream in2 = new FileInputStream(fromFile2);
 197  
 
 198  0
             toFile.getParentFile().mkdirs();
 199  0
             FileOutputStream out = new FileOutputStream(toFile);
 200  0
             BufferedInputStream inBuffer = new BufferedInputStream(in);
 201  0
             BufferedInputStream inBuffer2 = new BufferedInputStream(in2);
 202  0
             BufferedOutputStream outBuffer = new BufferedOutputStream(out);
 203  
 
 204  
             int theByte;
 205  
 
 206  0
             while ((theByte = inBuffer.read()) > -1)
 207  
             {
 208  0
                 outBuffer.write(theByte);
 209  
             }
 210  
 
 211  0
             while ((theByte = inBuffer2.read()) > -1)
 212  
             {
 213  0
                 outBuffer.write(theByte);
 214  
             }
 215  
 
 216  0
             outBuffer.close();
 217  0
             inBuffer.close();
 218  0
             inBuffer2.close();
 219  0
             out.close();
 220  0
             in.close();
 221  0
             in2.close();
 222  
 
 223  
             // cleanupif files are not the same length
 224  0
             if ((fromFile1.length() + fromFile2.length()) != toFile.length())
 225  
             {
 226  0
                 toFile.delete();
 227  
 
 228  0
                 return false;
 229  
             }
 230  
 
 231  0
             return true;
 232  
         }
 233  0
         catch (IOException e)
 234  
         {
 235  0
             e.printStackTrace();
 236  0
             return false;
 237  
         }
 238  
     }
 239  
 }