1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.util; 20 21 22 /* ------------------------------------------------------------ */ 23 /** UTF-8 StringBuilder. 24 * 25 * This class wraps a standard {@link java.lang.StringBuilder} and provides methods to append 26 * UTF-8 encoded bytes, that are converted into characters. 27 * 28 * This class is stateful and up to 4 calls to {@link #append(byte)} may be needed before 29 * state a character is appended to the string buffer. 30 * 31 * The UTF-8 decoding is done by this class and no additional buffers or Readers are used. 32 * The UTF-8 code was inspired by http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ 33 * 34 */ 35 public class Utf8StringBuilder extends Utf8Appendable 36 { 37 final StringBuilder _buffer; 38 39 public Utf8StringBuilder() 40 { 41 super(new StringBuilder()); 42 _buffer=(StringBuilder)_appendable; 43 } 44 45 public Utf8StringBuilder(int capacity) 46 { 47 super(new StringBuilder(capacity)); 48 _buffer=(StringBuilder)_appendable; 49 } 50 51 @Override 52 public int length() 53 { 54 return _buffer.length(); 55 } 56 57 @Override 58 public void reset() 59 { 60 super.reset(); 61 _buffer.setLength(0); 62 } 63 64 public StringBuilder getStringBuilder() 65 { 66 checkState(); 67 return _buffer; 68 } 69 70 @Override 71 public String toString() 72 { 73 checkState(); 74 return _buffer.toString(); 75 } 76 77 78 }