Java Code Examples for java.nio.charset.StandardCharsets#UTF_16

The following examples show how to use java.nio.charset.StandardCharsets#UTF_16 . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Tomcat8-Source-Read   File: TestB2CConverter.java    License: MIT License 6 votes vote down vote up
private void testMessages(int msgCount) throws Exception {
    B2CConverter conv = new B2CConverter(StandardCharsets.UTF_16);

    ByteChunk bc = new ByteChunk();
    CharChunk cc = new CharChunk(32);


    for (int i = 0; i < msgCount; i++) {
        bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length);
        conv.convert(bc, cc, true);
        Assert.assertEquals("ABC", cc.toString());
        bc.recycle();
        cc.recycle();
        conv.recycle();
    }

    System.out.println(cc);
}
 
Example 2
Source Project: ghidra   File: MemSearchAsciiTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void setEncoding(Charset encoding) throws Exception {
	JComboBox<Charset> encodingOptions =
		(JComboBox<Charset>) findComponentByName(pane, "Encoding Options", false);

	// Makes encoding UTF_16 in case encoding is UTF_16BE or UTF_16LE
	// BE and LE are not choices in the combo box.
	if (encoding == StandardCharsets.UTF_16BE || encoding == StandardCharsets.UTF_16LE) {
		encoding = StandardCharsets.UTF_16;
	}

	for (int i = 0; i < encodingOptions.getItemCount(); i++) {
		if (encodingOptions.getItemAt(i) == encoding) {
			int index = i;
			runSwing(() -> encodingOptions.setSelectedIndex(index));
			break;
		}
	}
}
 
Example 3
@Test
public void edgeVersionTest() throws Exception {
    Config config = new Config();
    HttpClient httpClient = new HttpClient(config);
    VersionDetector versionDetector = new VersionDetector(config,
            httpClient);
    Optional<String> driverVersion = Optional.empty();
    URL driverUrl = new URL("https://msedgedriver.azureedge.net/");
    Charset versionCharset = StandardCharsets.UTF_16;
    String driverName = "msedgedriver";
    String versionLabel = "LATEST_STABLE";

    Optional<String> driverVersionFromRepository = versionDetector
            .getDriverVersionFromRepository(driverVersion, driverUrl,
                    versionCharset, driverName, versionLabel, versionLabel);
    assertTrue(driverVersionFromRepository.isPresent());
    String edgeVersion = driverVersionFromRepository.get();
    log.debug("driverVersionFromRepository {}", edgeVersion);

    List<String> driverVersions = WebDriverManager.edgedriver()
            .getDriverVersions();
    log.debug("All driverUrls {}", driverVersions);
    assertTrue(format("Stable version (%s) is not in the URL list",
            edgeVersion), driverVersions.contains(edgeVersion));

}
 
Example 4
@Test
public void defaultCharsetModified() throws IOException {
	ConversionService cs = new DefaultConversionService();
	ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, StandardCharsets.UTF_16);
	converter.write((byte) 31, null, this.response);

	assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
}
 
Example 5
@Test
public void writeUtf16() throws IOException {
	MediaType contentType = new MediaType("text", "plain", StandardCharsets.UTF_16);
	this.converter.write(Integer.valueOf(958), contentType, this.response);

	assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
	assertTrue(this.servletResponse.getContentType().startsWith(MediaType.TEXT_PLAIN_VALUE));
	assertEquals(8, this.servletResponse.getContentLength());
	// First two bytes: byte order mark
	assertArrayEquals(new byte[] { -2, -1, 0, '9', 0, '5', 0, '8' }, this.servletResponse.getContentAsByteArray());
}
 
Example 6
@Test
public void defaultCharsetModified() throws IOException {
	ConversionService cs = new DefaultConversionService();
	ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, StandardCharsets.UTF_16);
	converter.write((byte) 31, null, this.response);

	assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
}
 
Example 7
@Test
public void writeUtf16() throws IOException {
	MediaType contentType = new MediaType("text", "plain", StandardCharsets.UTF_16);
	this.converter.write(Integer.valueOf(958), contentType, this.response);

	assertEquals("UTF-16", this.servletResponse.getCharacterEncoding());
	assertTrue(this.servletResponse.getContentType().startsWith(MediaType.TEXT_PLAIN_VALUE));
	assertEquals(8, this.servletResponse.getContentLength());
	// First two bytes: byte order mark
	assertArrayEquals(new byte[] { -2, -1, 0, '9', 0, '5', 0, '8' }, this.servletResponse.getContentAsByteArray());
}
 
Example 8
private void load(final String table, final File file) throws IOException {
    try (final LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_16))) {
        String line;
        while((line = reader.readLine()) != null) {
            final Matcher matcher = pattern.matcher(line);
            if(matcher.matches()) {
                cache.put(new Key(table, matcher.group(1)), matcher.group(2));
            }
        }
    }
}
 
