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.util.TreeMap;
   29   import java.util.Iterator;
   30   import java.io.FileNotFoundException;
   31   import java.io.IOException;
   32   import java.io.File;
   33   import java.io.FileInputStream;
   34   import java.io.FileOutputStream;
   35   
   36   final public class IdedHiSiMDB extends MDB {
   37     
   38     public IdedHiSiMDB(String fileName)
   39       throws FileNotFoundException, IOException, DatabaseCorruptException {
   40   
   41       super(fileName);
   42   
   43       loadExistingDatabase();
   44     }
   45       
   46   
   47     public IdedHiSiMDB(String fileName, boolean createEmpty)
   48       throws FileNotFoundException, IOException, DatabaseCorruptException {
   49   
   50       super(fileName);
   51   
   52       if (createEmpty) {
   53         createEmptyDatabase();
   54       } else {
   55         loadExistingDatabase();
   56       }
   57     }
   58     
   59       
   60     private void loadExistingDatabase()
   61       throws FileNotFoundException, IOException, DatabaseCorruptException {
   62       
   63       File file = new File(fileName);
   64       // TODO: check length is longer that max int
   65       int fileLengthInBytes = (int) file.length();
   66       int fileLengthInWords = fileLengthInBytes/2;
   67   
   68       FileInputStream fileStream = new FileInputStream(fileName);
   69   
   70       byte[] bytes = new byte[fileLengthInBytes];
   71   
   72       fileStream.read(bytes);
   73   
   74       parseMDBHeader(bytes);
   75   
   76       // Start parsing the data records
   77       int wordOffset = recordStart.getValue();
   78   
   79       int key = nextKey++;
   80       NullRecord nr = new NullRecord(key);
   81       WordPointer ptr = new WordPointer(wordOffset);
   82       keysToMDBRecordsMap.put(new Integer(key), nr);
   83       keysToWordPointersMap.put(new Integer(key), ptr);
   84       wordOffset += nr.getRecordLength();
   85       
   86       // parse data records
   87       while (wordOffset < fileLengthInWords) {
   88         key = nextKey++;
   89         IdedHiSiRecord rec = new IdedHiSiRecord(key, bytes, wordOffset,
   90                                                 numAccessKeys(),
   91                                                 numExtraInfo());
   92         ptr = new WordPointer(wordOffset);
   93         keysToMDBRecordsMap.put(new Integer(key), rec);
   94         keysToWordPointersMap.put(new Integer(key), ptr);
   95         wordOffset += rec.getRecordLength();
   96       }
   97     }
   98   
   99     
  100     private void createEmptyDatabase()
  101       throws DatabaseCorruptException {
  102         
  103       attributes = new Word(0x0001);
  104       status = new Word(0x0000);
  105       numOfKeys = new Word(0x0001);
  106       numOfFieldsPerRecord = new Word(0x0009);
  107       recordStart = new WordPointer(0x00000053);
  108       pXIM = new WordPointer(0x00000023);
  109       db_ID = new Word(0x0000);
  110       pDB_Name = new WordPointerToDisplayData(0x00000018);
  111       pParentDBFilename = new WordPointerToWordString(0x00000000);
  112       pRules = new Rule[1];
  113       pRules[0] = new Rule(new WordPointerToDisplayData(0x0000001c),
  114                            new WordPointerToWordString(0x00000000),
  115                            new DisplayData("Identified"), null);
  116       db_Name = new DisplayData("HiSi");
  117       ximData = new byte[]
  118         {0x00, 0x2e,
  119          0x00, 0x04, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
  120          0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
  121          0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00,
  122          0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x00,
  123          0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x3f, (byte)0xff,
  124          0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00,
  125          0x00, 0x00, 0x00, 0x03, 0x49, 0x6e, 0x66, 0x6f,
  126          0x00, 0x00, 0x00, 0x03, 0x50, 0x6c, 0x61, 0x79,
  127          0x00, 0x00, 0x00, 0x07, 0x44, 0x65, 0x6c, 0x65,
  128          0x74, 0x65, 0x20, 0x43, 0x6c, 0x69, 0x70, 0x00,
  129          0x00, 0x00, 0x00, 0x03, 0x45, 0x78, 0x69, 0x74,
  130          0x00, 0x00};
  131   
  132       // add null record
  133       int key = nextKey++;
  134       keysToMDBRecordsMap.put(new Integer(key), new NullRecord(key));
  135       keysToWordPointersMap.put(new Integer(key), recordStart);
  136     }
  137   
  138     
  139     void write() throws IOException, DatabaseInternalCorruptionException {
  140       File file = new File(fileName);
  141       File parentDir = new File(file.getParent());
  142       parentDir.mkdirs();
  143       NIOFile fileOutput = new NIOFile(fileName);
  144       super.write(fileOutput);
  145       fileOutput.write();
  146     }
  147   }

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