Search in sources :

Example 66 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project fastjson by alibaba.

the class ThreadLocalCache method getUTF8Decoder.

public static CharsetDecoder getUTF8Decoder() {
    CharsetDecoder decoder = decoderLocal.get();
    if (decoder == null) {
        decoder = new UTF8Decoder();
        decoderLocal.set(decoder);
    }
    return decoder;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder)

Example 67 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project head by mifos.

the class SqlExecutor method readFile.

/**
     * Closes the stream when done.
     *
     * @return individual statements
     * */
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.AssignmentInOperand", "PMD.AppendCharacterWithChar", "PMD.AvoidThrowingRawExceptionTypes", "PMD.DoNotThrowExceptionInFinally" })
public static String[] readFile(InputStream stream) {
    // http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/SQLExec.java
    try {
        ArrayList<String> statements = new ArrayList<String>();
        Charset utf8 = Charset.forName("UTF-8");
        CharsetDecoder decoder = utf8.newDecoder();
        BufferedReader in = new BufferedReader(new InputStreamReader(stream, decoder));
        StringBuffer sql = new StringBuffer();
        String line;
        boolean insideProcedure = false;
        while ((line = in.readLine()) != null) {
            if (line.startsWith("//") || line.startsWith("--") || line.startsWith("DELIMITER")) {
                continue;
            } else if (line.startsWith("BEGIN") || line.startsWith("END */")) {
                insideProcedure = insideProcedure ^ true;
            }
            line = line.trim();
            if ("".equals(line)) {
                continue;
            }
            sql.append("\n");
            sql.append(line);
            // so we cannot just remove it, instead we must end it
            if (line.indexOf("--") >= 0) {
                sql.append("\n");
            }
            if (sql.length() > 0 && sql.charAt(sql.length() - 1) == ';' && !insideProcedure) {
                statements.add(sql.substring(0, sql.length() - 1));
                sql.setLength(0);
            }
        }
        // Catch any statements not followed by ;
        if (sql.length() > 0) {
            statements.add(sql.toString());
        }
        return statements.toArray(new String[statements.size()]);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 68 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project dbeaver by serge-rider.

the class HexEditControl method composeByteToCharMap.

/**
     * compose byte-to-char map
     */
private void composeByteToCharMap() {
    if (charset == null || previewText == null)
        return;
    CharsetDecoder decoder = Charset.forName(charset).newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith(".");
    ByteBuffer bb = ByteBuffer.allocate(1);
    CharBuffer cb = CharBuffer.allocate(1);
    for (int i = 0; i < 256; ++i) {
        if (i < 0x20 || i == 0x7f) {
            byteToChar[i] = (char) (160 + i);
        } else {
            bb.clear();
            bb.put((byte) i);
            bb.rewind();
            cb.clear();
            decoder.reset();
            decoder.decode(bb, cb, true);
            decoder.flush(cb);
            cb.rewind();
            char decoded = cb.get();
            // neither font metrics nor graphic context work for charset 8859-1 chars between 128 and
            // 159
            // It works too slow. Dumn with it.
            byteToChar[i] = decoded;
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 69 with CharsetDecoder

use of java.nio.charset.CharsetDecoder in project helios by spotify.

the class LoggingLogStreamFollower method createStreamDecoders.

/**
   * Creates charset decoders for all available log message streams.
   *
   * @return a map containing a decoder for every log message stream type
   */
private Map<LogMessage.Stream, Decoder> createStreamDecoders() {
    final Map<LogMessage.Stream, Decoder> streamDecoders = new EnumMap<>(LogMessage.Stream.class);
    for (final LogMessage.Stream stream : LogMessage.Stream.values()) {
        final CharsetDecoder charsetDecoder = Charsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        final CharBuffer charBuffer = CharBuffer.allocate(1024);
        streamDecoders.put(stream, new Decoder(charsetDecoder, byteBuffer, charBuffer));
    }
    return streamDecoders;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) LogMessage(com.spotify.docker.client.LogMessage) CharBuffer(java.nio.CharBuffer) CharsetDecoder(java.nio.charset.CharsetDecoder) EnumMap(java.util.EnumMap) ByteBuffer(java.nio.ByteBuffer)

Example 70 with CharsetDecoder

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

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)

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