Code example for CharsetDecoder

Methods: decode, flush, maxCharsPerByte

0
	 */ 
	public static StringBuilder decodeUTF8(StringBuilder result, byte[] bytes, int offset, int length)
	{ 
		CharsetDecoder decoder = acquireUTF8Encoder();
 
		int scaledLength = (int) ((double) length * (double) decoder.maxCharsPerByte());
		char[] chars = charArrayPool.acquire();
 
		if (chars.length < scaledLength)
			chars = new char[scaledLength]; // we'll swap buffers on the pool, let the smaller one get
																			// gc'ed 
		CharBuffer cb = CharBuffer.wrap(chars);
 
		ByteBuffer bb = ByteBuffer.wrap(bytes, offset, length);
		CoderResult cr = decoder.decode(bb, cb, true);
 
		// cb.rewind(); // reset for re-use -- 
 
		int resultLength = cb.position();
 
		result.append(chars, 0, resultLength);
		decoder.flush(cb);
 
		utf8DecoderPool.release(decoder);
		charArrayPool.release(chars);
 
		return result;
	}