本文整理匯總了Java中java.nio.charset.CodingErrorAction類的典型用法代碼示例。如果您正苦於以下問題:Java CodingErrorAction類的具體用法?Java CodingErrorAction怎麽用?Java CodingErrorAction使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CodingErrorAction類屬於java.nio.charset包,在下文中一共展示了CodingErrorAction類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: B2CConverter
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
public B2CConverter(String encoding, boolean replaceOnError) throws IOException {
byte[] left = new byte[LEFTOVER_SIZE];
leftovers = ByteBuffer.wrap(left);
CodingErrorAction action;
if (replaceOnError) {
action = CodingErrorAction.REPLACE;
} else {
action = CodingErrorAction.REPORT;
}
Charset charset = getCharset(encoding);
// Special case. Use the Apache Harmony based UTF-8 decoder because it
// - a) rejects invalid sequences that the JVM decoder does not
// - b) fails faster for some invalid sequences
if (charset.equals(UTF_8)) {
decoder = new Utf8Decoder();
} else {
decoder = charset.newDecoder();
}
decoder.onMalformedInput(action);
decoder.onUnmappableCharacter(action);
}
示例2: canDecodeFile
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
private boolean canDecodeFile(FileObject fo, String encoding) {
CharsetDecoder decoder = Charset.forName(encoding).newDecoder().onUnmappableCharacter(CodingErrorAction.REPORT).onMalformedInput(CodingErrorAction.REPORT);
try {
BufferedInputStream bis = new BufferedInputStream(fo.getInputStream());
//I probably have to create such big buffer since I am not sure
//how to cut the file to smaller byte arrays so it cannot happen
//that an encoded character is divided by the arrays border.
//In such case it might happen that the method woult return
//incorrect value.
byte[] buffer = new byte[(int) fo.getSize()];
bis.read(buffer);
bis.close();
decoder.decode(ByteBuffer.wrap(buffer));
return true;
} catch (CharacterCodingException ex) {
//return false
} catch (IOException ioe) {
Logger.getLogger("global").log(Level.WARNING, "Error during charset verification", ioe);
}
return false;
}
示例3: setEncoding
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
protected final void setEncoding(final String encoding)
throws UnsupportedEncodingException {
final Charset charSet = charsetForName(encoding);
final CharsetEncoder encoder = charSet.newEncoder().onMalformedInput(
CodingErrorAction.REPLACE).onUnmappableCharacter(
CodingErrorAction.REPLACE);
final float maxBytesPerChar = encoder.maxBytesPerChar();
final float averageBytesPerChar = encoder.averageBytesPerChar();
final boolean fixedWidthCharset =
(maxBytesPerChar == Math.round(maxBytesPerChar))
&& (maxBytesPerChar == averageBytesPerChar);
//
m_fixedWidthCharset = fixedWidthCharset;
m_maxCharWidth = Math.round(maxBytesPerChar);
m_charset = charSet;
m_encoder = encoder;
m_encoding = m_charset.name();
}
示例4: decode
import java.nio.charset.CodingErrorAction; //導入依賴的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;
}
示例5: CharsetEncoderByteIterator
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
/**
* Constructs a new encoder instance that iterates over {@code string}, converting
* it to bytes using the charset {@code charset}.
*
* <p>The encoder reads up to {@code stepSize} characters at the same time,
* buffering the results internally. {@code stepSize} must be at least 2 (this is to
* ensure that surrogate pairs are processed correctly).
*
* @param string the string to iterate over, must not be {@code null}
* @param charset the charset to use for encoding characters to bytes, must not be {@code null}
* @param stepSize the number to characters to try encoding in each encoding step, must be
* positive
* @throws NullPointerException if {@code string} or {@code charset} is {@code null}
* @throws IllegalArgumentException if {@code stepSize} is lesser than 2
*/
public CharsetEncoderByteIterator(String string, Charset charset, int stepSize) {
Objects.requireNonNull(string);
Check.gt(stepSize, 1);
// use the same settings as String.getBytes(Charset)
this.encoder = charset.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.reset();
this.string = string;
this.idx = 0;
this.byteIdx = 0;
this.flushed = false;
// no need to allocate more chars than what the string can give us
stepSize = Math.min(stepSize, string.length());
stepSize = Math.max(2, stepSize); // but ensure we can always handle surrogate pairs
this.in = CharBuffer.allocate(stepSize);
int outBufferSize = (int) ((stepSize + 1) * encoder.maxBytesPerChar());
this.out = ByteBuffer.allocate(outBufferSize);
out.flip();
}
示例6: convertToUnicodeWithSubstitutions
import java.nio.charset.CodingErrorAction; //導入依賴的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;
}
}
示例7: B2CConverter
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
public B2CConverter(String encoding, boolean replaceOnError)
throws IOException {
byte[] left = new byte[LEFTOVER_SIZE];
leftovers = ByteBuffer.wrap(left);
CodingErrorAction action;
if (replaceOnError) {
action = CodingErrorAction.REPLACE;
} else {
action = CodingErrorAction.REPORT;
}
Charset charset = getCharset(encoding);
// Special case. Use the Apache Harmony based UTF-8 decoder because it
// - a) rejects invalid sequences that the JVM decoder does not
// - b) fails faster for some invalid sequences
if (charset.equals(UTF_8)) {
decoder = new Utf8Decoder();
} else {
decoder = charset.newDecoder();
}
decoder.onMalformedInput(action);
decoder.onUnmappableCharacter(action);
}
示例8: AbstractSessionOutputBuffer
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
protected AbstractSessionOutputBuffer(
final OutputStream outstream,
final int buffersize,
final Charset charset,
final int minChunkLimit,
final CodingErrorAction malformedCharAction,
final CodingErrorAction unmappableCharAction) {
super();
Args.notNull(outstream, "Input stream");
Args.notNegative(buffersize, "Buffer size");
this.outstream = outstream;
this.buffer = new ByteArrayBuffer(buffersize);
this.charset = charset != null ? charset : Consts.ASCII;
this.ascii = this.charset.equals(Consts.ASCII);
this.encoder = null;
this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512;
this.metrics = createTransportMetrics();
this.onMalformedCharAction = malformedCharAction != null ? malformedCharAction :
CodingErrorAction.REPORT;
this.onUnmappableCharAction = unmappableCharAction != null? unmappableCharAction :
CodingErrorAction.REPORT;
}
示例9: init
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
protected void init(final OutputStream outstream, final int buffersize, final HttpParams params) {
Args.notNull(outstream, "Input stream");
Args.notNegative(buffersize, "Buffer size");
Args.notNull(params, "HTTP parameters");
this.outstream = outstream;
this.buffer = new ByteArrayBuffer(buffersize);
final String charset = (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
this.charset = charset != null ? Charset.forName(charset) : Consts.ASCII;
this.ascii = this.charset.equals(Consts.ASCII);
this.encoder = null;
this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512);
this.metrics = createTransportMetrics();
final CodingErrorAction a1 = (CodingErrorAction) params.getParameter(
CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
this.onMalformedCharAction = a1 != null ? a1 : CodingErrorAction.REPORT;
final CodingErrorAction a2 = (CodingErrorAction) params.getParameter(
CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
this.onUnmappableCharAction = a2 != null? a2 : CodingErrorAction.REPORT;
}
示例10: decode
import java.nio.charset.CodingErrorAction; //導入依賴的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;
}
示例11: getUnicodeData
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
/**
* Returns the data for this frame, as it would be translated into
* Unicode; this may omit a few bytes of raw data at the end, or add extra
* at the start, to allow for the situation where a frame boundary occurs
* inside a Unicode character.
* @return the unicodeData
*/
private String getUnicodeData() {
byte[] b;
if (unicodePrefix.length != 0 || unicodeChopEnding != 0) {
b = new byte[frameData.length + unicodePrefix.length - unicodeChopEnding];
System.arraycopy(unicodePrefix,0,b,0,unicodePrefix.length);
System.arraycopy(frameData,0,b,
unicodePrefix.length,frameData.length - unicodeChopEnding);
} else b = frameData;
String unicodeData;
try {
unicodeData = Charset.forName("UTF-8").newDecoder().
onMalformedInput(CodingErrorAction.REPORT).
decode(ByteBuffer.wrap(b)).toString();
} catch (CharacterCodingException ex) {
throw new RuntimeException("UTF-8 became invalid while we weren't looking at it");
}
return unicodeData;
}
示例12: encode
import java.nio.charset.CodingErrorAction; //導入依賴的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;
}
示例13: decode
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
public String decode(String encodedFolderName) throws CharacterCodingException {
CharsetDecoder decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
ByteBuffer byteBuffer = ByteBuffer.wrap(encodedFolderName.getBytes(asciiCharset));
CharBuffer charBuffer = decoder.decode(byteBuffer);
return charBuffer.toString();
}
示例14: encodePassword
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
private static byte[] encodePassword(char[] pwd, Charset cs) throws IOException {
ByteBuffer pwdBytes =
cs.newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.encode(CharBuffer.wrap(pwd));
byte[] encoded = new byte[pwdBytes.remaining()];
pwdBytes.get(encoded);
return encoded;
}
示例15: createEncoder
import java.nio.charset.CodingErrorAction; //導入依賴的package包/類
public static CharsetEncoder createEncoder(final ConnectionConfig cconfig) {
if (cconfig == null) {
return null;
}
final Charset charset = cconfig.getCharset();
if (charset != null) {
final CodingErrorAction malformed = cconfig.getMalformedInputAction();
final CodingErrorAction unmappable = cconfig.getUnmappableInputAction();
return charset.newEncoder()
.onMalformedInput(malformed != null ? malformed : CodingErrorAction.REPORT)
.onUnmappableCharacter(unmappable != null ? unmappable: CodingErrorAction.REPORT);
} else {
return null;
}
}