Class BoundedAppender

java.lang.Object
org.apache.hadoop.yarn.util.BoundedAppender

@Public @Unstable @VisibleForTesting public class BoundedAppender extends Object
A CharSequence appender that considers its limit as upper bound.

When limit would be reached on append, past messages will be truncated from head, and a header telling the user about truncation will be prepended, with ellipses in between header and messages.

Note that header and ellipses are not counted against limit.

An example:

 
   // At the beginning it's an empty string
   final Appendable shortAppender = new BoundedAppender(80);
   // The whole message fits into limit
   shortAppender.append(
       "message1 this is a very long message but fitting into limit\n");
   // The first message is truncated, the second not
   shortAppender.append("message2 this is shorter than the previous one\n");
   // The first message is deleted, the second truncated, the third
   // preserved
   shortAppender.append("message3 this is even shorter message, maybe.\n");
   // The first two are deleted, the third one truncated, the last preserved
   shortAppender.append("message4 the shortest one, yet the greatest :)");
   // Current contents are like this:
   // Diagnostic messages truncated, showing last 80 chars out of 199:
   // ...s is even shorter message, maybe.
   // message4 the shortest one, yet the greatest :)
 
 

Note that null values are appended just like in original implementation.

Note that this class is not thread safe.

  • Field Details

    • TRUNCATED_MESSAGES_TEMPLATE

      @VisibleForTesting public static final String TRUNCATED_MESSAGES_TEMPLATE
      See Also:
  • Constructor Details

    • BoundedAppender

      public BoundedAppender(int limit)
  • Method Details

    • append

      public BoundedAppender append(CharSequence csq)
      Append a CharSequence considering limit, truncating from the head of csq or messages when necessary.
      Parameters:
      csq - the CharSequence to append
      Returns:
      this
    • length

      public int length()
      Get current length of messages considering truncates without header and ellipses.
      Returns:
      current length
    • getLimit

      public int getLimit()
    • toString

      public String toString()
      Get a string representation of the actual contents, displaying also a header and ellipses when there was a truncate.
      Overrides:
      toString in class Object
      Returns:
      String representation of the messages