Package org.eclipse.ecf.protocol.bittorrent.internal.torrent

Examples of org.eclipse.ecf.protocol.bittorrent.internal.torrent.Piece


    int numPieces = torrent.getNumPieces();
    pieces = new Vector(numPieces);
    trackerThread = new TrackerThread();
    states = PieceState.createStates(numPieces);
    for (int i = 0; i < numPieces; i++) {
      pieces.add(new Piece(states[i], i));
    }
    bitfield = new byte[numPieces % 8 != 0 ? (numPieces / 8) + 1
        : (numPieces / 8)];
    hasPiece = new boolean[numPieces];
    pieceAvailability = new int[numPieces];
    priorityPieces = new boolean[numPieces];
    interestedPieces = new boolean[numPieces];
    uninterestedPieces = new boolean[numPieces];
    incompletePieces = new Vector();
    stateListeners = new Vector();
    errorListeners = new Vector();
    pieceListeners = new Vector();
    progressListeners = new Vector();
    hashCheckListeners = new Vector();
    propertiesFile = new File(statePath, hexHash + ".properties"); //$NON-NLS-1$
    if (!propertiesFile.exists()) {
      properties = new Properties();
      properties.setProperty("target", targetFile.getAbsolutePath()); //$NON-NLS-1$
    } else if (properties == null) {
      properties = new Properties();
      properties.load(new FileInputStream(propertiesFile));
      restore(properties);
    } else {
      restore(properties);
    }
    this.properties = properties;
    store();

    String[] filenames = torrent.getFilenames();
    if (filenames.length != 1 && !targetFile.exists()
        && !targetFile.mkdirs()) {
      throw new IOException("The folders needed by this torrent could not be created"); //$NON-NLS-1$
    }
    files = new DataFile[filenames.length];

    fileInitialization(filenames, targetFile);
    for (int i = 0; i < numPieces; i++) {
      Piece piece = (Piece) pieces.get(i);
      piece.setLength(pieceLength);
    }
    ((Piece) pieces.get(numPieces - 1))
        .setLength((int) (total % pieceLength));

    checkFile();
View Full Code Here


      long length = files[i].length();
      if (pieceLen == 0) {
        pieceLen = pieceLength;
      }
      while (pieceLen < length) {
        Piece piece = (Piece) pieces.get(count);
        piece.addFile(files[i], pieceLen);
        count++;
        length -= pieceLen;
        if (pieceLen < pieceLength) {
          pieceLen = pieceLength;
        }
View Full Code Here

   *             state information, performing a hash check, or querying the
   *             tracker
   */
  synchronized void write(int number, int index, byte[] data, int offset,
      int length) throws IOException {
    Piece piece = (Piece) pieces.get(number);
    if (!hasPiece[number] && piece.write(index, data, offset, length)) {
      remaining -= length;
      downloaded += length;
      saveState();
      fireBlockDownloadedEvent(number, index, length);
      if (!incompletePieces.contains(piece)) {
        incompletePieces.add(piece);
      }

      if (piece.isComplete()) {
        incompletePieces.remove(piece);
        checkCompletedPiece(piece, number);
      }
    }
  }
View Full Code Here

  synchronized Piece request(boolean[] peerPieces) {
    if (isCompleted) {
      return null;
    }

    Piece request = null;
    if (!isSelective) {
      request = request(hasPiece, peerPieces);
    } else if (isPrioritizing) {
      request = request(priorityPieces, peerPieces);
      if (request == null) {
View Full Code Here

      return null;
    }

    boolean hasIncompletePiece = false;
    for (int i = 0; i < incompletePieces.size(); i++) {
      Piece piece = (Piece) incompletePieces.get(i);
      if (peerPieces[piece.getNumber()]) {
        hasIncompletePiece = true;
        break;
      }
    }

    if (hasIncompletePiece) {
      if (incompletePieces.size() == 0) {
        return hasPiece == compare ? getRarePiece(peerPieces)
            : getRarePiece(compare, peerPieces);
      }
      Piece piece = (Piece) incompletePieces.get(ConnectionPool.RANDOM
          .nextInt(incompletePieces.size()));
      while (!peerPieces[piece.getNumber()]) {
        if (isCompleted) {
          return null;
        } else if (incompletePieces.size() == 0) {
          return hasPiece == compare ? getRarePiece(peerPieces)
              : getRarePiece(compare, peerPieces);
View Full Code Here

      throw new IllegalArgumentException("The array's size should be " //$NON-NLS-1$
          + this.states.length);
    }
    this.states = states;
    for (int i = 0; i < states.length; i++) {
      Piece piece = (Piece) pieces.get(i);
      if (piece.isComplete()) {
        completedPieces--;
        hasPiece[i] = false;
      }
      remaining += piece.getWritten();
      piece.setState(states[i]);
      int written = piece.getWritten();
      remaining -= written;
      if (written == piece.getLength()) {
        completedPieces++;
        hasPiece[i] = true;
        incompletePieces.remove(piece);
      } else if (written != 0 && !incompletePieces.contains(piece)) {
        incompletePieces.add(piece);
View Full Code Here

          while ((read += channel.read(buffer)) == pieceLength) {
            if (isInterrupted()) {
              cleanup();
              return;
            }
            Piece piece = (Piece) pieces.get(count);
            if (piece.isComplete()) {
              completedPieces--;
              hasPiece[i] = false;
            }
            remaining += piece.getWritten();
            buffer.rewind();
            if (torrent.getPieces()[count]
                .equals(new String(shaDigest.digest(buffer
                    .array()), "ISO-8859-1"))) { //$NON-NLS-1$
              piece.setAsCompleted();
              hasPiece[count] = true;
              completedPieces++;
              remaining -= piece.getLength();
            } else {
              piece.reset();
            }
            incompletePieces.remove(piece);
            fireHashCheckedEvent(count);
            count++;
            read = 0;
          }
        }

        if (read > 0) {
          if (isInterrupted()) {
            cleanup();
            return;
          }
          Piece piece = (Piece) pieces.get(count);
          if (piece.isComplete()) {
            completedPieces--;
            hasPiece[count] = false;
          }
          remaining += piece.getWritten();
          buffer.rewind();
          shaDigest.update(buffer.array(), 0, read);
          if (torrent.getPieces()[count].equals(new String(shaDigest
              .digest(), "ISO-8859-1"))) { //$NON-NLS-1$
            hasPiece[count] = true;
            piece.setAsCompleted();
            completedPieces++;
            remaining -= piece.getLength();
          } else {
            piece.reset();
          }
          incompletePieces.remove(piece);
          fireHashCheckedEvent(count);
        }
View Full Code Here

TOP

Related Classes of org.eclipse.ecf.protocol.bittorrent.internal.torrent.Piece

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.