Example 9
Source Project: bytes-java   File: UtilConverterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCharToByteArray() {
    Charset[] charsets = new Charset[]{StandardCharsets.UTF_8, StandardCharsets.US_ASCII, StandardCharsets.UTF_16};
    for (Charset charset : charsets) {
        checkCharArrayToByteArray("".toCharArray(), charset);
        checkCharArrayToByteArray("A".toCharArray(), charset);
        checkCharArrayToByteArray("12".toCharArray(), charset);
        checkCharArrayToByteArray("XYZ".toCharArray(), charset);
        checkCharArrayToByteArray("abcdefg".toCharArray(), charset);
        checkCharArrayToByteArray("71oh872gdl2dhp81g".toCharArray(), charset);

    }

    checkCharArrayToByteArray("யe2ாமறிந்தиют убSîne klâwenasd1".toCharArray(), StandardCharsets.UTF_8);
}
 
Example 10
Source Project: OsmGo   File: Filesystem.java    License: MIT License 5 votes vote down vote up
private Charset getEncoding(String encoding) {
  if (encoding == null) {
    return null;
  }

  switch(encoding) {
    case "utf8":
      return StandardCharsets.UTF_8;
    case "utf16":
      return StandardCharsets.UTF_16;
    case "ascii":
      return StandardCharsets.US_ASCII;
  }
  return null;
}
 
Example 11
@Test
public void wrongEncodingTest1() throws Exception {
	File f = getFile("encodingTestBad.xml");
	Writer writer = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_16);
	//purposefully write the XML in UTF-16 to make sure that the XmlReader will re-encode the file in UTF-8
	writer.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
	writer.write("<bad-encoding-test>[email protected]#$%^&amp;*()_+`1234567890-=[]{}\\|&lt;&gt;?,./abcdefÄä</bad-encoding-test>");
	writer.flush();
	writer.close();

	XmlReader loader = new XmlReader(f);
	assertEquals("bad-encoding-test", loader.getDocument().getDocumentElement().getTagName());
	assertEquals("[email protected]#$%^&*()_+`1234567890-=[]{}\\|<>?,./abcdefÄä", XmlUtil.getImmediateTextContent(loader.getDocumentElement()));
}
 
Example 12
Source Project: pulsar   File: DefaultSchemasTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStringSchema() throws Exception {
    String testString = "hello world";
    byte[] testBytes = testString.getBytes(StandardCharsets.UTF_8);
    StringSchema stringSchema = new StringSchema();
    assertEquals(testString, stringSchema.decode(testBytes));
    assertEquals(stringSchema.encode(testString), testBytes);

     byte[] bytes2 = testString.getBytes(StandardCharsets.UTF_16);
    StringSchema stringSchemaUtf16 = new StringSchema(StandardCharsets.UTF_16);
    assertEquals(testString, stringSchemaUtf16.decode(bytes2));
    assertEquals(stringSchemaUtf16.encode(testString), bytes2);
}
 
Example 13
Source Project: nifi   File: HashService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the raw {@code byte[]} hash of the specified value.
 *
 * @param algorithm the hash algorithm to use
 * @param value     the value to hash (cannot be {@code null} but can be an empty String)
 * @param charset   the charset to use
 * @return the hash value in bytes
 */
public static byte[] hashValueRaw(HashAlgorithm algorithm, String value, Charset charset) {
    if (value == null) {
        throw new IllegalArgumentException("The value cannot be null");
    }
    /** See the note on {@link HashServiceTest#testHashValueShouldHandleUTF16BOMIssue()} */
    if (charset == StandardCharsets.UTF_16) {
        logger.warn("The charset provided was UTF-16, but Java will insert a Big Endian BOM in the decoded message before hashing, so switching to UTF-16BE");
        charset = StandardCharsets.UTF_16BE;
    }
    return hashValueRaw(algorithm, value.getBytes(charset));
}
 
Example 14
Source Project: tutorials   File: StringToByteArrayUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenGetBytesWithStandardCharset_thenOK() {
    final String inputString = "Hello World!";
    final Charset charset = StandardCharsets.UTF_16;

    byte[] byteArrray = inputString.getBytes(charset);

    System.out.printf(
        "Using Standard Charset:%s, Input String:%s, Output byte array:%s\n",
        charset, inputString, Arrays.toString(byteArrray));

    assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
            111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 },
        byteArrray);
}
 
Example 15
Source Project: tutorials   File: ByteArrayToStringUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenStringConstructorWithStandardCharSet_thenOK() {
    final Charset charset = StandardCharsets.UTF_16;
    
    final byte[] byteArrray = { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
            111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 };

    String string = new String(byteArrray, charset);

    assertEquals("Hello World!", string);
}
 
Example 16
Source Project: ghidra   File: AsciiSearchFormat.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean usesEndieness() {
	return encodingCB.getSelectedItem() == StandardCharsets.UTF_16; // Only UTF-16 uses Endianness.
}
 
