本文整理汇总了Java中java.nio.charset.CharsetDecoder.onUnmappableCharacter方法的典型用法代码示例。如果您正苦于以下问题:Java CharsetDecoder.onUnmappableCharacter方法的具体用法?Java CharsetDecoder.onUnmappableCharacter怎么用?Java CharsetDecoder.onUnmappableCharacter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.charset.CharsetDecoder
的用法示例。
在下文中一共展示了CharsetDecoder.onUnmappableCharacter方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import java.nio.charset.CharsetDecoder; //导入方法依赖的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;
}
示例2: convertToUnicodeWithSubstitutions
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
* Convert text in a given character set to a Unicode string. Any invalid
* characters are replaced with U+FFFD. Returns null if the character set
* is not recognized.
* @param text ByteBuffer containing the character array to convert.
* @param charsetName Character set it's in encoded in.
* @return: Unicode string on success, null on failure.
*/
@CalledByNative
private static String convertToUnicodeWithSubstitutions(
ByteBuffer text,
String charsetName) {
try {
Charset charset = Charset.forName(charsetName);
// TODO(mmenke): Investigate if Charset.decode() can be used
// instead. The question is whether it uses the proper replace
// character. JDK CharsetDecoder docs say U+FFFD is the default,
// but Charset.decode() docs say it uses the "charset's default
// replacement byte array".
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
decoder.replaceWith("\uFFFD");
return decoder.decode(text).toString();
} catch (Exception e) {
return null;
}
}
示例3: decode
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
private static String decode(ByteBuffer utf8, boolean replace)
throws CharacterCodingException {
CharsetDecoder decoder = DECODER_FACTORY.get();
if (replace) {
decoder.onMalformedInput(
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;
}
示例4: prepareDecoder
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public CharsetDecoder prepareDecoder(Charset charset) {
CharsetDecoder decoder = charset.newDecoder();
if (strict) {
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
} else {
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
return decoder;
}
示例5: fromUtf8
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public static String fromUtf8(byte[] bytes, int off, int len) {
CharsetDecoder decoder = UTF_8.newDecoder();
decoder.onMalformedInput(IGNORE);
decoder.onUnmappableCharacter(IGNORE);
ByteBuffer buffer = ByteBuffer.wrap(bytes, off, len);
try {
return decoder.decode(buffer).toString();
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
示例6: stringUtf8
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException {
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
decode.onMalformedInput( codingErrorAction );
decode.onUnmappableCharacter( codingErrorAction );
// decode.replaceWith( "X" );
String s;
try {
bytes.mark();
s = decode.decode( bytes ).toString();
bytes.reset();
} catch ( CharacterCodingException e ) {
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
}
return s;
}
示例7: create
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
CharsetDecoder chardecoder = null;
CharsetEncoder charencoder = null;
final Charset charset = cconfig.getCharset();
final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ?
cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ?
cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
if (charset != null) {
chardecoder = charset.newDecoder();
chardecoder.onMalformedInput(malformedInputAction);
chardecoder.onUnmappableCharacter(unmappableInputAction);
charencoder = charset.newEncoder();
charencoder.onMalformedInput(malformedInputAction);
charencoder.onUnmappableCharacter(unmappableInputAction);
}
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
return new LoggingManagedHttpClientConnection(
id,
log,
headerlog,
wirelog,
cconfig.getBufferSize(),
cconfig.getFragmentSizeHint(),
chardecoder,
charencoder,
cconfig.getMessageConstraints(),
null,
null,
requestWriterFactory,
responseParserFactory);
}
示例8: stringUtf8
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
decode.onMalformedInput(codingErrorAction);
decode.onUnmappableCharacter(codingErrorAction);
// decode.replaceWith( "X" );
String s;
try {
bytes.mark();
s = decode.decode(bytes).toString();
bytes.reset();
} catch (CharacterCodingException e) {
throw new InvalidDataException(CloseFrame.NO_UTF8, e);
}
return s;
}
示例9: stringUtf8
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
decode.onMalformedInput(codingErrorAction);
decode.onUnmappableCharacter(codingErrorAction);
try {
bytes.mark();
String s = decode.decode(bytes).toString();
bytes.reset();
return s;
} catch (Throwable e) {
throw new InvalidDataException((int) CloseFrame.NO_UTF8, e);
}
}
示例10: a
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
public static String a(ByteBuffer byteBuffer) {
CharsetDecoder newDecoder = Charset.forName("UTF8").newDecoder();
newDecoder.onMalformedInput(a);
newDecoder.onUnmappableCharacter(a);
try {
byteBuffer.mark();
String charBuffer = newDecoder.decode(byteBuffer).toString();
byteBuffer.reset();
return charBuffer;
} catch (Throwable e) {
throw new b((int) CloseFrame.NO_UTF8, e);
}
}
示例11: checkMeasuredInternal
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
@Override
protected Def checkMeasuredInternal(FileObject fo,
SearchListener listener) {
MappedByteBuffer bb = null;
FileChannel fc = null;
try {
listener.fileContentMatchingStarted(fo.getPath());
File file = FileUtil.toFile(fo);
// Open the file and then get a channel from the stream
FileInputStream fis = new FileInputStream(file);
fc = fis.getChannel();
// Get the file's size and then map it into memory
int sz = (int) fc.size();
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
// if (asciiPattern && !matchesIgnoringEncoding(bb)) {
// return null;
//}
// Decode the file into a char buffer
Charset charset = FileEncodingQuery.getEncoding(fo);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
CharBuffer cb = decoder.decode(bb);
List<TextDetail> textDetails = matchWholeFile(cb, fo);
if (textDetails == null) {
return null;
} else {
Def def = new Def(fo, decoder.charset(), textDetails);
return def;
}
} catch (Exception e) {
listener.generalError(e);
return null;
} finally {
if (fc != null) {
try {
fc.close();
} catch (IOException ex) {
listener.generalError(ex);
}
}
MatcherUtils.unmap(bb);
}
}
示例12: checkSmall
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
* Check file content using Java NIO API.
*/
private Def checkSmall(FileObject fo, File file,
SearchListener listener) {
MappedByteBuffer bb = null;
FileChannel fc = null;
try {
// Open the file and then get a channel from the stream
FileInputStream fis = new FileInputStream(file);
fc = fis.getChannel();
// Get the file's size and then map it into memory
int sz = (int) fc.size();
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
if (asciiPattern && !matchesIgnoringEncoding(bb)) {
return null;
}
// Decode the file into a char buffer
Charset charset = FileEncodingQuery.getEncoding(fo);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
CharBuffer cb = decoder.decode(bb);
List<TextDetail> textDetails = multiline
? matchWholeFile(cb, fo)
: matchLines(cb, fo);
if (textDetails == null) {
return null;
} else {
Def def = new Def(fo, decoder.charset(), textDetails);
return def;
}
} catch (Exception e) {
listener.generalError(e);
return null;
} finally {
if (fc != null) {
try {
fc.close();
} catch (IOException ex) {
listener.generalError(ex);
}
}
unmap(bb);
}
}
示例13: length
import java.nio.charset.CharsetDecoder; //导入方法依赖的package包/类
/**
* Compute lenght of this sequence - quite expensive operation, indeed.
*/
@Override
public int length() {
if (length != -1) {
return length;
}
long start = System.currentTimeMillis();
int charactersRead = 0;
long bytesRead = 0;
MappedByteBuffer mappedByteBuffer = null;
CharBuffer charBuffer = CharBuffer.allocate(SIZE_LIMIT);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
try {
while (bytesRead < fileSize) {
mappedByteBuffer = fileChannel.map(
FileChannel.MapMode.READ_ONLY, bytesRead,
Math.min(SIZE_LIMIT, fileSize - bytesRead));
CoderResult result;
do {
charBuffer.clear();
result = decoder.decode(
mappedByteBuffer, charBuffer,
bytesRead + SIZE_LIMIT >= fileSize);
if (result.isUnmappable() || result.isMalformed()
|| result.isError()) {
throw new IOException("Error decoding file: "
+ result.toString() + " ");
}
if (bytesRead + SIZE_LIMIT >= fileSize) {
LOG.info("Coding end");
}
charactersRead += charBuffer.position();
} while (result.isOverflow());
int readNow = mappedByteBuffer.position();
bytesRead += readNow;
unmap(mappedByteBuffer);
}
charBuffer.clear();
boolean repeat;
do {
repeat = decoder.flush(charBuffer).isOverflow();
charactersRead += charBuffer.position();
charBuffer.clear();
} while (repeat);
} catch (IOException ex) {
if (mappedByteBuffer != null) {
unmap(mappedByteBuffer);
}
Exceptions.printStackTrace(ex);
}
length = charactersRead;
LOG.log(Level.INFO, "Length computed in {0} ms.", //NOI18N
System.currentTimeMillis() - start);
return length;
}