/**
   * Returns true if the code was executed correctly. False if there was an error trying to share
   * the file. If the file was not supposed to be shared, and was not shared, true would still be
   * returned.
   */
  private boolean shareTorrentFile(File torrentFile) {
    if (torrentManager.isDownloadingTorrent(torrentFile)) {
      return true;
    }

    if (!SharingSettings.SHARE_DOWNLOADED_FILES_IN_NON_SHARED_DIRECTORIES.getValue()) {
      return true;
    }

    BTData btData = null;
    FileInputStream torrentInputStream = null;
    try {
      torrentInputStream = new FileInputStream(torrentFile);
      Map<?, ?> torrentFileMap = (Map<?, ?>) Token.parse(torrentInputStream.getChannel());
      btData = new BTDataImpl(torrentFileMap);
    } catch (IOException e) {
      LOG.error("Error reading torrent file: " + torrentFile, e);
      return false;
    } finally {
      FileUtils.close(torrentInputStream);
    }

    if (btData.isPrivate()) {
      gnutellaFileList.remove(torrentFile);
      return true;
    }

    File saveDir = SharingSettings.getSaveDirectory();
    File torrentParent = torrentFile.getParentFile();
    if (torrentParent.equals(saveDir)) {
      // already in saveDir
      gnutellaFileList.add(torrentFile);
      return true;
    }

    final File tFile = getSharedTorrentMetaDataFile(btData);
    if (tFile.equals(torrentFile)) {
      gnutellaFileList.add(tFile);
      return true;
    }

    gnutellaFileList.remove(tFile);
    File backup = null;
    if (tFile.exists()) {
      backup = new File(tFile.getParent(), tFile.getName().concat(".bak"));
      FileUtils.forceRename(tFile, backup);
    }

    if (FileUtils.copy(torrentFile, tFile)) {
      gnutellaFileList.add(tFile);
    } else {
      if (backup != null) {
        // restore backup
        if (FileUtils.forceRename(backup, tFile)) {
          gnutellaFileList.add(tFile);
        }
      }
    }
    return true;
  }
  private static ThemeSetter loadCurrentTheme() {
    ThemeSetter currentTheme = DEFAULT_THEME;

    BufferedReader input = null;

    try {

      File skinsFile = ThemeSettings.SKINS_FILE;

      if (skinsFile.exists()) {
        input = new BufferedReader(new FileReader(skinsFile));
        String name = input.readLine();
        for (int i = 0; i < THEMES.size(); i++) {
          if (THEMES.get(i).getName().equals(name)) {
            currentTheme = THEMES.get(i);
            break;
          }
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      FileUtils.close(input);
    }

    return currentTheme;
  }
  private static void saveCurrentTheme(ThemeSetter theme) throws IOException {
    File skinsFile = ThemeSettings.SKINS_FILE;

    BufferedWriter output = new BufferedWriter(new FileWriter(skinsFile));

    try {

      output.write(theme.getName());

    } finally {
      FileUtils.close(output);
    }
  }
Esempio n. 4
0
 /**
  * A utility method to close Closeable objects (Readers, Writers, Input- and OutputStreams and
  * RandomAccessFiles).
  */
 public static void close(Closeable closeable) {
   FileUtils.close(closeable);
 }