001
014
015 package com.liferay.portal.kernel.nio.charset;
016
017 import java.nio.ByteBuffer;
018 import java.nio.CharBuffer;
019 import java.nio.charset.CharacterCodingException;
020 import java.nio.charset.Charset;
021 import java.nio.charset.CharsetDecoder;
022 import java.nio.charset.CodingErrorAction;
023
024
027 public class CharsetDecoderUtil {
028
029 public static CharBuffer decode(String charsetName, byte[] bytes) {
030 return decode(charsetName, ByteBuffer.wrap(bytes));
031 }
032
033 public static CharBuffer decode(
034 String charsetName, byte[] bytes, int offset, int length) {
035
036 return decode(charsetName, ByteBuffer.wrap(bytes, offset, length));
037 }
038
039 public static CharBuffer decode(String charsetName, ByteBuffer byteBuffer) {
040 try {
041 CharsetDecoder charsetDecoder = getCharsetDecoder(charsetName);
042
043 return charsetDecoder.decode(byteBuffer);
044 }
045 catch (CharacterCodingException cce) {
046 throw new Error(cce);
047 }
048 }
049
050 public static CharsetDecoder getCharsetDecoder(String charsetName) {
051 return getCharsetDecoder(charsetName, CodingErrorAction.REPLACE);
052 }
053
054 public static CharsetDecoder getCharsetDecoder(
055 String charsetName, CodingErrorAction codingErrorAction) {
056
057 Charset charset = Charset.forName(charsetName);
058
059 CharsetDecoder charsetDecoder = charset.newDecoder();
060
061 charsetDecoder.onMalformedInput(codingErrorAction);
062 charsetDecoder.onUnmappableCharacter(codingErrorAction);
063
064 return charsetDecoder;
065 }
066
067 }