ByteToCharEncoder.java

package com.ziesemer.utils.codec.base;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;

import com.ziesemer.utils.codec.IByteToCharEncoder;

/**
 * @author Mark A. Ziesemer
 * 	<a href="http://www.ziesemer.com.">&lt;www.ziesemer.com&gt;</a>
 * @see java.nio.charset.CharsetDecoder
 */
public abstract class ByteToCharEncoder<T extends ByteToCharEncoder<T>>
		extends BaseCoder<ByteBuffer, CharBuffer, T>
		implements IByteToCharEncoder{
	
	private static final long serialVersionUID = 1L;
	
	protected ByteToCharEncoder(final float minInPerOut, final float averageCharsPerByte, final float maxCharsPerByte){
		super(minInPerOut, averageCharsPerByte, maxCharsPerByte);
	}
	
	@Override
	protected CharBuffer allocate(final int capacity){
		return CharBuffer.allocate(capacity);
	}
	
	@Override
	protected CharBuffer put(final CharBuffer base, final CharBuffer put){
		return base.put(put);
	}
	
	/**
	 * Convenience method for {@link #code(java.nio.Buffer)}.
	 * Does not handle all desired variations by design, e.g. only encoding a portion of the input.
	 */
	public String encodeToString(final byte[] in) throws CharacterCodingException{
		final CharBuffer out = code(ByteBuffer.wrap(in));
		return out.toString();
	}
	
}