Search in sources :

Example 6 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project weex-example by KalicyZhou.

the class PDF417HighLevelEncoder method determineConsecutiveBinaryCount.

/**
   * Determines the number of consecutive characters that are encodable using binary compaction.
   *
   * @param msg      the message
   * @param startpos the start position within the message
   * @param encoding the charset used to convert the message to a byte array
   * @return the requested character count
   */
private static int determineConsecutiveBinaryCount(String msg, int startpos, Charset encoding) throws WriterException {
    final CharsetEncoder encoder = encoding.newEncoder();
    int len = msg.length();
    int idx = startpos;
    while (idx < len) {
        char ch = msg.charAt(idx);
        int numericCount = 0;
        while (numericCount < 13 && isDigit(ch)) {
            numericCount++;
            //textCount++;
            int i = idx + numericCount;
            if (i >= len) {
                break;
            }
            ch = msg.charAt(i);
        }
        if (numericCount >= 13) {
            return idx - startpos;
        }
        ch = msg.charAt(idx);
        if (!encoder.canEncode(ch)) {
            throw new WriterException("Non-encodable character detected: " + ch + " (Unicode: " + (int) ch + ')');
        }
        idx++;
    }
    return idx - startpos;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) WriterException(com.google.zxing.WriterException)

Example 7 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project robovm by robovm.

the class ASCIICharsetEncoderTest method testInternalState_Reset.

//reset could be called at any time
public void testInternalState_Reset() {
    CharsetEncoder newEncoder = cs.newEncoder();
    //Init - > reset
    newEncoder.reset();
    //reset - > reset
    newEncoder.reset();
    //encoding - >reset
    {
        CharBuffer in = CharBuffer.wrap("A");
        ByteBuffer out = ByteBuffer.allocate(0x10);
        newEncoder.encode(in, out, false);
        newEncoder.reset();
    }
    //encoding end -> reset
    {
        CharBuffer in = CharBuffer.wrap("A");
        ByteBuffer out = ByteBuffer.allocate(0x10);
        newEncoder.encode(in, out, true);
        newEncoder.reset();
    }
    //flused -> reset
    {
        CharBuffer in = CharBuffer.wrap("A");
        ByteBuffer out = ByteBuffer.allocate(0x10);
        newEncoder.encode(in, out, true);
        newEncoder.flush(out);
        newEncoder.reset();
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 8 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project robovm by robovm.

the class Manifest 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 Manifest}.
     */
static void write(Manifest 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 : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 9 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project platform_frameworks_base by android.

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)

Example 10 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project gocd by gocd.

the class NokogiriHelpers method convertEncoding.

public static byte[] convertEncoding(Charset output_charset, String input_string) throws CharacterCodingException {
    CharsetEncoder encoder = output_charset.newEncoder();
    CharBuffer charBuffer = CharBuffer.wrap(input_string);
    ByteBuffer byteBuffer = encoder.encode(charBuffer);
    byte[] buffer = new byte[byteBuffer.remaining()];
    byteBuffer.get(buffer);
    return buffer;
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)177 ByteBuffer (java.nio.ByteBuffer)87 Charset (java.nio.charset.Charset)50 CharBuffer (java.nio.CharBuffer)42 CharacterCodingException (java.nio.charset.CharacterCodingException)31 CoderResult (java.nio.charset.CoderResult)31 OutputStreamWriter (java.io.OutputStreamWriter)21 IOException (java.io.IOException)15 BufferedWriter (java.io.BufferedWriter)13 OutputStream (java.io.OutputStream)10 FileOutputStream (java.io.FileOutputStream)7 InputStream (java.io.InputStream)7 IStatus (org.eclipse.core.runtime.IStatus)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 CoreException (org.eclipse.core.runtime.CoreException)6 Status (org.eclipse.core.runtime.Status)6 ArrayEncoder (sun.nio.cs.ArrayEncoder)6 Writer (java.io.Writer)5