Jump to content

How works "Autodetect of pieces length" during creating torrent-file ?


avkiev

Recommended Posts

How works "Autodetect of pieces length" during creating torrent-file ?

I think - it is smth like that:

int AutoPieceLenInK(long Filelen)

{

if (FileLen < s1) return(32);

if (FileLen < s2) return(64);

if (FileLen < s3) return(128);

if (FileLen < s4) return(256);

if (FileLen < s5) return(512);

if (FileLen < s6) return(1024);

if (FileLen < s7) return(2048);

return(4096);

}

Which values has constants s1-s7 ?

Could you, please, share source code of this function ?

Thanks in advance

Link to comment
Share on other sites

Thank you very much. The answer is:

public static final long TO_DEFAULT_FIXED_PIECE_SIZE = 256*1024;

public static final long TO_DEFAULT_VARIABLE_PIECE_SIZE_MIN = 32*1024;

public static final long TO_DEFAULT_VARIABLE_PIECE_SIZE_MAX = 2*1024*1024;

public static final long TO_DEFAULT_VARIABLE_PIECE_NUM_LOWER = 1024;

public static final long TO_DEFAULT_VARIABLE_PIECE_NUM_UPPER = 2048;

public static final long[] STANDARD_PIECE_SIZES = { 32*1024, 48*1024, 64*1024, 96*1024,

128*1024, 192*1024, 256*1024, 384*1024,

512*1024, 768*1024, 1024*1024,

1536*1024, 2*1024*1024, 3*1024*1024, 4*1024*1024 };

public static long

getComputedPieceSize(

long total_size,

long _piece_min_size,

long _piece_max_size,

long _piece_num_lower,

long _piece_num_upper )

{

long piece_length = -1;

long current_piece_size = _piece_min_size;

while( current_piece_size <= _piece_max_size ){

long pieces = total_size / current_piece_size;

if ( pieces <= _piece_num_upper ){

piece_length = current_piece_size;

break;

}

current_piece_size = current_piece_size << 1;

}

// if we haven't set a piece length here then there are too many pieces even

// at maximum piece size. Go for largest piece size

if ( piece_length == -1 ){

// just go for the maximum piece size

piece_length = _piece_max_size;

}

return( piece_length );

}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...