本文整理匯總了Java中java.nio.charset.CharsetDecoder.decode方法的典型用法代碼示例。如果您正苦於以下問題:Java CharsetDecoder.decode方法的具體用法?Java CharsetDecoder.decode怎麽用?Java CharsetDecoder.decode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.charset.CharsetDecoder
的用法示例。
在下文中一共展示了CharsetDecoder.decode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toString
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
String toString(byte[] ba) {
CharsetDecoder cd = decoder().reset();
int clen = (int)(ba.length * cd.maxCharsPerByte());
char[] ca = new char[clen];
if (clen == 0)
return new String(ca);
ByteBuffer bb = ByteBuffer.wrap(ba, 0, ba.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: 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());
}
}
示例3: canDecodeFile
import java.nio.charset.CharsetDecoder; //導入方法依賴的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;
}
示例4: getString
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
private static String getString(ByteBuffer buffer) {
Charset charset = null;
CharsetDecoder decoder = null;
CharBuffer charBuffer = null;
try {
charset = Charset.forName("UTF-8");
decoder = charset.newDecoder();
// 用這個的話,隻能輸出來一次結果,第二次顯示為空
// charBuffer = decoder.decode(buffer);
charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
return charBuffer.toString();
}
catch (Exception ex) {
ex.printStackTrace();
return "error";
}
}
示例5: detectingCharset
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
private static String detectingCharset(byte[] bytes) throws Exception {
//----------------------------------------------------------------
// Test special public methods of CharsetDecoder while we're here
//----------------------------------------------------------------
CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
check(cd.isAutoDetecting(), "isAutodecting()");
check(! cd.isCharsetDetected(), "isCharsetDetected");
cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
check(! cd.isCharsetDetected(), "isCharsetDetected");
try {
cd.detectedCharset();
fail("no IllegalStateException");
} catch (IllegalStateException e) {}
cd.decode(ByteBuffer.wrap(bytes));
check(cd.isCharsetDetected(), "isCharsetDetected");
Charset cs = cd.detectedCharset();
check(cs != null, "cs != null");
check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
return cs.name();
}
示例6: checkForUtf8
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
/**
* Checks the given file if it is encoded in UTF-8
*
* @param inputfile File
* @throws Exception if the File is not encoded in UTF-8 an Exception will be thrown
*/
public static void checkForUtf8(File inputfile)
throws Exception {
byte[] buffer = new byte[256];
int fileEnd =-1;
FileInputStream sqlFileInputStream = new FileInputStream(inputfile);
BufferedInputStream bufferedInputFileStream = new BufferedInputStream(sqlFileInputStream);
CharsetDecoder decoder = createCharsetDecoder();
int lineBytes = bufferedInputFileStream.read(buffer);
while (lineBytes != fileEnd) {
try {
decoder.decode(ByteBuffer.wrap(buffer));
lineBytes = bufferedInputFileStream.read(buffer);
} catch (CharacterCodingException e) {
throw new GretlException("Wrong encoding (not UTF-8) detected in File " + inputfile.getAbsolutePath());
}
}
}
示例7: 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());
}
示例8: decodeString
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
private static String decodeString(ByteBuffer src) throws CharacterCodingException
{
// the decoder needs to be reset every time we use it, hence the copy per thread
CharsetDecoder theDecoder = TL_UTF8_DECODER.get();
theDecoder.reset();
CharBuffer dst = TL_CHAR_BUFFER.get();
int capacity = (int) ((double) src.remaining() * theDecoder.maxCharsPerByte());
if (dst == null)
{
capacity = Math.max(capacity, 4096);
dst = CharBuffer.allocate(capacity);
TL_CHAR_BUFFER.set(dst);
}
else
{
dst.clear();
if (dst.capacity() < capacity)
{
dst = CharBuffer.allocate(capacity);
TL_CHAR_BUFFER.set(dst);
}
}
CoderResult cr = theDecoder.decode(src, dst, true);
if (!cr.isUnderflow())
cr.throwException();
return dst.flip().toString();
}
示例9: decodeJson
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
public static WsPacket decodeJson(ByteBuffer buffer) {
try {
Charset charset = Charset.forName(ENCODE);
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer);
return decodeJson(charBuffer.toString());
} catch (Exception e) {
if (WSManager.log != null) {
WSManager.log.error("json轉換成protobuf異常", e);
}
return null;
}
}
示例10: decode
import java.nio.charset.CharsetDecoder; //導入方法依賴的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) {
throw new JSONException(x.getMessage(), x);
}
}
示例11: utf8ToString
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
public static String utf8ToString(byte[] utf8Data, int offset, int length)
throws CharacterCodingException {
// NOTE: Using the String(..., UTF8) constructor would be wrong. That method will
// ignore UTF-8 errors in the input.
CharsetDecoder decoder = UTF8.newDecoder();
CharBuffer result = decoder.decode(ByteBuffer.wrap(utf8Data, offset, length));
return result.toString();
}
示例12: 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));
}
示例13: receive
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
@Override
public void receive(ByteBuffer bb, String channelid) {
if (host != null || !host.isEmpty())
{
//byte[] data = new byte[bb.remaining()];
//bb.get(data);
if(this.getRunningState()==RunningState.STARTED){
if(token == null)
{
doHttp("", RequestType.GENERATE_TOKEN);
}
try{
CharsetDecoder decoder = getCharsetDecoder();
CharBuffer charBuffer = decoder.decode(bb);
String jsonString = charBuffer.toString();
doHttp(jsonString, RequestType.UPDATE);
}
catch(Exception e)
{
String errorMsg = LOGGER.translate("BUFFER_PARSING_ERROR", e.getMessage());
LOGGER.error(errorMsg);
LOGGER.info(e.getMessage(), e);
//errorMessage = errorMsg;
}
}
}
}
示例14: test
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
static void test(String expectedCharset, byte[] input) throws Exception {
Charset cs = Charset.forName("x-JISAutoDetect");
CharsetDecoder autoDetect = cs.newDecoder();
Charset cs2 = Charset.forName(expectedCharset);
CharsetDecoder decoder = cs2.newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer charOutput = CharBuffer.allocate(128);
CharBuffer charExpected = CharBuffer.allocate(128);
bb.put(input);
bb.flip();
bb.mark();
CoderResult result = autoDetect.decode(bb, charOutput, true);
checkCoderResult(result);
charOutput.flip();
String actual = charOutput.toString();
bb.reset();
result = decoder.decode(bb, charExpected, true);
checkCoderResult(result);
charExpected.flip();
String expected = charExpected.toString();
check(actual.equals(expected),
String.format("actual=%s expected=%s", actual, expected));
}
示例15: decode
import java.nio.charset.CharsetDecoder; //導入方法依賴的package包/類
private CharSequence decode(ByteBuffer buf)
{
// Decode a byte buffer into a CharBuffer
CharBuffer isodcb = null;
CharsetDecoder isodecoder = charset.newDecoder();
try {
isodcb = isodecoder.decode(buf);
} catch (CharacterCodingException e) {
log.error(e);
}
return (CharSequence)isodcb;
}