Ejemplo n.º 1
0
 public int getNoOfBlocks() {
   int noOfBlocks = (int) getFileSize() / Settings.getBlockSize();
   if (getFileSize() % Settings.getBlockSize() != 0) {
     noOfBlocks++;
   }
   return noOfBlocks;
 }
Ejemplo n.º 2
0
  public File getDownloadTargetFile() throws FileNotFoundException, IOException {
    File file = new File(Settings.getSharedFolder() + getFileName() + Settings.getTmpExtension());

    if (!file.exists() /* && getCompleteFile() == null TODO activate */) {
      file.createNewFile();
      RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
      randomAccessFile.setLength(getFileSize());
      randomAccessFile.close();
    }

    if (file.exists()) return file;

    return null;
  }
Ejemplo n.º 3
0
 public String getRealFileName() {
   if (getFileName().endsWith(Settings.getTmpExtension())) {
     return getFileNameWithoutExtension();
   } else {
     return getFileName();
   }
 }
Ejemplo n.º 4
0
  public String getRouterCommand() {
    ArrayList<String> commandArgs = new ArrayList<String>();
    commandArgs.add(getRealFileName());
    commandArgs.add("" + getFileSize());
    commandArgs.add("" + getNoOfBlocks());
    File statusFile;
    try {
      statusFile = getDownloadStatusFile();

      if (statusFile == null) return "";

      RandomAccessFile randomAccesStatus = new RandomAccessFile(statusFile, "r");
      byte[] content = new byte[(int) randomAccesStatus.length()];
      randomAccesStatus.readFully(content, 0, content.length);
      randomAccesStatus.close();
      String stringContent = new String(content);
      commandArgs.add(stringContent);

      String command = ""; // Settings.getListStartSign();
      for (String s : commandArgs) {
        command += s + Settings.getSplitChar();
      }
      command = command.substring(0, command.length() - 1); // strip last
      // split
      // char
      command += ""; // Settings.getListStopSign();

      return command;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "";
  }
Ejemplo n.º 5
0
  public File getDownloadStatusFile() throws IOException {
    String fileName = Settings.getInfoFolder() + getFileName() + Settings.getInfoExtension();

    File file = new File(fileName);
    if (!file.exists() /* && getCompleteFile() == null TODO activate */) {
      file.createNewFile();
      RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

      for (int i = 1; i <= getNoOfBlocks(); i++) {
        randomAccessFile.write((byte) '0');
      }
      randomAccessFile.close();
    }

    if (file.exists()) return file;

    return null;
  }
Ejemplo n.º 6
0
  public void completeDownload() throws FileNotFoundException, IOException {
    if (Settings.DEBUG) {
      System.out.println("complete download of " + getRealFileName());
    }
    getDownloadStatusFile().delete();

    File newFileName = new File(Settings.getSharedFolder() + getRealFileName());

    // delete if new file already exists
    if (newFileName.exists()) {
      if (Settings.DEBUG) {
        System.out.println("Going to delete downloaded file because target already exsists");
      }
      getDownloadTargetFile().delete();
    }

    getDownloadTargetFile().renameTo(newFileName);

    DownloadController.removeDownload(this);
  }
Ejemplo n.º 7
0
  public File getCompleteFile() {
    File file = new File(Settings.getSharedFolder() + getFileName());
    if (file.exists()) return file;

    return null;
  }
Ejemplo n.º 8
0
 public int getRestSize() {
   return (int) (getFileSize() % Settings.getBlockSize());
 }