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