Example usage for java.nio.charset StandardCharsets UTF_16LE

List of usage examples for java.nio.charset StandardCharsets UTF_16LE

Introduction

In this page you can find the example usage for java.nio.charset StandardCharsets UTF_16LE.

Prototype

Charset UTF_16LE

To view the source code for java.nio.charset StandardCharsets UTF_16LE.

Click Source Link

Document

Sixteen-bit UCS Transformation Format, little-endian byte order.

Usage

From source file:org.sonar.scanner.scan.filesystem.CharsetValidationTest.java

@Test
public void detectUTF16Ascii() throws CharacterCodingException {
    String text = "some text to test";
    byte[] utf16be = encode(text, StandardCharsets.UTF_16BE);
    byte[] utf16le = encode(text, StandardCharsets.UTF_16LE);
    byte[] utf8 = encode(text, StandardCharsets.UTF_8);
    byte[] iso88591 = encode(text, StandardCharsets.ISO_8859_1);
    byte[] utf32 = encode(text, Charset.forName("UTF-32LE"));

    assertThat(charsets.isUTF16(utf16le, true).charset()).isEqualTo(StandardCharsets.UTF_16LE);
    assertThat(charsets.isUTF16(utf16be, true).charset()).isEqualTo(StandardCharsets.UTF_16BE);
    // not enough nulls -> we don't know
    assertThat(charsets.isUTF16(iso88591, true).valid()).isEqualTo(Validation.MAYBE);
    assertThat(charsets.isUTF16(utf8, true).valid()).isEqualTo(Validation.MAYBE);
    // fail based on double nulls
    assertThat(charsets.isUTF16(utf32, true).valid()).isEqualTo(Validation.NO);
}

From source file:com.github.anba.es6draft.chakra.ChakraTest.java

private static Charset charsetFor(BOMInputStream bis, Charset defaultCharset) throws IOException {
    ByteOrderMark bom = bis.getBOM();//from w  w  w .  j  a va 2 s . c  o  m
    if (ByteOrderMark.UTF_8.equals(bom)) {
        return StandardCharsets.UTF_8;
    }
    if (ByteOrderMark.UTF_16LE.equals(bom)) {
        return StandardCharsets.UTF_16LE;
    }
    if (ByteOrderMark.UTF_16BE.equals(bom)) {
        return StandardCharsets.UTF_16BE;
    }
    return defaultCharset;
}

From source file:org.polymap.p4.data.importer.prompts.CharsetPrompt.java

private void initCharsets() {
    charsets = new TreeMap<String, Charset>();

    for (Charset charset : Charset.availableCharsets().values()) {
        charsets.put(displayName(charset), charset);
    }/*from  ww w.  java2s . c om*/

    displayNames = new ListOrderedSet<String>();
    // add all defaults on top
    displayNames.add(displayName(StandardCharsets.ISO_8859_1));
    displayNames.add(displayName(StandardCharsets.US_ASCII));
    displayNames.add(displayName(StandardCharsets.UTF_8));
    displayNames.add(displayName(StandardCharsets.UTF_16));
    displayNames.add(displayName(StandardCharsets.UTF_16BE));
    displayNames.add(displayName(StandardCharsets.UTF_16LE));

    // a separator
    charsets.put(SEPARATOR, selection);
    displayNames.add(SEPARATOR);

    // add the rest
    for (String displayName : charsets.keySet()) {
        displayNames.add(displayName);
    }
}

From source file:com.microsoft.azure.management.datalake.store.uploader.StringExtensionsTests.java

@Test
public void StringExtensions_FindNewLine_UTF16() {
    for (Triple<String, Integer, Integer> t : TestDataUTF16) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16LE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE, null);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }/*from   w w  w.ja  v  a 2  s . c o  m*/

    for (Triple<String, Integer, Integer> t : TestDataUTF16CustomDelim) {
        byte[] exactBuffer = t.getLeft().getBytes(StandardCharsets.UTF_16LE);
        byte[] largerBuffer = new byte[exactBuffer.length + 100];
        System.arraycopy(exactBuffer, 0, largerBuffer, 0, exactBuffer.length);

        int forwardInExactBuffer = StringExtensions.findNewline(exactBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInExactBuffer);

        int forwardInLargeBuffer = StringExtensions.findNewline(largerBuffer, 0, exactBuffer.length, false,
                StandardCharsets.UTF_16LE, customDelim);
        Assert.assertEquals(t.getMiddle().intValue(), forwardInLargeBuffer);

        int reverseInExactBuffer = StringExtensions.findNewline(exactBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInExactBuffer);

        int reverseInLargeBuffer = StringExtensions.findNewline(largerBuffer,
                Math.max(0, exactBuffer.length - 1), exactBuffer.length, true, StandardCharsets.UTF_16LE,
                customDelim);
        Assert.assertEquals(t.getRight().intValue(), reverseInLargeBuffer);
    }
}

From source file:org.kitodo.production.services.data.LdapServerService.java

private BasicAttribute proceedPassword(String identifier, String newPassword, JDKMessageDigest.MD4 digester) {
    try {//from  w  w  w . j a  va 2  s  .c om
        byte[] hash;
        if (Objects.isNull(digester)) {
            hash = LdapUser.lmHash(newPassword);
        } else {
            hash = digester.digest(newPassword.getBytes(StandardCharsets.UTF_16LE));
        }
        return new BasicAttribute(identifier, LdapUser.toHexString(hash));
        // TODO: Don't catch super class exception, make sure that
        // the password isn't logged here
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
            | BadPaddingException | RuntimeException e) {
        logger.error(e.getMessage(), e);
        return null;
    }
}

From source file:com.sonymobile.android.media.internal.ISOBMFFParser.java

private String readID3String(int frameSize) {
    String metadataString = null;
    try {/*from  w  w  w.j a  va2 s  .c om*/
        int encoding = mDataSource.readByte();
        if (frameSize > 1) {
            if (encoding == 0) {
                // ISO 8859-1
                byte[] buffer = new byte[frameSize - 1];
                mDataSource.read(buffer);
                metadataString = new String(buffer, StandardCharsets.ISO_8859_1);
            } else if (encoding == 2) {
                // UTF-16
                int bom = mDataSource.readShort();
                byte[] buffer = new byte[frameSize - 3];
                short little_endian = (short) 0xFFFE;
                mDataSource.read(buffer);
                if (bom == little_endian) {
                    metadataString = new String(buffer, StandardCharsets.UTF_16LE);
                } else {
                    metadataString = new String(buffer, StandardCharsets.UTF_16BE);
                }
            } else if (encoding == 3) {
                // UTF-8
                byte[] buffer = new byte[frameSize - 1];
                mDataSource.read(buffer);
                metadataString = new String(buffer, StandardCharsets.UTF_8);
            } else {
                // UCS-2
                int bom = mDataSource.readShort();
                byte[] buffer = new byte[frameSize - 3];
                mDataSource.read(buffer);
                short little_endian = (short) 0xFFFE;
                if (bom == little_endian) {
                    for (int i = 0; i < buffer.length; i += 2) {
                        byte tempByte = buffer[i];
                        buffer[i] = buffer[i + 1];
                        buffer[i + 1] = tempByte;
                    }
                }
                metadataString = new String(buffer, "UCS-2");
            }
        }
    } catch (IOException e) {
        if (LOGS_ENABLED)
            Log.e(TAG, "IOEception reading ID3 string", e);
    }
    return metadataString;
}