Search in sources :

Example 56 with CharsetDecoder

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

the class Text method decode.

private static String decode(ByteBuffer utf8, boolean replace) throws CharacterCodingException {
    CharsetDecoder decoder = DECODER_FACTORY.get();
    if (replace) {
        decoder.onMalformedInput(java.nio.charset.CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    String str = decoder.decode(utf8).toString();
    // set decoder back to its default value: REPORT
    if (replace) {
        decoder.onMalformedInput(CodingErrorAction.REPORT);
        decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return str;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder)

Example 57 with CharsetDecoder

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

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

use of java.nio.charset.CharsetDecoder in project sessdb by ppdai.

the class Slices method decodeString.

public static String decodeString(ByteBuffer src, Charset charset) {
    final CharsetDecoder decoder = getDecoder(charset);
    final CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte()));
    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = decoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
    return dst.flip().toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 59 with CharsetDecoder

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

the class CharsetDecoderTest method test_decode.

/**
	 * @tests java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer)
	 */
public void test_decode() throws CharacterCodingException {
    // Regression for HARMONY-33
    //		ByteBuffer bb = ByteBuffer.allocate(1);
    //		bb.put(0, (byte) 77);
    //		CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
    //		decoder.onMalformedInput(CodingErrorAction.REPLACE);
    //		decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    //		decoder.decode(bb);
    // Regression for HARMONY-67
    //		byte[] b = new byte[] { (byte) 1 };
    //		ByteBuffer buf = ByteBuffer.wrap(b);
    //		CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
    //		assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
    //
    //		charbuf = Charset.forName("UTF-16BE").decode(buf);
    //		assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
    //
    //		charbuf = Charset.forName("UTF-16LE").decode(buf);
    //		assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
    // Regression for HARMONY-99
    CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
    decoder2.onMalformedInput(CodingErrorAction.REPORT);
    decoder2.onUnmappableCharacter(CodingErrorAction.REPORT);
    ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
    try {
        decoder2.decode(in);
        fail("Assert 3: MalformedInputException should have thrown");
    } catch (MalformedInputException e) {
    //expected
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 60 with CharsetDecoder

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

the class StringTest method test_23831.

// https://code.google.com/p/android/issues/detail?id=23831
public void test_23831() throws Exception {
    byte[] bytes = { (byte) 0xf5, (byte) 0xa9, (byte) 0xea, (byte) 0x21 };
    String expected = "��!";
    // Since we use icu4c for CharsetDecoder...
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    assertEquals(expected, decoder.decode(ByteBuffer.wrap(bytes)).toString());
    // Our fast-path code in String should behave the same...
    assertEquals(expected, new String(bytes, "UTF-8"));
}
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