Exemplo n.º 1
0
    /**
     * Creates a new entry if not yet present. Adds the fragment. If all fragements for a given
     * message have been received, an entire message is reassembled and returned. Otherwise null is
     * returned.
     *
     * @param id - the message ID, unique for a sender
     * @param frag_id the index of this fragmentation (0..tot_frags-1)
     * @param tot_frags the total number of fragmentations expected
     * @param fragment - the byte buffer for this fragment
     */
    public synchronized byte[] add(long id, int frag_id, int tot_frags, byte[] fragment) {
      byte[] retval = null; // initialize the return value to default not complete

      FragEntry e = table.get(id);
      if (e == null) { // Create new entry if not yet present
        e = new FragEntry(id, tot_frags);
        table.put(id, e);
      }

      e.set(frag_id, fragment);
      if (e.isComplete()) {
        retval = e.assembleBuffer();
        table.remove(id);
      }
      return retval;
    }