コード例 #1
0
  protected static long getTorrentDataSizeFromFileOrDirSupport(File file) {
    String name = file.getName();

    if (name.equals(".") || name.equals("..")) {

      return (0);
    }

    if (!file.exists()) {

      return (0);
    }

    if (file.isFile()) {

      return (file.length());

    } else {

      File[] dir_files = file.listFiles();

      long length = 0;

      for (int i = 0; i < dir_files.length; i++) {

        length += getTorrentDataSizeFromFileOrDirSupport(dir_files[i]);
      }

      return (length);
    }
  }
コード例 #2
0
  protected long getTotalFileSizeSupport(File file) throws TOTorrentException {

    String name = file.getName();

    /////////////////////////////////////////

    /////////////////////////////////////

    if (name.equals(".") || name.equals("..")) {

      return (0);
    }

    if (!file.exists()) {

      throw (new TOTorrentException(
          "TOTorrentCreate: file '" + file.getName() + "' doesn't exist",
          TOTorrentException.RT_FILE_NOT_FOUND));
    }

    if (file.isFile()) {

      if (!ignoreFile(name)) {

        total_file_count++;

        return (file.length());

      } else {

        return (0);
      }
    } else {

      File[] dir_files = file.listFiles();

      if (dir_files == null) {

        throw (new TOTorrentException(
            "TOTorrentCreate: directory '"
                + file.getAbsolutePath()
                + "' returned error when listing files in it",
            TOTorrentException.RT_FILE_NOT_FOUND));
      }

      long length = 0;

      for (int i = 0; i < dir_files.length; i++) {

        length += getTotalFileSizeSupport(dir_files[i]);
      }

      return (length);
    }
  }
コード例 #3
0
  private void loadExportedParameters() {
    synchronized (exported_parameters) {
      try {
        File parent_dir = new File(SystemProperties.getUserPath());

        File props = new File(parent_dir, "exported_params.properties");

        if (props.exists()) {

          LineNumberReader lnr =
              new LineNumberReader(new InputStreamReader(new FileInputStream(props), "UTF-8"));

          try {
            while (true) {

              String line = lnr.readLine();

              if (line == null) {

                break;
              }

              String[] bits = line.split("=");

              if (bits.length == 2) {

                String key = bits[0].trim();
                String value = bits[1].trim();

                if (key.length() > 0 && value.length() > 0) {

                  imported_parameters.put(key, value);
                }
              }
            }
          } finally {

            lnr.close();
          }
        }
      } catch (Throwable e) {

        e.printStackTrace();
      }
    }

    COConfigurationManager.setIntDefault("instance.port", Constants.INSTANCE_PORT);

    registerExportedParameter("instance.port", "instance.port");
  }
コード例 #4
0
  public String getDirectoryParameter(String parameter) throws IOException {
    String dir = getStringParameter(parameter);

    if (dir.length() > 0) {
      File temp = new File(dir);
      if (!temp.exists()) {
        FileUtil.mkdirs(temp);
      }
      if (!temp.isDirectory()) {
        throw new IOException("Configuration error. This is not a directory: " + dir);
      }
    }

    return dir;
  }
コード例 #5
0
    private Map<String, List<Long>> getContents() {
      if (contents == null) {

        File file = getCacheFile();

        if (file.exists()) {

          System.out.println("Reading cache: " + file);

          contents = FileUtil.readResilientFile(file);

        } else {

          contents = new HashMap<String, List<Long>>();
        }
      }

      return (contents);
    }
