當前位置: 首頁>>代碼示例>>Java>>正文


Java StandardCharsets.UTF_16BE屬性代碼示例

本文整理匯總了Java中java.nio.charset.StandardCharsets.UTF_16BE屬性的典型用法代碼示例。如果您正苦於以下問題:Java StandardCharsets.UTF_16BE屬性的具體用法?Java StandardCharsets.UTF_16BE怎麽用?Java StandardCharsets.UTF_16BE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在java.nio.charset.StandardCharsets的用法示例。


在下文中一共展示了StandardCharsets.UTF_16BE屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: byteToCharArray

private static char[] byteToCharArray(final byte[] bytes) {
    Charset cs = StandardCharsets.UTF_8;
    int start = 0;
    // BOM detection.
    if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
        start = 2;
        cs = StandardCharsets.UTF_16BE;
    } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
        start = 2;
        cs = StandardCharsets.UTF_16LE;
    } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
        start = 3;
        cs = StandardCharsets.UTF_8;
    } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) {
        start = 4;
        cs = Charset.forName("UTF-32LE");
    } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
        start = 4;
        cs = Charset.forName("UTF-32BE");
    }

    return new String(bytes, start, bytes.length - start, cs).toCharArray();
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:23,代碼來源:Source.java

示例2: byteToCharArray

private static char[] byteToCharArray(final byte[] bytes) {
    Charset cs = StandardCharsets.UTF_8;
    int start = 0;
    // BOM detection.
    if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
        start = 2;
        cs = StandardCharsets.UTF_16BE;
    } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
        if (bytes.length > 3 && bytes[2] == 0 && bytes[3] == 0) {
            start = 4;
            cs = Charset.forName("UTF-32LE");
        } else {
            start = 2;
            cs = StandardCharsets.UTF_16LE;
        }
    } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
        start = 3;
        cs = StandardCharsets.UTF_8;
    } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
        start = 4;
        cs = Charset.forName("UTF-32BE");
    }

    return new String(bytes, start, bytes.length - start, cs).toCharArray();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:Source.java

示例3: testAsciiExtensions

@Test
public void testAsciiExtensions(){
	Assert.assertTrue(unknownCharacterInt.equals(65533));

	for(int i = 0; i < 256; ++i){
		String ascii = new String(new byte[]{ByteTool.fromUnsignedInt0To255(i)}, StandardCharsets.US_ASCII);

		String latin1 = new String(new byte[]{ByteTool.fromUnsignedInt0To255(i)},
				StandardCharsets.ISO_8859_1);

		String windows1252 = new String(new byte[]{ByteTool.fromUnsignedInt0To255(i)}, Charset.forName(
				"windows-1252"));

		String utf16be = new String(new byte[]{(byte)0, ByteTool.fromUnsignedInt0To255(i)},
				StandardCharsets.UTF_16BE);

		String utf8 = new String(new byte[]{ByteTool.fromUnsignedInt0To255(i)}, StandardCharsets.UTF_8);

		if(i < 0x80){
			Assert.assertEquals(latin1, ascii);
			Assert.assertEquals(windows1252, latin1);
			Assert.assertEquals(utf16be, windows1252);
			Assert.assertEquals(utf8, utf16be);
		}else if(i < 160){
			Assert.assertEquals(unknownCharacter.toString(), ascii);// invalid octet
			Assert.assertEquals(latin1.charAt(0), i);// valid octet, but not not mapped to any character
			Assert.assertTrue(StringTool.notEmpty(windows1252));
			Assert.assertTrue(latin1.equals(utf16be));
			Assert.assertTrue(!windows1252.equals(utf16be));
			Assert.assertEquals(unknownCharacter.toString(), utf8);// utf8 will expect 2 bytes here, so our 1
																	// byte is junk
		}else{
			Assert.assertEquals(unknownCharacter.toString(), ascii);// invalid octet
			Assert.assertTrue(StringTool.notEmpty(latin1));
			Assert.assertTrue(StringTool.notEmpty(windows1252));
			Assert.assertEquals(windows1252, latin1);
			Assert.assertEquals(utf16be, windows1252);
			Assert.assertEquals(unknownCharacter.toString(), utf8);// utf8 will expect 2 bytes here, so our 1
																	// byte is junk
		}
	}
}
 
開發者ID:hotpads,項目名稱:datarouter,代碼行數:42,代碼來源:StringByteTool.java

示例4: readUnicodeEscape

/**
 * The next four chars must be hex digits referring to a 16-bit unicode escape
 * @return a 16-bit integer that must be encoded in UTF8
 * @throws IOException
 */
private String readUnicodeEscape() throws IOException {
	unicode[0] = (byte) ((readHexDigit() << 4) | (readHexDigit()));
	unicode[1] = (byte) ((readHexDigit() << 4) | (readHexDigit()));
	return new String(unicode, StandardCharsets.UTF_16BE);
}
 
開發者ID:Puppygames,項目名稱:thjson,代碼行數:10,代碼來源:THJSONTokenizer.java


注:本文中的java.nio.charset.StandardCharsets.UTF_16BE屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。