예제 #1
0
  /**
   * Lis et retourne une piece depuis le fichier temporaire sur le disque (la piece doit exister)
   *
   * @param num : numéros de la piece
   */
  private synchronized byte[] readPieceTmpFile(int num) {
    if (num < 0 || num >= this.nbPieces()) {
      throw new IllegalArgumentException();
    }

    try {
      FileInputStream reader_tmp = new FileInputStream(this);
      FileChannel reader = reader_tmp.getChannel();

      int index_piece = Tools.readInt(reader, 4 + _key.length() + 4 + 4 + 4 * num);
      if (index_piece < 0) {
        throw new IllegalArgumentException();
      }

      int size = _piecesize;
      if (num == this.nbPieces() - 1) {
        size = _size - _piecesize * (this.nbPieces() - 1);
      }

      byte[] piece = Tools.readBytes(reader, this.headerSize() + _piecesize * index_piece, size);
      reader_tmp.close();

      return piece;
    } catch (Exception e) {
      System.out.println("Unable to read tmp file piece");
      e.printStackTrace();
    }
    return new byte[0];
  }
예제 #2
0
  /** Lis le header du fichier temporaire et charge ses informations */
  private void readHeaderTmpFile() {
    try {
      FileInputStream reader_tmp = new FileInputStream(this);
      FileChannel reader = reader_tmp.getChannel();

      int key_size = 0;
      int offset = 0;

      // Taile de le clef
      key_size = Tools.readInt(reader, offset);
      offset += 4;
      // Clef
      _key = Tools.readString(reader, offset, key_size);
      offset += key_size;
      // Size
      _size = Tools.readInt(reader, offset);
      offset += 4;
      // piecesize
      _piecesize = Tools.readInt(reader, offset);
      offset += 4;
      // Buffermap
      _buffermap = new Buffermap(this.nbPieces(), false);
      int i;
      for (i = 0; i < this.nbPieces(); i++) {
        int index = Tools.readInt(reader, offset);
        if (index >= 0) {
          _buffermap.setBit(i, true);
        } else {
          _buffermap.setBit(i, false);
        }
        offset += 4;
      }

      reader_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to read tmp file header");
      e.printStackTrace();
    }
  }