コード例 #6
0
  public long[] getTotalUsageInPeriod(Date start_date, Date end_date) {
    synchronized (this) {
      long[] result = new long[STAT_ENTRY_COUNT];

      long start_millis = start_date.getTime();
      long end_millis = end_date.getTime();

      long now = SystemTime.getCurrentTime();

      long now_day = (now / DAY_IN_MILLIS) * DAY_IN_MILLIS;

      if (end_millis > now) {

        end_millis = now;
      }

      long start_day = (start_millis / DAY_IN_MILLIS) * DAY_IN_MILLIS;
      long end_day = (end_millis / DAY_IN_MILLIS) * DAY_IN_MILLIS;

      if (start_day > end_day) {

        return (result);
      }

      long start_offset = start_millis - start_day;

      start_offset = start_offset / MIN_IN_MILLIS;

      boolean offset_cachable = start_offset % 60 == 0;

      System.out.println(
          "start="
              + debug_utc_format.format(start_date)
              + ", end="
              + debug_utc_format.format(end_date)
              + ", offset="
              + start_offset);

      MonthCache month_cache = null;

      for (long this_day = start_day; this_day <= end_day; this_day += DAY_IN_MILLIS) {

        String[] bits = utc_date_format.format(new Date(this_day)).split(",");

        String year_str = bits[0];
        String month_str = bits[1];
        String day_str = bits[2];

        int year = Integer.parseInt(year_str);
        int month = Integer.parseInt(month_str);
        int day = Integer.parseInt(day_str);

        if (month_cache == null || !month_cache.isForMonth(year_str, month_str)) {

          if (month_cache != null && month_cache.isDirty()) {

            month_cache.save();
          }

          month_cache = getMonthCache(year_str, month_str);
        }

        boolean can_cache =
            this_day != now_day
                && (this_day > start_day || (this_day == start_day && offset_cachable))
                && this_day < end_day;

        long cache_offset = this_day == start_day ? start_offset : 0;

        if (can_cache) {

          long[] cached_totals = month_cache.getTotals(day, cache_offset);

          if (cached_totals != null) {

            for (int i = 0; i < cached_totals.length; i++) {

              result[i] += cached_totals[i];
            }

            continue;
          }
        } else {

          if (this_day == now_day) {

            if (day_cache != null) {

              if (day_cache.isForDay(year_str, month_str, day_str)) {

                long[] cached_totals = day_cache.getTotals(cache_offset);

                if (cached_totals != null) {

                  for (int i = 0; i < cached_totals.length; i++) {

                    result[i] += cached_totals[i];
                  }

                  continue;
                }

              } else {

                day_cache = null;
              }
            }
          }
        }

        String current_rel_file =
            bits[0] + File.separator + bits[1] + File.separator + bits[2] + ".dat";

        File stats_file = new File(stats_dir, current_rel_file);

        if (!stats_file.exists()) {

          if (can_cache) {

            month_cache.setTotals(day, cache_offset, new long[0]);
          }
        } else {

          LineNumberReader lnr = null;

          try {
            System.out.println("Reading " + stats_file);

            lnr = new LineNumberReader(new FileReader(stats_file));

            long file_start_time = 0;

            long[] file_totals = null;

            long[] file_result_totals = new long[STAT_ENTRY_COUNT];

            long[] session_start_stats = null;
            long session_start_time = 0;
            long session_time = 0;

            while (true) {

              String line = lnr.readLine();

              if (line == null) {

                break;
              }

              // System.out.println( line );

              String[] fields = line.split(",");

              if (fields.length < 6) {

                continue;
              }

              String first_field = fields[0];

              if (first_field.equals("s")) {

                session_start_time = Long.parseLong(fields[2]) * MIN_IN_MILLIS;

                if (file_totals == null) {

                  file_totals = new long[STAT_ENTRY_COUNT];

                  file_start_time = session_start_time;
                }

                session_time = session_start_time;

                session_start_stats = new long[STAT_ENTRY_COUNT];

                for (int i = 3; i < 9; i++) {

                  session_start_stats[i - 3] = Long.parseLong(fields[i]);
                }
              } else if (session_start_time > 0) {

                session_time += MIN_IN_MILLIS;

                int field_offset = 0;

                if (first_field.equals("e")) {

                  field_offset = 3;
                }

                long[] line_stats = new long[STAT_ENTRY_COUNT];

                for (int i = 0; i < 6; i++) {

                  line_stats[i] = Long.parseLong(fields[i + field_offset]);

                  file_totals[i] += line_stats[i];
                }

                if (session_time >= start_millis && session_time <= end_millis) {

                  for (int i = 0; i < 6; i++) {

                    result[i] += line_stats[i];

                    file_result_totals[i] += line_stats[i];
                  }
                }

                // System.out.println( getString( line_stats ));
              }
            }

            System.out.println(
                "File total: start="
                    + debug_utc_format.format(file_start_time)
                    + ", end="
                    + debug_utc_format.format(session_time)
                    + " - "
                    + getString(file_totals));

            if (can_cache) {

              month_cache.setTotals(day, cache_offset, file_result_totals);

              if (cache_offset != 0) {

                month_cache.setTotals(day, 0, file_totals);
              }
            } else {

              if (this_day == now_day) {

                if (day_cache == null) {

                  System.out.println("Creating day cache");

                  day_cache = new DayCache(year_str, month_str, day_str);
                }

                day_cache.setTotals(cache_offset, file_result_totals);

                if (cache_offset != 0) {

                  day_cache.setTotals(0, file_totals);
                }
              }
            }

          } catch (Throwable e) {

            Debug.out(e);

          } finally {

            if (lnr != null) {

              try {
                lnr.close();

              } catch (Throwable e) {
              }
            }
          }
        }
      }

      if (month_cache != null && month_cache.isDirty()) {

        month_cache.save();
      }

      System.out.println("    -> " + getString(result));

      return (result);
    }
  }