Search in sources :

Example 76 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project jdk8u_jdk by JetBrains.

the class NIOJISAutoDetectTest method test.

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

Example 77 with CharsetDecoder

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

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 78 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project zm-mailbox by Zimbra.

the class ZipUtil method convertBytesIfPossible.

private static String convertBytesIfPossible(byte[] rawBytes, Charset cset) {
    CharsetDecoder charsetDecoder = reportingDecoder(cset);
    try {
        CharBuffer cb = charsetDecoder.decode(ByteBuffer.wrap(rawBytes));
        String val = cb.toString();
        ZimbraLog.misc.debug("ZipUtil name '%s' decoded from Charset='%s'", val, cset.name());
        return val;
    } catch (Exception ex) {
        ZimbraLog.misc.trace("ZipUtil failed decode from Charset='%s' %s", cset.name(), ex.getMessage());
    }
    return null;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException)

Example 79 with CharsetDecoder

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

the class VisorTaskUtils method decode.

/**
     * Decode file charset.
     *
     * @param f File to process.
     * @return File charset.
     * @throws IOException in case of error.
     */
public static Charset decode(File f) throws IOException {
    SortedMap<String, Charset> charsets = Charset.availableCharsets();
    String[] firstCharsets = { Charset.defaultCharset().name(), "US-ASCII", "UTF-8", "UTF-16BE", "UTF-16LE" };
    Collection<Charset> orderedCharsets = U.newLinkedHashSet(charsets.size());
    for (String c : firstCharsets) if (charsets.containsKey(c))
        orderedCharsets.add(charsets.get(c));
    orderedCharsets.addAll(charsets.values());
    try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
        FileChannel ch = raf.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(DFLT_BUFFER_SIZE);
        ch.read(buf);
        buf.flip();
        for (Charset charset : orderedCharsets) {
            CharsetDecoder decoder = charset.newDecoder();
            decoder.reset();
            try {
                decoder.decode(buf);
                return charset;
            } catch (CharacterCodingException ignored) {
            }
        }
    }
    return Charset.defaultCharset();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 80 with CharsetDecoder

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

the class Bytes method fromByteBuffer.

/** Decode a string into a ByteBuffer */
public static String fromByteBuffer(ByteBuffer bb) {
    //return BlockUTF8.toString(bb) ;
    // To be removed (Dec 2011)
    CharsetDecoder dec = Chars.allocDecoder();
    String x = fromByteBuffer(bb, dec);
    Chars.deallocDecoder(dec);
    return x;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder)

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