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;
}
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);
}
}
}
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;
}
}
}
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;
}
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();
}
Aggregations