use of java.nio.charset.CharsetDecoder in project android_frameworks_base by crdroidandroid.
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 dropbox-sdk-java by dropbox.
the class StringUtil method utf8ToString.
public static String utf8ToString(byte[] utf8Data, int offset, int length) throws CharacterCodingException {
// NOTE: Using the String(..., UTF8) constructor would be wrong. That method will
// ignore UTF-8 errors in the input.
CharsetDecoder decoder = UTF8.newDecoder();
CharBuffer result = decoder.decode(ByteBuffer.wrap(utf8Data, offset, length));
return result.toString();
}
use of java.nio.charset.CharsetDecoder in project tomcat by apache.
the class TestUtf8 method testJvmDecoder.
@Test
public void testJvmDecoder() {
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
int testCount = 0;
try {
for (Utf8TestCase testCase : TEST_CASES) {
doTest(decoder, testCase, testCase.flagsJvm);
testCount++;
}
} finally {
System.err.println("Workarounds added to " + workAroundCount + " tests to account for known JVM bugs");
if (testCount < TEST_CASES.size()) {
System.err.println("Executed " + testCount + " of " + TEST_CASES.size() + " UTF-8 tests before " + "encountering a failure");
}
}
}
use of java.nio.charset.CharsetDecoder in project gitblit by gitblit.
the class StringUtils method decodeString.
/**
* Decodes a string by trying several charsets until one does not throw a
* coding exception. Last resort is to interpret as UTF-8 with illegal
* character substitution.
*
* @param content
* @param charsets optional
* @return a string
*/
public static String decodeString(byte[] content, String... charsets) {
Set<String> sets = new LinkedHashSet<String>();
if (!ArrayUtils.isEmpty(charsets)) {
sets.addAll(Arrays.asList(charsets));
}
String value = null;
sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
for (String charset : sets) {
try {
Charset cs = Charset.forName(charset);
CharsetDecoder decoder = cs.newDecoder();
CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
value = buffer.toString();
break;
} catch (CharacterCodingException e) {
// ignore and advance to the next charset
} catch (IllegalCharsetNameException e) {
// ignore illegal charset names
} catch (UnsupportedCharsetException e) {
// ignore unsupported charsets
}
}
if (value != null && value.startsWith("")) {
// strip UTF-8 BOM
return value.substring(1);
}
return value;
}
use of java.nio.charset.CharsetDecoder in project buck by facebook.
the class UnixArchive method loadEntries.
@SuppressWarnings("PMD.PrematureDeclaration")
private ImmutableList<UnixArchiveEntry> loadEntries() {
int offset = EXPECTED_GLOBAL_HEADER.length;
int headerOffset = offset;
ImmutableList.Builder<UnixArchiveEntry> builder = ImmutableList.builder();
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
while (true) {
buffer.position(offset);
byte[] markerBytes = new byte[ENTRY_MARKER.length];
buffer.get(markerBytes, 0, markerBytes.length);
if (!Arrays.equals(markerBytes, ENTRY_MARKER)) {
throw new HumanReadableException("Unknown entry marker");
}
int filenameLength = getIntFromStringAtRange(LENGTH_OF_FILENAME_SIZE, decoder);
long fileModification = getLongFromStringAtRange(MODIFICATION_TIME_SIZE, decoder);
int ownerId = getIntFromStringAtRange(OWNER_ID_SIZE, decoder);
int groupId = getIntFromStringAtRange(GROUP_ID_SIZE, decoder);
int fileMode = getIntFromStringAtRange(FILE_MODE_SIZE, decoder);
int fileAndFilenameSize = getIntFromStringAtRange(FILE_AND_FILENAME_SIZE, decoder);
byte[] magic = new byte[END_OF_HEADER_MAGIC_SIZE];
buffer.get(magic, 0, magic.length);
if (!Arrays.equals(magic, END_OF_HEADER_MAGIC)) {
throw new HumanReadableException("Unknown file magic");
}
long fileSizeWithoutFilename = fileAndFilenameSize - filenameLength;
offset = buffer.position();
String filename;
try {
filename = nulTerminatedCharsetDecoder.decodeString(buffer);
} catch (CharacterCodingException e) {
throw new HumanReadableException(e, "Unable to read filename from buffer starting at %d", offset);
}
offset += filenameLength;
builder.add(UnixArchiveEntry.of(filenameLength, fileModification, ownerId, groupId, fileMode, fileSizeWithoutFilename, filename, headerOffset, offset - headerOffset, offset));
offset += fileSizeWithoutFilename;
if (offset == buffer.capacity()) {
break;
}
}
return builder.build();
}
Aggregations