本文整理汇总了Java中java.nio.charset.CharacterCodingException类的典型用法代码示例。如果您正苦于以下问题:Java CharacterCodingException类的具体用法?Java CharacterCodingException怎么用?Java CharacterCodingException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CharacterCodingException类属于java.nio.charset包,在下文中一共展示了CharacterCodingException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testParseFalseEncodedFile
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public void testParseFalseEncodedFile() throws IOException {
Path tempDir = createTempDir();
Path dict = tempDir.resolve("foo.dict");
Settings nodeSettings = Settings.builder()
.put("foo.bar_path", dict)
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir).build();
try (OutputStream writer = Files.newOutputStream(dict)) {
writer.write(new byte[]{(byte) 0xff, 0x00, 0x00}); // some invalid UTF-8
writer.write('\n');
}
Environment env = new Environment(nodeSettings);
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class,
() -> Analysis.getWordList(env, nodeSettings, "foo.bar"));
assertEquals("Unsupported character encoding detected while reading foo.bar_path: " + tempDir.resolve("foo.dict").toString()
+ " - files must be UTF-8 encoded" , ex.getMessage());
assertTrue(ex.getCause().toString(), ex.getCause() instanceof MalformedInputException
|| ex.getCause() instanceof CharacterCodingException);
}
示例2: processData
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public void processData() throws CannotReadException {
//Skip other flags
dataBuffer.position(dataBuffer.position() + VERSION_FLAG_LENGTH + OTHER_FLAG_LENGTH + RESERVED_FLAG_LENGTH);
CharsetDecoder decoder = Charset.forName("ISO-8859-1").newDecoder();
try {
handlerType = decoder.decode((ByteBuffer) dataBuffer.slice().limit(HANDLER_LENGTH)).toString();
} catch (CharacterCodingException cee) {
//Ignore
}
//To getFields human readable name
mediaDataType = mediaDataTypeMap.get(handlerType);
}
示例3: processData
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public void processData() throws CannotReadException
{
//Skip other flags
dataBuffer.position(dataBuffer.position() + VERSION_FLAG_LENGTH + OTHER_FLAG_LENGTH + RESERVED_FLAG_LENGTH);
CharsetDecoder decoder = Charset.forName("ISO-8859-1").newDecoder();
try
{
handlerType = decoder.decode((ByteBuffer) dataBuffer.slice().limit(HANDLER_LENGTH)).toString();
}
catch (CharacterCodingException cee)
{
//Ignore
}
//To getFields human readable name
mediaDataType = mediaDataTypeMap.get( handlerType);
}
示例4: readByteArray
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
/**
* Read a string from buffer of fixed size(size has already been set in constructor)
*
* @param arr this is the buffer for the frame
* @param offset this is where to start reading in the buffer for this field
*/
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException {
logger.config("Reading from array from offset:" + offset);
try {
String charSetName = getTextEncodingCharSet();
CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();
//Decode buffer if runs into problems should through exception which we
//catch and then set value to empty string.
logger.finest("Array length is:" + arr.length + "offset is:" + offset + "Size is:" + size);
if (arr.length - offset < size) {
throw new InvalidDataTypeException("byte array is to small to retrieve string of declared length:" + size);
}
String str = decoder.decode(ByteBuffer.wrap(arr, offset, size)).toString();
if (str == null) {
throw new NullPointerException("String is null");
}
value = str;
} catch (CharacterCodingException ce) {
logger.severe(ce.getMessage());
value = "";
}
logger.config("Read StringFixedLength:" + value);
}
示例5: splitKeyVal
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
private void splitKeyVal(byte[] line, int length, Text key, Text val)
throws IOException {
// Need to find numKeyFields separators
int pos = UTF8ByteArrayUtils.findBytes(line, 0, length, separator);
for(int k=1; k<numKeyFields && pos!=-1; k++) {
pos = UTF8ByteArrayUtils.findBytes(line, pos + separator.length,
length, separator);
}
try {
if (pos == -1) {
key.set(line, 0, length);
val.set("");
} else {
StreamKeyValUtil.splitKeyVal(line, 0, length, key, val, pos,
separator.length);
}
} catch (CharacterCodingException e) {
throw new IOException(StringUtils.stringifyException(e));
}
}
示例6: decode
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public static String decode(ByteBuffer buffer) {
//buffer.flip();
// FIXME: Setting the charset somewhere
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer;
try {
charBuffer = decoder.decode(buffer);
String message = charBuffer.toString();
message = message.split("\n")[0];
message = message.split("\r")[0];
return message;
}
catch(CharacterCodingException cce) {
cce.printStackTrace();
}
return "";
}
示例7: decode
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
try {
CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = charsetDecoder.flush(charByte);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
// Substitution is always enabled,
// so this shouldn't happen
throw new JSONException("utf8 decode error, " + x.getMessage(), x);
}
}
示例8: getCharAt
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
private char getCharAt(int index) throws IndexOutOfBoundsException {
if(sink.buffer.scope.isBefore(index)) {
reset();
}
while(!sink.buffer.scope.isInside(index)) {
boolean hasNext;
try {
hasNext = sink.next();
} catch (CharacterCodingException e) {
throw new SourceIOException(e);
}
if (listener != null && sink.buffer.scope.start > maxReadOffset) {
maxReadOffset = sink.buffer.scope.start;
listener.fileContentMatchingProgress(source.fo.getPath(),
maxReadOffset);
}
if(!hasNext) {
throw new IndexOutOfBoundsException("index is " +
index + " > lenght"); // NOI18N
}
}
return sink.charAt(index);
}
示例9: encode
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
/**
* Converts the provided String to bytes using the
* UTF-8 encoding. If <code>replace</code> is true, then
* malformed input is replaced with the
* substitution character, which is U+FFFD. Otherwise the
* method throws a MalformedInputException.
* @return ByteBuffer: bytes stores at ByteBuffer.array()
* and length is ByteBuffer.limit()
*/
public static ByteBuffer encode(String string, boolean replace)
throws CharacterCodingException {
CharsetEncoder encoder = ENCODER_FACTORY.get();
if (replace) {
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
ByteBuffer bytes =
encoder.encode(CharBuffer.wrap(string.toCharArray()));
if (replace) {
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
}
return bytes;
}
示例10: testEncodingOfString
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
public void testEncodingOfString() throws CharacterCodingException {
final PropCharsetEncoder encoder
= new PropCharsetEncoder(new PropCharset());
compare(encoder.encodeStringForTests(""),
new byte[] {});
compare(encoder.encodeStringForTests("a"),
new byte[] {'a'});
compare(encoder.encodeStringForTests("\\"), //pending character
new byte[] {'\\'});
compare(encoder.encodeStringForTests("\\\\"),
new byte[] {'\\', '\\'});
compare(encoder.encodeStringForTests("\\t"),
new byte[] {'\\', 't'});
compare(encoder.encodeStringForTests("key\t=value"),
new byte[] {'k', 'e', 'y', '\t', '=', 'v', 'a', 'l', 'u', 'e'});
compare(encoder.encodeStringForTests("key=\tvalue"),
new byte[] {'k', 'e', 'y', '=', '\t', 'v', 'a', 'l', 'u', 'e'});
}
示例11: decode
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
private static String decode(ByteBuffer utf8, boolean replace)
throws CharacterCodingException {
CharsetDecoder decoder = DECODER_FACTORY.get();
if (replace) {
decoder.onMalformedInput(
java.nio.charset.CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
String str = decoder.decode(utf8).toString();
// set decoder back to its default value: REPORT
if (replace) {
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
}
return str;
}
示例12: decodeUT8
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
/**
* Decode the buffer using the CharsetDecoder.
* @param byteBuf
* @return false if failed because the charbuffer was not big enough
* @throws RuntimeException if it fails for encoding errors
*/
private boolean decodeUT8(ByteBuffer byteBuf) {
// We give it all of the input data in call.
boolean endOfInput = true;
decoder.reset();
charBuffer.rewind();
// Convert utf-8 bytes to sequence of chars
CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput);
if (result.isOverflow()) {
// Not enough space in the charBuffer.
return false;
} else if (result.isError()) {
// Any other error
try {
result.throwException();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
return true;
}
示例13: byCharsetEncoder_US_ASCII
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
@Benchmark
public byte[] byCharsetEncoder_US_ASCII() {
try {
CharsetEncoder encoder = asciiencode.get();
CharBuffer buffer = charbuffergenerator.get();
buffer.clear();
buffer.append(STR);
buffer.flip();
ByteBuffer outbuffer = bytebuffergenerator.get();
outbuffer.clear();
CoderResult result = encoder.encode(buffer, outbuffer, false);
if (result.isError()) {
result.throwException();
}
byte[] b = new byte[STR.length()];
outbuffer.flip();
outbuffer.get(b);
return b;
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
示例14: writeBuffer
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
private boolean writeBuffer(final ByteBuffer buffer, final IoCallback callback) {
StringBuilder builder = new StringBuilder();
try {
builder.append(charsetDecoder.decode(buffer));
} catch (CharacterCodingException e) {
callback.onException(exchange, this, e);
return false;
}
String data = builder.toString();
writer.write(data);
if (writer.checkError()) {
callback.onException(exchange, this, new IOException());
return false;
}
return true;
}
示例15: decodeProperties
import java.nio.charset.CharacterCodingException; //导入依赖的package包/类
private Map<String, String> decodeProperties ( final IoSession session, final IoBuffer data ) throws CharacterCodingException
{
final int count = data.getInt ();
final Map<String, String> result = new HashMap<String, String> ( count );
final CharsetDecoder decoder = getCharsetDecoder ( session );
for ( int i = 0; i < count; i++ )
{
final String key = data.getString ( decoder );
final String value = data.getString ( decoder );
result.put ( key, value );
}
return result;
}