Search in sources :

Example 41 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project android_frameworks_base by crdroidandroid.

the class WifiSsid method toString.

@Override
public String toString() {
    byte[] ssidBytes = octets.toByteArray();
    // behavior of returning empty string for this case.
    if (octets.size() <= 0 || isArrayAllZeroes(ssidBytes))
        return "";
    // TODO: Handle conversion to other charsets upon failure
    Charset charset = Charset.forName("UTF-8");
    CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    CharBuffer out = CharBuffer.allocate(32);
    CoderResult result = decoder.decode(ByteBuffer.wrap(ssidBytes), out, true);
    out.flip();
    if (result.isError()) {
        return NONE;
    }
    return out.toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CoderResult(java.nio.charset.CoderResult)

Example 42 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project dropbox-sdk-java by dropbox.

the class StringUtil method utf8ToString.

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer)

Example 43 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project tomcat by apache.

the class TestUtf8 method testJvmDecoder.

@Test
public void testJvmDecoder() {
    CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
    int testCount = 0;
    try {
        for (Utf8TestCase testCase : TEST_CASES) {
            doTest(decoder, testCase, testCase.flagsJvm);
            testCount++;
        }
    } finally {
        System.err.println("Workarounds added to " + workAroundCount + " tests to account for known JVM bugs");
        if (testCount < TEST_CASES.size()) {
            System.err.println("Executed " + testCount + " of " + TEST_CASES.size() + " UTF-8 tests before " + "encountering a failure");
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) Test(org.junit.Test)

Example 44 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project gitblit by gitblit.

the class StringUtils method decodeString.

/**
	 * Decodes a string by trying several charsets until one does not throw a
	 * coding exception.  Last resort is to interpret as UTF-8 with illegal
	 * character substitution.
	 *
	 * @param content
	 * @param charsets optional
	 * @return a string
	 */
public static String decodeString(byte[] content, String... charsets) {
    Set<String> sets = new LinkedHashSet<String>();
    if (!ArrayUtils.isEmpty(charsets)) {
        sets.addAll(Arrays.asList(charsets));
    }
    String value = null;
    sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
    for (String charset : sets) {
        try {
            Charset cs = Charset.forName(charset);
            CharsetDecoder decoder = cs.newDecoder();
            CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
            value = buffer.toString();
            break;
        } catch (CharacterCodingException e) {
        // ignore and advance to the next charset
        } catch (IllegalCharsetNameException e) {
        // ignore illegal charset names
        } catch (UnsupportedCharsetException e) {
        // ignore unsupported charsets
        }
    }
    if (value != null && value.startsWith("")) {
        // strip UTF-8 BOM
        return value.substring(1);
    }
    return value;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 45 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project buck by facebook.

the class UnixArchive method loadEntries.

@SuppressWarnings("PMD.PrematureDeclaration")
private ImmutableList<UnixArchiveEntry> loadEntries() {
    int offset = EXPECTED_GLOBAL_HEADER.length;
    int headerOffset = offset;
    ImmutableList.Builder<UnixArchiveEntry> builder = ImmutableList.builder();
    CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
    while (true) {
        buffer.position(offset);
        byte[] markerBytes = new byte[ENTRY_MARKER.length];
        buffer.get(markerBytes, 0, markerBytes.length);
        if (!Arrays.equals(markerBytes, ENTRY_MARKER)) {
            throw new HumanReadableException("Unknown entry marker");
        }
        int filenameLength = getIntFromStringAtRange(LENGTH_OF_FILENAME_SIZE, decoder);
        long fileModification = getLongFromStringAtRange(MODIFICATION_TIME_SIZE, decoder);
        int ownerId = getIntFromStringAtRange(OWNER_ID_SIZE, decoder);
        int groupId = getIntFromStringAtRange(GROUP_ID_SIZE, decoder);
        int fileMode = getIntFromStringAtRange(FILE_MODE_SIZE, decoder);
        int fileAndFilenameSize = getIntFromStringAtRange(FILE_AND_FILENAME_SIZE, decoder);
        byte[] magic = new byte[END_OF_HEADER_MAGIC_SIZE];
        buffer.get(magic, 0, magic.length);
        if (!Arrays.equals(magic, END_OF_HEADER_MAGIC)) {
            throw new HumanReadableException("Unknown file magic");
        }
        long fileSizeWithoutFilename = fileAndFilenameSize - filenameLength;
        offset = buffer.position();
        String filename;
        try {
            filename = nulTerminatedCharsetDecoder.decodeString(buffer);
        } catch (CharacterCodingException e) {
            throw new HumanReadableException(e, "Unable to read filename from buffer starting at %d", offset);
        }
        offset += filenameLength;
        builder.add(UnixArchiveEntry.of(filenameLength, fileModification, ownerId, groupId, fileMode, fileSizeWithoutFilename, filename, headerOffset, offset - headerOffset, offset));
        offset += fileSizeWithoutFilename;
        if (offset == buffer.capacity()) {
            break;
        }
    }
    return builder.build();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ImmutableList(com.google.common.collect.ImmutableList) HumanReadableException(com.facebook.buck.util.HumanReadableException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Aggregations

CharsetDecoder (java.nio.charset.CharsetDecoder)90 CharBuffer (java.nio.CharBuffer)45 ByteBuffer (java.nio.ByteBuffer)33 CoderResult (java.nio.charset.CoderResult)25 Charset (java.nio.charset.Charset)24 InputStreamReader (java.io.InputStreamReader)11 CharacterCodingException (java.nio.charset.CharacterCodingException)9 IOException (java.io.IOException)8 BufferedReader (java.io.BufferedReader)5 Properties (java.util.Properties)5 RegisterRequestProcessor (com.linkedin.databus.container.request.RegisterRequestProcessor)4 LogicalSource (com.linkedin.databus.core.data_model.LogicalSource)4 ChunkedWritableByteChannel (com.linkedin.databus2.core.container.ChunkedWritableByteChannel)4 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)4 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)4 SourceIdNameRegistry (com.linkedin.databus2.schemas.SourceIdNameRegistry)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)4 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)3