Search in sources :

Example 36 with CharsetDecoder

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

the class ParseUtil method decode.

/**
     * Returns a new String constructed from the specified String by replacing
     * the URL escape sequences and UTF8 encoding with the characters they
     * represent.
     */
public static String decode(String s) {
    int n = s.length();
    if ((n == 0) || (s.indexOf('%') < 0))
        return s;
    StringBuilder sb = new StringBuilder(n);
    ByteBuffer bb = ByteBuffer.allocate(n);
    CharBuffer cb = CharBuffer.allocate(n);
    CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8").onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
    char c = s.charAt(0);
    for (int i = 0; i < n; ) {
        assert c == s.charAt(i);
        if (c != '%') {
            sb.append(c);
            if (++i >= n)
                break;
            c = s.charAt(i);
            continue;
        }
        bb.clear();
        int ui = i;
        for (; ; ) {
            assert (n - i >= 2);
            try {
                bb.put(unescape(s, i));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException();
            }
            i += 3;
            if (i >= n)
                break;
            c = s.charAt(i);
            if (c != '%')
                break;
        }
        bb.flip();
        cb.clear();
        dec.reset();
        CoderResult cr = dec.decode(bb, cb, true);
        if (cr.isError())
            throw new IllegalArgumentException("Error decoding percent encoded characters");
        cr = dec.flush(cb);
        if (cr.isError())
            throw new IllegalArgumentException("Error decoding percent encoded characters");
        sb.append(cb.flip().toString());
    }
    return sb.toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 37 with CharsetDecoder

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

the class ZipCoder method decoder.

private CharsetDecoder decoder() {
    CharsetDecoder dec = decTL.get();
    if (dec == null) {
        dec = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
        decTL.set(dec);
    }
    return dec;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder)

Example 38 with CharsetDecoder

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

the class ZipCoder method toString.

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int) (length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 39 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project ceylon-compiler by ceylon.

the class BaseFileManager method getDecoder.

public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null) ? Charset.forName(encodingName) : this.charset;
    CharsetDecoder decoder = cs.newDecoder();
    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;
    return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CodingErrorAction(java.nio.charset.CodingErrorAction) Charset(java.nio.charset.Charset)

Example 40 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project ceylon-compiler by ceylon.

the class BaseFileManager method decode.

public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
    String encodingName = getEncodingName();
    CharsetDecoder decoder;
    try {
        decoder = getDecoder(encodingName, ignoreEncodingErrors);
    } catch (IllegalCharsetNameException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    } catch (UnsupportedCharsetException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    }
    // slightly overestimate the buffer size to avoid reallocation.
    float factor = decoder.averageCharsPerByte() * 0.8f + decoder.maxCharsPerByte() * 0.2f;
    CharBuffer dest = CharBuffer.allocate(10 + (int) (inbuf.remaining() * factor));
    while (true) {
        CoderResult result = decoder.decode(inbuf, dest, true);
        dest.flip();
        if (result.isUnderflow()) {
            // make sure there is at least one extra character
            if (dest.limit() == dest.capacity()) {
                dest = CharBuffer.allocate(dest.capacity() + 1).put(dest);
                dest.flip();
            }
            return dest;
        } else if (result.isOverflow()) {
            // buffer too small; expand
            int newCapacity = 10 + dest.capacity() + (int) (inbuf.remaining() * decoder.maxCharsPerByte());
            dest = CharBuffer.allocate(newCapacity).put(dest);
        } else if (result.isMalformed() || result.isUnmappable()) {
            // report coding error (warn only pre 1.5)
            if (!getSource().allowEncodingErrors()) {
                log.error(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            } else {
                log.warning(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            }
            // skip past the coding error
            inbuf.position(inbuf.position() + result.length());
            // undo the flip() to prepare the output buffer
            // for more translation
            dest.position(dest.limit());
            dest.limit(dest.capacity());
            // backward compatible
            dest.put((char) 0xfffd);
        } else {
            throw new AssertionError(result);
        }
    }
// unreached
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) SimpleDiagnosticPosition(com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition) CoderResult(java.nio.charset.CoderResult)

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