sclass StringBufferWithMaxSize implements Appendable { int maxSize; // in characters new StringBuilder buf; *(int *maxSize) {} public Appendable append(char c) { ret append(str(c)); } public Appendable append(CharSequence csq, int start, int end) { ret append(csq.subSequence(start, end)); } int size() { ret l(buf); } public synchronized Appendable append(CharSequence csq) { if (full()) this; int n = l(csq); if (size()+n > maxSize) buf.append(subCharSequence(csq, maxSize-size())); else buf.append(csq); this; } bool full() { ret size() >= maxSize; } synchronized toString { ret str(buf); } }