public Result generate ( ) throws IOException { if ( path == null ) { throw new IOException ( "path can't be null" ) ; } File absPath = path . getAbsoluteFile ( ) ; file_storage fs = new file_storage ( ) ; add_files_listener l1 = new add_files_listener ( ) { @ Override public boolean pred ( String p ) { return listener != null ? listener . accept ( p ) : true ; } } ; add_files_ex ( fs , absPath . getPath ( ) , l1 , flags ) ; if ( fs . total_size ( ) == 0 ) { throw new IOException ( "content total size can't be 0" ) ; } create_torrent t = new create_torrent ( fs , pieceSize , padFileLimit , flags , alignment ) ; final int numPieces = t . num_pieces ( ) ; set_piece_hashes_listener l2 = new set_piece_hashes_listener ( ) { @ Override public void progress ( int i ) { if ( listener != null ) { listener . progress ( i , numPieces ) ; } } } ; File parent = absPath . getParentFile ( ) ; if ( parent == null ) { throw new IOException ( "path's parent can't be null" ) ; } error_code ec = new error_code ( ) ; set_piece_hashes_ex ( t , parent . getAbsolutePath ( ) , l2 , ec ) ; if ( ec . value ( ) != 0 ) { throw new IOException ( ec . message ( ) ) ; } if ( comment != null ) { t . set_comment ( comment ) ; } if ( creator != null ) { t . set_creator ( creator ) ; } for ( String s : urlSeeds ) { t . add_url_seed ( s ) ; } for ( String s : httpSeeds ) { t . add_http_seed ( s ) ; } for ( Pair < String , Integer > n : nodes ) { t . add_node ( n . to_string_int_pair ( ) ) ; } for ( Pair < String , Integer > tr : trackers ) { t . add_tracker ( tr . first , tr . second ) ; } if ( priv ) { t . set_priv ( priv ) ; } if ( ! similarTorrents . isEmpty ( ) ) { for ( Sha1Hash h : similarTorrents ) { t . add_similar_torrent ( h . swig ( ) ) ; } } if ( ! collections . isEmpty ( ) ) { for ( String s : collections ) { t . add_collection ( s ) ; } } return new Result ( t ) ; }
This function will generate a result withe the . torrent file as a bencode tree .