Class BlockInfo
BlockInfo represents the current state of a block (typically 16KB) being
downloaded. It tracks which peer is providing the data, how many bytes have been
received, the total block size, the block's state (none/requested/writing/finished),
and how many peers are requesting the same block (useful for parallel downloads).
Understanding Block States:
Blocks progress through multiple states as they're downloaded and written to disk:
- NONE: Block not yet requested (awaiting download)
- REQUESTED: Block requested from peer, awaiting transfer
- WRITING: Block received, queued for disk write
- FINISHED: Block written to disk and hash-verified
Block State Transitions:
NONE │ ├─ (peer requests it) → REQUESTED │ │ │ (data received) → WRITING │ │ │ (written to disk) → FINISHED │ └─ (timeout) → back to NONE (retry)
Monitoring Block Progress with BlockInfo:
// Get information about blocks in incomplete pieces
sm.addListener(new AlertListener() {
public int[] types() {
return new int[] {AlertType.PIECE_INFO.swig()};
}
public void alert(Alert<?> alert) {
PieceInfoAlert a = (PieceInfoAlert) alert;
// Block-level detail: which peer, how many bytes, state
java.util.ArrayList<BlockInfo> blockData = a.getBlockData();
for (BlockInfo block : blockData) {
TcpEndpoint peer = block.peer();
int bytes = block.bytesProgress();
int total = block.blockSize();
BlockInfo.BlockState state = block.state();
System.out.println(\"Block from \" + peer);
System.out.println(\" Progress: \" + bytes + \" / \" + total + \" bytes\");
System.out.println(\" State: \" + state);
System.out.println(\" Peers: \" + block.numPeers());
}
}
});
Block Information Properties:
BlockInfo block = ...; // From PieceInfoAlert
// Which peer is providing this block?
TcpEndpoint peer = block.peer();
System.out.println(\"Downloaded from: \" + peer);
// Download progress (partial blocks show intermediate state)
int received = block.bytesProgress();
int total = block.blockSize();
double progress = (double) received / total * 100;
System.out.println(String.format(\"Progress: %.1f%%\", progress));
// Block size (typically 16KB, or less for last block)
System.out.println(\"Block size: \" + total + \" bytes\");
// Current state
BlockInfo.BlockState state = block.state();
if (state == BlockInfo.BlockState.REQUESTED) {
System.out.println(\"Block is downloading...\");
} else if (state == BlockInfo.BlockState.WRITING) {
System.out.println(\"Block downloaded, writing to disk...\");
} else if (state == BlockInfo.BlockState.FINISHED) {
System.out.println(\"Block complete and verified!\");
}
// How many peers are downloading this block?
// (Parallel downloads of same block to speed up finish)
int peers = block.numPeers();
System.out.println(\"Downloading from \" + peers + \" peer(s)\");
Block State Enum - BlockState:
BlockInfo.BlockState.NONE: - Block not yet requested - Peer field is unspecified - bytesProgress() is zero - Awaiting selection by piece picker BlockInfo.BlockState.REQUESTED: - Block has been requested from a peer - Data transfer is in progress - bytesProgress() shows partial data (0 to blockSize) - Waiting for network delivery BlockInfo.BlockState.WRITING: - Block received completely - Queued for disk write - Hash verification in progress - Short-lived state (usually milliseconds) BlockInfo.BlockState.FINISHED: - Block written to disk - Hash verified (matches piece hash) - Fully part of the download - Stable end-state
Analyzing Download Performance:
sm.addListener(new AlertListener() {
public int[] types() {
return new int[] {AlertType.PIECE_INFO.swig()};
}
public void alert(Alert<?> alert) {
PieceInfoAlert a = (PieceInfoAlert) alert;
java.util.ArrayList<BlockInfo> blocks = a.getBlockData();
// Count blocks in each state
int requested = 0, writing = 0, finished = 0;
double avgProgress = 0;
for (BlockInfo block : blocks) {
switch (block.state()) {
case REQUESTED:
requested++;
avgProgress += (double) block.bytesProgress() / block.blockSize();
break;
case WRITING:
writing++;
break;
case FINISHED:
finished++;
break;
}
}
System.out.println(\"Active downloads (REQUESTED): \" + requested);
System.out.println(\" Average progress: \" +
String.format(\"%.1f%%\", avgProgress / Math.max(requested, 1) * 100));
System.out.println(\"Queued for write (WRITING): \" + writing);
System.out.println(\"Verified (FINISHED): \" + finished);
}
});
Parallel Block Downloads:
At the end of a download, the same block may be requested from multiple peers to speed up completion:
// Near end of torrent - speed up final pieces
for (BlockInfo block : blockData) {
int peers = block.numPeers();
if (peers > 1) {
System.out.println(\"Fast-tracking: \" + peers + \" peers uploading same block\");
// Multiple peers racing to transfer same block
// First one to complete wins, others are cancelled
}
}
Error Handling and Timeouts:
// If a peer stalls while downloading a block:
for (BlockInfo block : blockData) {
if (block.state() == BlockInfo.BlockState.REQUESTED) {
int received = block.bytesProgress();
int total = block.blockSize();
// Stalled: no progress for a while
if (received > 0 && received < total) {
TcpEndpoint stalled = block.peer();
System.out.println(\"Peer \" + stalled + \" has stalled\");
// Library may disconnect and retry with another peer
}
}
}
Performance Notes:
- BlockInfo objects are read-only snapshots; state changes reflect in new alerts
- Block size is typically 16,384 bytes (16 KB)
- Last block of last piece may be smaller
- PIECE_INFO alerts are triggered frequently; suitable for UI updates
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintThe total number of bytes in this block.intThe number of bytes that have been received for this block.intnumPeers()The number of peers that is currently requesting this block.peer()The peer is the ip address of the peer this block was downloaded from.state()The state this block is in.com.frostwire.jlibtorrent.swig.block_infoswig()
-
Constructor Details
-
BlockInfo
public BlockInfo(com.frostwire.jlibtorrent.swig.block_info b) - Parameters:
b- the native object
-
-
Method Details
-
swig
public com.frostwire.jlibtorrent.swig.block_info swig()- Returns:
- the native object
-
peer
The peer is the ip address of the peer this block was downloaded from.- Returns:
- the peer tcp endpoint
-
bytesProgress
public int bytesProgress()The number of bytes that have been received for this block.- Returns:
- the number of bytes received
-
blockSize
public int blockSize()The total number of bytes in this block.- Returns:
- otal number of bytes
-
state
-
numPeers
public int numPeers()The number of peers that is currently requesting this block. Typically this is 0 or 1, but at the end of the torrent blocks may be requested by more peers in parallel to speed things up.- Returns:
- number of peers
-