/**
   * Given the given fragmentation threshold, determine whether the given bundle should be split
   * into several smaller bundles. If so, this returns true and generates a bunch of bundle received
   * events for the individual fragments.
   *
   * @param bundle Bundle to split into fragments
   * @param link Bundle to send on the given link
   * @param max_length Maximum length of the fragment
   * @return FragmentState that has the list of created fragments for the given bundle.
   */
  public FragmentState proactively_fragment(Bundle bundle, final Link link, int max_length) {
    int payload_len = bundle.payload().length();

    Bundle fragment;

    FragmentState state = new FragmentState(bundle);

    int todo = payload_len;
    int offset = 0;
    int count = 0;

    BlockInfoVec first_frag_blocks = new BlockInfoVec();
    BlockInfoVec all_frag_blocks = new BlockInfoVec();
    BlockInfoVec this_frag_blocks = first_frag_blocks;

    ListIterator<BlockInfo> entry = bundle.xmit_link_block_set().find_blocks(link).listIterator();

    while (entry.hasNext()) {
      BlockInfo block_info = entry.next();

      if ((block_info.type() == bundle_block_type_t.PRIMARY_BLOCK)
          || (block_info.type() == bundle_block_type_t.PAYLOAD_BLOCK)) {

        all_frag_blocks.add(block_info);
        first_frag_blocks.add(block_info);

      } else if ((block_info.flags() & block_flag_t.BLOCK_FLAG_REPLICATE.getCode()) > 0) {
        all_frag_blocks.add(block_info);
      } else {
        first_frag_blocks.add(block_info);
      }
    }

    do {
      fragment = create_fragment(bundle, link, this_frag_blocks, offset, max_length);
      assert (fragment != null) : TAG + ": proactively_fragment() fragment not valid";

      state.add_fragment(fragment);
      offset += fragment.payload().length();
      todo -= fragment.payload().length();
      this_frag_blocks = all_frag_blocks;
      ++count;

    } while (todo > 0);

    Logger.getInstance()
        .debug(
            TAG,
            String.format(
                "proactively fragmenting " + "%s byte payload into %s %s byte fragments",
                payload_len, count, max_length));

    String[] hash_key = new String[1];
    get_hash_key(fragment, hash_key);
    fragment_table_.put(hash_key[0], state);

    return state;
  }
  /**
   * Create a bundle fragment from another bundle.
   *
   * @param bundle the source bundle from which we create the fragment. Note: the bundle may itself
   *     be a fragment
   * @param offset the offset relative to this bundle (not the original) for the for the new
   *     fragment. note that if this bundle is already a fragment, the offset into the original
   *     bundle will be this bundle's frag_offset + offset
   * @param length the length of the fragment we want
   * @return Newly created bundle
   */
  public Bundle create_fragment(Bundle bundle, BlockInfoVec blocks, int offset, int length) {

    Bundle fragment = new Bundle(location_t.MEMORY);
    bundle.copy_metadata(fragment);
    fragment.set_is_fragment(true);
    fragment.set_do_not_fragment(false);

    // initialize the fragment's orig_length and figure out the offset
    // into the payload
    if (!bundle.is_fragment()) {
      fragment.set_orig_length(bundle.payload().length());
      fragment.set_frag_offset(offset);
    } else {
      fragment.set_orig_length(bundle.orig_length());
      fragment.set_frag_offset(bundle.frag_offset() + offset);
    }

    // check for overallocated length
    if ((offset + length) > fragment.orig_length()) {
      Logger.getInstance()
          .error(
              TAG,
              String.format(
                  "fragment length overrun: "
                      + "orig_length %d frag_offset %d requested offset %d length %d",
                  fragment.orig_length(), fragment.frag_offset(), offset, length));

      // Panic;
      return null;
    }

    // initialize payload
    fragment.payload().write_data(bundle.payload(), offset, length, 0);

    // copy all blocks that follow the payload, and all those before
    // the payload that are marked with the "must be replicated in every
    // fragment" bit
    ListIterator<BlockInfo> iter = blocks.listIterator();

    // BlockInfoVec vec;

    boolean found_payload = false;

    while (iter.hasNext()) {
      BlockInfo entry = iter.next();

      int type = entry.type().getCode();

      if ((type == bundle_block_type_t.PRIMARY_BLOCK.getCode())
          || (bundle_block_type_t.PAYLOAD_BLOCK.getCode() > 0)
          || found_payload
          || ((entry.flags() & block_flag_t.BLOCK_FLAG_REPLICATE.getCode()) > 0)) {

        // we need to include this block; copy the BlockInfo into the
        // fragment
        fragment.recv_blocks().add(entry);

        if (type == bundle_block_type_t.PAYLOAD_BLOCK.getCode()) {
          found_payload = true;
        }
      }
    }
    return fragment;
  }