Example 17
Source Project: ghidra   File: AsciiSearchFormat.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SearchData getSearchData(String input) {
	final byte MASK_BYTE = (byte) 0xdf;

	int inputLength = input.length();
	Charset encodingSelection = (Charset) encodingCB.getSelectedItem();
	if (encodingSelection == StandardCharsets.UTF_16) {
		encodingSelection =
			(isBigEndian) ? StandardCharsets.UTF_16BE : StandardCharsets.UTF_16LE;
	}

	//Escape sequences in the "input" are 2 Characters long.
	if (escapeSequencesCkB.isSelected() && inputLength >= 2) {
		input = StringUtilities.convertEscapeSequences(input);
	}
	byte[] byteArray = input.getBytes(encodingSelection);
	byte[] maskArray = new byte[byteArray.length];
	Arrays.fill(maskArray, (byte) 0xff);

	// Time to mask some bytes for case insensitive searching.
	if (!caseSensitiveCkB.isSelected()) {
		int i = 0;
		while (i < byteArray.length) {
			if (encodingSelection == StandardCharsets.US_ASCII &&
				Character.isLetter(byteArray[i])) {
				maskArray[i] = MASK_BYTE;
				i++;
			}
			else if (encodingSelection == StandardCharsets.UTF_8) {
				int numBytes = bytesPerCharUTF8(byteArray[i]);
				if (numBytes == 1 && Character.isLetter(byteArray[i])) {
					maskArray[i] = MASK_BYTE;
				}
				i += numBytes;
			}
			// Assumes UTF-16 will return 2 Bytes for each character.
			// 4-byte UTF-16 will never satisfy the below checks because
			// none of their bytes can ever be 0.
			else if (encodingSelection == StandardCharsets.UTF_16BE) {
				if (byteArray[i] == (byte) 0x0 && Character.isLetter(byteArray[i + 1])) { // Checks if ascii character.
					maskArray[i + 1] = MASK_BYTE;
				}
				i += 2;
			}
			else if (encodingSelection == StandardCharsets.UTF_16LE) {
				if (byteArray[i + 1] == (byte) 0x0 && Character.isLetter(byteArray[i])) { // Checks if ascii character.
					maskArray[i] = MASK_BYTE;
				}
				i += 2;
			}
			else {
				i++;
			}
		}
	}

	return SearchData.createSearchData(input, byteArray, maskArray);
}
 
Example 18
Source Project: qpid-broker-j   File: AbstractDecoder.java    License: Apache License 2.0 4 votes vote down vote up
private Object read(Type t)
{
    switch (t)
    {
    case BIN8:
    case UINT8:
        return readUint8();
    case INT8:
        return get();
    case CHAR:
        return (char) get();
    case BOOLEAN:
        return get() > 0;

    case BIN16:
    case UINT16:
        return readUint16();

    case INT16:
        return (short) readUint16();

    case BIN32:
    case UINT32:
        return readUint32();

    case CHAR_UTF32:
    case INT32:
        return (int) readUint32();

    case FLOAT:
        return Float.intBitsToFloat((int) readUint32());

    case BIN64:
    case UINT64:
    case INT64:
    case DATETIME:
        return readUint64();

    case DOUBLE:
        return Double.longBitsToDouble(readUint64());

    case UUID:
        return readUuid();

    case STR8:
        return readStr8();

    case STR16:
        return readStr16();

    case STR8_LATIN:
    case STR16_LATIN:
        Charset charset;
        try
        {
            charset = Charset.forName("ISO-8859-15");
        }
        catch (UnsupportedCharsetException e)
        {
            // We do not want to start throwing execptions from here so we fall back to ISO_8859_1
            charset = StandardCharsets.ISO_8859_1;
        }
        return new String(readBytes(t), charset);

    case STR8_UTF16:
    case STR16_UTF16:
        return new String(readBytes(t), StandardCharsets.UTF_16);

    case MAP:
        return readMap();
    case LIST:
        return readList();
    case ARRAY:
        return readArray();
    case STRUCT32:
        return readStruct32();

    case BIN40:
    case DEC32:
    case BIN72:
    case DEC64:
        // XXX: what types are we supposed to use here?
        return readBytes(t);

    case VOID:
        return null;

    default:
        return readBytes(t);
    }
}
 
Example 19
@Override
protected Charset getVersionCharset() {
    return StandardCharsets.UTF_16;
}
 
Example 20
Source Project: ghidra   File: CliStreamUserStrings.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Gets the user string at the given index.
 * 
 * @param index The index of the user string to get.
 * @return The user string at the given index.  Could be null if the index was invalid or
 *   there was a problem reading the user string.
 */
public String getUserString(int index) {
	byte[] bytes = blobMap.get(index).getContents();
	return new String(bytes, 0, bytes.length - 1, StandardCharsets.UTF_16);
}