示例#1
0
  /**
   * Ecrit la piece donnée dans le fichier temporaire sur le disque
   *
   * @param piece : pièce à écrire
   * @param num : numéros de la pièce
   */
  private synchronized void writePieceTmpFile(byte[] piece, int num) {
    if (num < 0 || num >= this.nbPieces()) {
      throw new IllegalArgumentException();
    }
    if (piece.length > _piecesize) {
      throw new IllegalArgumentException();
    }

    try {
      RandomAccessFile writer_tmp = new RandomAccessFile(this, "rw");
      FileChannel writer = writer_tmp.getChannel();

      int index_piece = ((int) this.length() - this.headerSize()) / _piecesize;

      if (piece.length < _piecesize) {
        piece = Arrays.copyOf(piece, _piecesize);
      }
      Tools.write(writer, 4 + _key.length() + 4 + 4 + 4 * num, index_piece);
      Tools.write(writer, this.headerSize() + _piecesize * index_piece, piece);

      writer.force(true);
      writer_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to write tmp file piece");
      e.printStackTrace();
    }
  }
示例#2
0
  /** Initialise le header du fichier temporaire */
  private void initHeaderTmpFile() {
    try {
      FileOutputStream writer_tmp = new FileOutputStream(this);
      FileChannel writer = writer_tmp.getChannel();

      int offset = 0;

      // Taille de la clef
      Tools.write(writer, offset, _key.length());
      offset += 4;
      // Clef
      Tools.write(writer, offset, _key);
      offset += _key.length();
      // Size
      Tools.write(writer, offset, _size);
      offset += 4;
      // piecesize
      Tools.write(writer, offset, _piecesize);
      offset += 4;
      // Buffermap
      int i;
      for (i = 0; i < this.nbPieces(); i++) {
        Tools.write(writer, offset, -1);
        offset += 4;
      }

      writer.force(true);
      writer_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to create a new tmp file");
      e.printStackTrace();
    }
  }
示例#3
0
  /**
   * Créer le fichier complet correspondant Copie et réassemble les données Supprime le fichier
   * temporaire et renvoi le nouveau fichier
   */
  public synchronized FileShared tmpToComplete() {
    String name = this.getName();
    name = name.substring(0, name.length() - ((String) App.config.get("tmpExtension")).length());

    File complete = new File(App.config.get("downloadDir") + File.separator + name);
    if (complete.exists()) {
      throw new IllegalArgumentException();
    }

    try {
      FileOutputStream writer_tmp = new FileOutputStream(complete, true);
      FileChannel writer = writer_tmp.getChannel();

      int i;
      for (i = 0; i < this.nbPieces(); i++) {
        byte[] piece = this.readPieceTmpFile(i);
        Tools.write(writer, 0, piece);
      }

      writer.force(true);
      writer_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to write complete file");
      e.printStackTrace();
    }

    this.delete();

    return new FileShared(name);
  }