use of java.nio.charset.CharsetEncoder in project drill by apache.
the class Text method encode.
/**
* Converts the provided String to bytes using the UTF-8 encoding. If <code>replace</code> is true, then malformed
* input is replaced with the substitution character, which is U+FFFD. Otherwise the method throws a
* MalformedInputException.
*
* @return ByteBuffer: bytes stores at ByteBuffer.array() and length is ByteBuffer.limit()
*/
public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException {
CharsetEncoder encoder = ENCODER_FACTORY.get();
if (replace) {
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray()));
if (replace) {
encoder.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
}
return bytes;
}
use of java.nio.charset.CharsetEncoder in project karaf by apache.
the class ShellTable method supportsUnicode.
private boolean supportsUnicode(PrintStream out) {
if (forceAscii) {
return false;
}
String encoding = getEncoding(out);
if (encoding == null) {
return false;
}
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
return encoder.canEncode(separator) && encoder.canEncode(SEP_HORIZONTAL) && encoder.canEncode(SEP_CROSS);
}
use of java.nio.charset.CharsetEncoder in project logging-log4j2 by apache.
the class StringBuilderEncoder method encode.
@Override
public void encode(final StringBuilder source, final ByteBufferDestination destination) {
final ByteBuffer temp = getByteBuffer();
temp.clear();
temp.limit(Math.min(temp.capacity(), destination.getByteBuffer().capacity()));
final CharsetEncoder charsetEncoder = getCharsetEncoder();
final int estimatedBytes = estimateBytes(source.length(), charsetEncoder.maxBytesPerChar());
if (temp.remaining() < estimatedBytes) {
encodeSynchronized(getCharsetEncoder(), getCharBuffer(), source, destination);
} else {
encodeWithThreadLocals(charsetEncoder, getCharBuffer(), temp, source, destination);
}
}
use of java.nio.charset.CharsetEncoder in project logging-log4j2 by apache.
the class StringBuilderEncoder method getCharsetEncoder.
private CharsetEncoder getCharsetEncoder() {
CharsetEncoder result = charsetEncoderThreadLocal.get();
if (result == null) {
result = charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
charsetEncoderThreadLocal.set(result);
}
return result;
}
use of java.nio.charset.CharsetEncoder in project android_frameworks_base by crdroidandroid.
the class StrictJarManifest method write.
/**
* Writes out the attribute information of the specified manifest to the
* specified {@code OutputStream}
*
* @param manifest
* the manifest to write out.
* @param out
* The {@code OutputStream} to write to.
* @throws IOException
* If an error occurs writing the {@code StrictJarManifest}.
*/
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
String version = manifest.mainAttributes.getValue(versionName);
if (version == null) {
versionName = Attributes.Name.SIGNATURE_VERSION;
version = manifest.mainAttributes.getValue(versionName);
}
if (version != null) {
writeEntry(out, versionName, version, encoder, buffer);
Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
if (!name.equals(versionName)) {
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
}
}
}
out.write(LINE_SEPARATOR);
Iterator<String> i = manifest.getEntries().keySet().iterator();
while (i.hasNext()) {
String key = i.next();
writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
Attributes attributes = manifest.entries.get(key);
Iterator<?> entries = attributes.keySet().iterator();
while (entries.hasNext()) {
Attributes.Name name = (Attributes.Name) entries.next();
writeEntry(out, name, attributes.getValue(name), encoder, buffer);
}
out.write(LINE_SEPARATOR);
}
}
Aggregations