当前位置: 首页>>代码示例>>Java>>正文


Java CharsetDecoder.decode方法代码示例

本文整理汇总了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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ZipCoder.java

示例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());
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:DatabaseConnectionConvertor.java

示例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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:HtmlEditorSupport.java

示例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";
    }
}
 
开发者ID:BriData,项目名称:DBus,代码行数:18,代码来源:MessageScheme.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:NIOJISAutoDetectTest.java

示例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());
        }
    }
}
 
开发者ID:sogis,项目名称:gretl,代码行数:30,代码来源:FileStylingDefinition.java

示例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());
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:17,代码来源:ZipCoder.java

示例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();
}
 
开发者ID:Netflix,项目名称:sstable-adaptor,代码行数:29,代码来源:CBUtil.java

示例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;
		}

	}
 
开发者ID:dianbaer,项目名称:grain,代码行数:16,代码来源:WSCodeUtil.java

示例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);
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:15,代码来源:IOUtils.java

示例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();
}
 
开发者ID:yifangyun,项目名称:fangcloud-java-sdk,代码行数:9,代码来源:StringUtil.java

示例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));
}
 
开发者ID:christian-schlichtherle,项目名称:truevfs,代码行数:14,代码来源:OctetCharsetTestSuite.java

示例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;
			}
		}
	}
	
}
 
开发者ID:Esri,项目名称:defense-solutions-proofs-of-concept,代码行数:29,代码来源:MLOBIOutboundTransport.java

示例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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:31,代码来源:NIOJISAutoDetectTest.java

示例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;
}
 
开发者ID:Esri,项目名称:defense-solutions-proofs-of-concept,代码行数:13,代码来源:RegexTextInboundAdapter.java


注:本文中的java.nio.charset.CharsetDecoder.decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。