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();