/**
   * This method deletes a data file or resets its free position. this is used only for nio files -
   * not OOo files
   */
  public static void deleteOrResetFreePos(Database database, String filename) {

    ScaledRAFile raFile = null;

    try {
      database.getFileAccess().removeElement(filename);
    } catch (IOException e) {
      database.logger.appLog.logContext(e);
    }

    if (database.isStoredFileAccess()) {
      return;
    }

    if (!database.getFileAccess().isStreamElement(filename)) {
      return;
    }

    try {
      raFile = new ScaledRAFile(filename, false);

      raFile.seek(LONG_FREE_POS_POS);
      raFile.writeLong(INITIAL_FREE_POS);
    } catch (IOException e) {
      database.logger.appLog.logContext(e);
    } finally {
      if (raFile != null) {
        try {
          raFile.close();
        } catch (IOException e) {
          database.logger.appLog.logContext(e);
        }
      }
    }
  }
  HsqlArrayList defrag(Database db, ScaledRAFile sourcenotused, String filename)
      throws IOException, HsqlException {

    Trace.printSystemOut("Defrag Transfer begins");

    HsqlArrayList rootsList = new HsqlArrayList();
    HsqlArrayList tTable = db.getTables();

    // erik        to specify scale;
    ScaledRAFile dest =
        ScaledRAFile.newScaledRAFile(filename + ".new", false, 1, ScaledRAFile.DATA_FILE_RAF);

    // erik        desl.seek(Cache.INITIAL_FREE_POS / cacheFileScale);
    dest.seek(Cache.INITIAL_FREE_POS);

    for (int i = 0, tSize = tTable.size(); i < tSize; i++) {
      Table t = (Table) tTable.get(i);

      if (t.tableType == Table.CACHED_TABLE) {
        int[] rootsArray = writeTableToDataFile(t, dest);

        rootsList.add(rootsArray);
      } else {
        rootsList.add(null);
      }

      Trace.printSystemOut(t.getName().name, " complete");
    }

    // erik        no change
    int pos = (int) dest.getFilePointer();

    // erik        desl.seek(Cache.FREE_POS_POS / cacheFileScale);
    dest.seek(Cache.FREE_POS_POS);
    dest.writeInt(pos);
    dest.close();

    for (int i = 0, size = rootsList.size(); i < size; i++) {
      int[] roots = (int[]) rootsList.get(i);

      if (roots != null) {
        Trace.printSystemOut(org.hsqldb.lib.StringUtil.getList(roots, ",", ""));
      }
    }

    Trace.printSystemOut("Transfer complete: ", stopw.elapsedTime());

    return rootsList;
  }
  public static int getFlags(String filename) throws HsqlException {

    try {
      ScaledRAFile raFile =
          (ScaledRAFile)
              ScaledRAFile.newScaledRAFile(filename, true, ScaledRAFile.DATA_FILE_RAF, null, null);

      raFile.seek(FLAGS_POS);

      int flags = raFile.readInt();

      raFile.close();

      return flags;
    } catch (IOException e) {
      throw Trace.error(Trace.DATA_FILE_ERROR);
    }
  }