Search in sources :

Example 71 with CharsetEncoder

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;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 72 with CharsetEncoder

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);
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 73 with CharsetEncoder

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);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 74 with CharsetEncoder

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;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 75 with CharsetEncoder

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);
    }
}
Also used : Attributes(java.util.jar.Attributes) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)75 ByteBuffer (java.nio.ByteBuffer)36 Charset (java.nio.charset.Charset)20 CoderResult (java.nio.charset.CoderResult)13 CharBuffer (java.nio.CharBuffer)12 CharacterCodingException (java.nio.charset.CharacterCodingException)10 Attributes (java.util.jar.Attributes)5 OutputStreamWriter (java.io.OutputStreamWriter)4 BufferedWriter (java.io.BufferedWriter)3 WriterException (com.google.zxing.WriterException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ArrayEncoder (sun.nio.cs.ArrayEncoder)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)1 ServiceException (com.zimbra.common.service.ServiceException)1 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailItem (com.zimbra.cs.mailbox.MailItem)1