super.insertString(0, s.substring(len - MAX_CHARACTERS), a);
super.insertString(0, s.substring(len - bufferSize), a);
  public void appendString(String s, AttributeSet a) {    try {      int len = s.length();      // special case, the string is larger than the document      // just insert the end of the string      if (len >= MAX_CHARACTERS) {        remove(0, getLength());        super.insertString(0, s.substring(len - MAX_CHARACTERS), a);        return;      }      int bufferLength = getLength();      int neededSpace = bufferLength + len;      // if appending string will exceed buffer length      // then remove at least the first 20% of buffer      if (neededSpace > MAX_CHARACTERS) {        int tmp = Math.max(neededSpace - MAX_CHARACTERS,                           MIN_REMOVE_CHARACTERS);        // don't remove more characters than exist        tmp = Math.min(tmp, bufferLength);        remove(0, tmp);      }      super.insertString(getLength(), s, a);    } catch (BadLocationException ble) {      System.out.println("Bad location exception: " + ble +                         " " + ble.offsetRequested());      ble.printStackTrace();    }  }