本文整理汇总了Java中java.nio.charset.CharsetDecoder类的典型用法代码示例。如果您正苦于以下问题:Java CharsetDecoder类的具体用法?Java CharsetDecoder怎么用?Java CharsetDecoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CharsetDecoder类属于java.nio.charset包,在下文中一共展示了CharsetDecoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toString
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
String toString(byte[] ba, int length) {
CharsetDecoder cd = decoder().reset();
int len = (int)(length * cd.maxCharsPerByte());
char[] ca = new char[len];
if (len == 0)
return new String(ca);
ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = cd.decode(bb, cb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = cd.flush(cb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
return new String(ca, 0, cb.position());
}
示例2: LineReader
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
LineReader(CharsetDecoder decoder, InputStream stream) throws IOException {
isr = new InputStreamReader(stream, decoder);
try {
br = new BufferedReader(isr);
} catch (Throwable t) {
if (isr != null) {
try {
isr.close();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
throw new IOException(t);
}
}
示例3: decodePassword
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
static String decodePassword(byte[] bytes) throws CharacterCodingException {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); // NOI18N
ByteBuffer input = ByteBuffer.wrap(bytes);
int outputLength = (int)(bytes.length * (double)decoder.maxCharsPerByte());
if (outputLength == 0) {
return ""; // NOI18N
}
char[] chars = new char[outputLength];
CharBuffer output = CharBuffer.wrap(chars);
CoderResult result = decoder.decode(input, output, true);
if (!result.isError() && !result.isOverflow()) {
result = decoder.flush(output);
}
if (result.isError() || result.isOverflow()) {
throw new CharacterCodingException();
} else {
return new String(chars, 0, output.position());
}
}
示例4: 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;
}
示例5: SessionInputBufferImpl
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
/**
* Creates new instance of SessionInputBufferImpl.
*
* @param metrics HTTP transport metrics.
* @param buffersize buffer size. Must be a positive number.
* @param minChunkLimit size limit below which data chunks should be buffered in memory
* in order to minimize native method invocations on the underlying network socket.
* The optimal value of this parameter can be platform specific and defines a trade-off
* between performance of memory copy operations and that of native method invocation.
* If negative default chunk limited will be used.
* @param constraints Message constraints. If <code>null</code>
* {@link MessageConstraints#DEFAULT} will be used.
* @param chardecoder chardecoder to be used for decoding HTTP protocol elements.
* If <code>null</code> simple type cast will be used for byte to char conversion.
*/
public SessionInputBufferImpl(
final HttpTransportMetricsImpl metrics,
final int buffersize,
final int minChunkLimit,
final MessageConstraints constraints,
final CharsetDecoder chardecoder) {
Args.notNull(metrics, "HTTP transport metrcis");
Args.positive(buffersize, "Buffer size");
this.metrics = metrics;
this.buffer = new byte[buffersize];
this.bufferpos = 0;
this.bufferlen = 0;
this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512;
this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
this.linebuffer = new ByteArrayBuffer(buffersize);
this.decoder = chardecoder;
}
示例6: BHttpConnectionBase
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
/**
* Creates new instance of BHttpConnectionBase.
*
* @param buffersize buffer size. Must be a positive number.
* @param fragmentSizeHint fragment size hint.
* @param chardecoder decoder to be used for decoding HTTP protocol elements.
* If <code>null</code> simple type cast will be used for byte to char conversion.
* @param charencoder encoder to be used for encoding HTTP protocol elements.
* If <code>null</code> simple type cast will be used for char to byte conversion.
* @param constraints Message constraints. If <code>null</code>
* {@link MessageConstraints#DEFAULT} will be used.
* @param incomingContentStrategy incoming content length strategy. If <code>null</code>
* {@link LaxContentLengthStrategy#INSTANCE} will be used.
* @param outgoingContentStrategy outgoing content length strategy. If <code>null</code>
* {@link StrictContentLengthStrategy#INSTANCE} will be used.
*/
protected BHttpConnectionBase(
final int buffersize,
final int fragmentSizeHint,
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
final MessageConstraints constraints,
final ContentLengthStrategy incomingContentStrategy,
final ContentLengthStrategy outgoingContentStrategy) {
super();
Args.positive(buffersize, "Buffer size");
final HttpTransportMetricsImpl inTransportMetrics = new HttpTransportMetricsImpl();
final HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl();
this.inbuffer = new SessionInputBufferImpl(inTransportMetrics, buffersize, -1,
constraints != null ? constraints : MessageConstraints.DEFAULT, chardecoder);
this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, buffersize, fragmentSizeHint,
charencoder);
this.connMetrics = new HttpConnectionMetricsImpl(inTransportMetrics, outTransportMetrics);
this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy :
LaxContentLengthStrategy.INSTANCE;
this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
StrictContentLengthStrategy.INSTANCE;
}
示例7: getJavaEncoding
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
/**
* Retrieves the CharsetDecoder for the given encoding. Note, This isn't perfect as I think ISCII-DEVANAGARI and
* MICROSOFT-CP1251 etc are allowed...
*
* @param encoding Encoding to retrieve the CharsetDecoder for
* @return CharSetDecoder for the given encoding
*/
private CharsetDecoder getJavaEncoding(String encoding) {
if ("ISO8859-14".equals(encoding)) {
return new ISO8859_14Decoder();
}
String canon = CHARSET_ALIASES.get(encoding);
if (canon != null) {
encoding = canon;
}
Charset charset = Charset.forName(encoding);
return charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE);
}
示例8: 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);
}
}
示例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);
// 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;
}
示例10: BufferedCharSequence
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
/**
* Creates {@code BufferedCharSequence} for specified FileObject {@code fo}.
*
* @param fo FileObject containing character data.
* @param decoder Charset decoder.
* @param size file size.
*/
public BufferedCharSequence(final FileObject fo,
CharsetDecoder decoder, long size) {
this.source = new Source(fo, size);
this.decoder = decoder;
this.sink = new Sink(this.source);
LOG.log(Level.FINER, "<init> {0}; decoder = {1}; {2}", // NOI18N
new Object[]{this.source, this.decoder, this.sink});
}
示例11: decoder
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
private CharsetDecoder decoder() {
if (dec == null) {
dec = cs.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
}
return dec;
}
示例12: getUTF8Decoder
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
public static CharsetDecoder getUTF8Decoder() {
CharsetDecoder decoder = (CharsetDecoder) decoderLocal.get();
if (decoder != null) {
return decoder;
}
decoder = new UTF8Decoder();
decoderLocal.set(decoder);
return decoder;
}
示例13: getRealCharsetName
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
private String getRealCharsetName (CharsetDecoder decoder) {
if (decoder instanceof ContentBaseEncodingQuery.SniffingDecoder) {
decoder = ((ContentBaseEncodingQuery.SniffingDecoder)decoder).decoder;
assertNotNull(decoder);
}
return decoder.charset().name();
}
示例14: testRoundTrip
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
@Test
public void testRoundTrip() throws CharacterCodingException {
final CharsetDecoder dec = charset.newDecoder();
final CharsetEncoder enc = charset.newEncoder();
final byte[] b1 = new byte[256];
for (int i = 0; i < b1.length; i++)
b1[i] = (byte) i;
final ByteBuffer bb1 = ByteBuffer.wrap(b1);
final CharBuffer cb = dec.decode(bb1);
final ByteBuffer bb2 = enc.encode(cb);
final byte[] b2 = bb2.array();
assertTrue(Arrays.equals(b1, b2));
}
示例15: parseObject
import java.nio.charset.CharsetDecoder; //导入依赖的package包/类
public static final <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) {
charsetDecoder.reset();
char[] chars = ThreadLocalCache.getChars((int) (((double) len) * ((double) charsetDecoder.maxCharsPerByte())));
ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
CharBuffer charByte = CharBuffer.wrap(chars);
IOUtils.decode(charsetDecoder, byteBuf, charByte);
return parseObject(chars, charByte.position(), clazz, features);
}