Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program 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
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm.db;
   26   
   27   
   28   import java.io.FileNotFoundException;
   29   import java.io.IOException;
   30   import java.io.File;
   31   import java.io.FileInputStream;
   32   import java.io.FileOutputStream;
   33   
   34   final public class PlaylistMDB extends MDB {
   35   
   36     private int type;
   37     
   38     public PlaylistMDB(String fileName, int type)
   39       throws FileNotFoundException, IOException, DatabaseCorruptException{
   40   
   41       super(fileName);
   42       this.type = type;
   43   
   44       loadExistingDatabase();
   45     }
   46       
   47   
   48     public PlaylistMDB(String fileName, int type, boolean createEmpty)
   49       throws FileNotFoundException, IOException, DatabaseCorruptException {
   50   
   51       super(fileName);
   52       this.type = type;
   53   
   54       if (createEmpty) {
   55         createEmptyDatabase();
   56       } else {
   57         loadExistingDatabase();
   58       }
   59     }
   60     
   61       
   62     private void loadExistingDatabase()
   63       throws FileNotFoundException, IOException, DatabaseCorruptException {
   64       
   65       File file = new File(fileName);
   66       // TODO: check length is longer that max int
   67       int fileLengthInBytes = (int) file.length();
   68       int fileLengthInWords = fileLengthInBytes/2;
   69   
   70       FileInputStream fileStream = new FileInputStream(fileName);
   71   
   72       byte[] bytes = new byte[fileLengthInBytes];
   73   
   74       fileStream.read(bytes);
   75   
   76       parseMDBHeader(bytes);
   77   
   78       // Start parsing the data records
   79       int wordOffset = recordStart.getValue();
   80   
   81       int key = nextKey++;
   82       NullRecord nr = new NullRecord(key);
   83       WordPointer ptr = new WordPointer(wordOffset);
   84       keysToMDBRecordsMap.put(new Integer(key), nr);
   85       keysToWordPointersMap.put(new Integer(key), ptr);
   86       wordOffset += nr.getRecordLength();
   87   
   88       // parse data records
   89       while (wordOffset < fileLengthInWords) {
   90         key = nextKey++;
   91         PlaylistRecord pr = new PlaylistRecord(key, bytes, wordOffset,
   92                                                numAccessKeys(), numExtraInfo());
   93         ptr = new WordPointer(wordOffset);
   94         keysToMDBRecordsMap.put(new Integer(key), pr);
   95         keysToWordPointersMap.put(new Integer(key), ptr);
   96         wordOffset += pr.getRecordLength();
   97       }
   98     }
   99   
  100   
  101     private void createEmptyDatabase()
  102       throws DatabaseCorruptException {
  103         
  104       switch (type) {
  105       case AudioDBBase.DBTYPE_AUDIO: {
  106         attributes = new Word(0x0000);
  107         status = new Word(0x0000);
  108         numOfKeys = new Word(0x0001);
  109         numOfFieldsPerRecord = new Word(0x0001);
  110         recordStart = new WordPointer(0x0000002e);
  111         pXIM = new WordPointer(0x00000000);
  112         db_ID = new Word(0x0000);
  113         pDB_Name = new WordPointerToDisplayData(0x00000018);
  114         pParentDBFilename = new WordPointerToWordString(0x0000001f);
  115         pRules = new Rule[1];
  116         pRules[0] = new Rule(new WordPointerToDisplayData(0x00000025),
  117                              new WordPointerToWordString(0x00000000),
  118                              new DisplayData("Playlists"), null);
  119         db_Name = new DisplayData("Playlists");
  120         parentDBFilename = new WordString("audio.mdb");
  121         ximData = null;
  122       
  123         // add null record
  124         int key = nextKey++;
  125         keysToMDBRecordsMap.put(new Integer(key), new NullRecord(key));
  126         keysToWordPointersMap.put(new Integer(key), recordStart);
  127         break;
  128       }
  129   
  130       case AudioDBBase.DBTYPE_PCAUDIO: {
  131         attributes = new Word(0x0000);
  132         status = new Word(0x0000);
  133         numOfKeys = new Word(0x0001);
  134         numOfFieldsPerRecord = new Word(0x0001);
  135         recordStart = new WordPointer(0x0000002e);
  136         pXIM = new WordPointer(0x00000000);
  137         db_ID = new Word(0x0000);
  138         pDB_Name = new WordPointerToDisplayData(0x00000018);
  139         pParentDBFilename = new WordPointerToWordString(0x0000001f);
  140         pRules = new Rule[1];
  141         pRules[0] = new Rule(new WordPointerToDisplayData(0x00000025),
  142                              new WordPointerToWordString(0x00000000),
  143                              new DisplayData("Playlists"), null);
  144         db_Name = new DisplayData("Playlists");
  145         parentDBFilename = new WordString("audio.mdb");
  146         ximData = null;
  147       
  148         // add null record
  149         int key = nextKey++;
  150         NullRecord nr = new NullRecord(key);
  151         keysToMDBRecordsMap.put(new Integer(key), nr);
  152         keysToWordPointersMap.put(new Integer(key), recordStart);
  153         WordPointer nextPtr =
  154           new WordPointer(recordStart.getValue() + nr.getRecordLength());
  155         key = nextKey++;
  156         keysToMDBRecordsMap.put(new Integer(key),
  157                                 new PlaylistRecord(key, "!My Mix"));
  158         keysToWordPointersMap.put(new Integer(key), nextPtr);
  159         break;
  160       }
  161       }
  162     }
  163   
  164   
  165     /**
  166      * Writes this MDB to disk.
  167      *
  168      * @exception IOException it an I/O error occurs during writing
  169      * @exception DatabaseInternalCorruptionException if the Internal
  170      * database is corrupted
  171      */
  172     void write() throws IOException, DatabaseInternalCorruptionException  {
  173       File file = new File(fileName);
  174       File parentDir = new File(file.getParent());
  175       parentDir.mkdirs();
  176       NIOFile fileOutput = new NIOFile(fileName);
  177       super.write(fileOutput);
  178       fileOutput.write();
  179     }
  180   
  181   
  182     /**
  183      * Creates a new playlist.  The key of the new playlist is returned.
  184      *
  185      * @param name The name of this new playlist.
  186      * @param playlistSAI The SAI for playlists.
  187      * @param playlistPAI The PAI for playlists.
  188      * @return The key of the new playlist.
  189      */
  190     int createPlaylist(String name, PlaylistSAI playlistSAI, PAI playlistPAI) {
  191       int key = nextKey++;
  192       PlaylistRecord rec = new PlaylistRecord(key, name);
  193       keysToMDBRecordsMap.put(new Integer(key), rec);
  194       keysToWordPointersMap = null;
  195       int paiKey = playlistPAI.createNewRecord();
  196       playlistSAI.addNewRecord(key, paiKey);
  197       return key;
  198     }
  199   
  200   
  201     void renamePlaylist(String newName, int playlistKey) {
  202       PlaylistRecord pr =
  203         (PlaylistRecord)keysToMDBRecordsMap.get(new Integer(playlistKey));
  204       pr.setName(newName);
  205     }
  206   }
  207   

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » db » [javadoc | source]