code: private static void endFragment(StringBuilder sb, String text, int offset, int limit)
     {
        if (limit == text.length())
        {
           // append all
           sb.append(text.substring(offset));
           return;
        }
        int end = offset;
        for (int i = end; i < limit; i++)
        {
           if (Character.isWhitespace(text.charAt(i)))
           {
              // potential end
              end = i;
           }
        }
        sb.append(text.substring(offset, end)).append(" ...");
     }
docs: Writes the end of a fragment to the string buffer <code>sb</code>. The
  last occurrence of a matching term is indicated by the
  <code>offset</code> into the <code>text</code>.
  
  @param sb     where to append the start of the fragment.
  @param text   the original text.
  @param offset the end offset of the last matching term in the fragment.
  @param limit  do not go further than <code>limit</code>.