Search in sources :

Example 91 with CharsetEncoder

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

the class CharsetTest method test_allAvailableCharsets.

public void test_allAvailableCharsets() throws Exception {
    // Check that we can instantiate every Charset, CharsetDecoder, and CharsetEncoder.
    for (String charsetName : Charset.availableCharsets().keySet()) {
        if (charsetName.equals("UTF-32")) {
            // TODO: remove this hack when UTF-32 is fixed.
            continue;
        }
        Charset cs = Charset.forName(charsetName);
        assertNotNull(cs.newDecoder());
        if (cs.canEncode()) {
            CharsetEncoder enc = cs.newEncoder();
            assertNotNull(enc);
            assertNotNull(enc.replacement());
        }
    }
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 92 with CharsetEncoder

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

the class OldOutputStreamWriterTest method test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder.

public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder() throws IOException {
    OutputStreamWriter writer;
    Support_OutputStream out = new Support_OutputStream();
    Charset cs = Charset.forName("ascii");
    CharsetEncoder enc = cs.newEncoder();
    try {
        writer = new OutputStreamWriter(null, enc);
        fail("Test 1: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    try {
        writer = new OutputStreamWriter(out, (CharsetEncoder) null);
        fail("Test 2: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    writer = new OutputStreamWriter(out, cs);
    assertEquals("Test 3: CharacterEncoder not set correctly. ", cs, Charset.forName(writer.getEncoding()));
    writer.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 93 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project XobotOS by xamarin.

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 = Charsets.UTF_8.newEncoder();
    ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
    String version = manifest.mainAttributes.getValue(Attributes.Name.MANIFEST_VERSION);
    if (version != null) {
        writeEntry(out, Attributes.Name.MANIFEST_VERSION, version, encoder, buffer);
        Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            if (!name.equals(Attributes.Name.MANIFEST_VERSION)) {
                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, NAME_ATTRIBUTE, key, encoder, buffer);
        Attributes attrib = manifest.entries.get(key);
        Iterator<?> entries = attrib.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            writeEntry(out, name, attrib.getValue(name), encoder, buffer);
        }
        out.write(LINE_SEPARATOR);
    }
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 94 with CharsetEncoder

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

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 {
    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 95 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project intellij-community by JetBrains.

the class PyElementGeneratorImpl method createStringLiteralFromString.

@Override
public PyStringLiteralExpression createStringLiteralFromString(@Nullable PsiFile destination, @NotNull String unescaped, final boolean preferUTF8) {
    boolean useDouble = !unescaped.contains("\"");
    boolean useMulti = unescaped.matches(".*(\r|\n).*");
    String quotes;
    if (useMulti) {
        quotes = useDouble ? "\"\"\"" : "'''";
    } else {
        quotes = useDouble ? "\"" : "'";
    }
    StringBuilder buf = new StringBuilder(unescaped.length() * 2);
    buf.append(quotes);
    VirtualFile vfile = destination == null ? null : destination.getVirtualFile();
    Charset charset;
    if (vfile == null) {
        charset = (preferUTF8 ? CharsetToolkit.UTF8_CHARSET : Charset.forName("US-ASCII"));
    } else {
        charset = vfile.getCharset();
    }
    CharsetEncoder encoder = charset.newEncoder();
    Formatter formatter = new Formatter(buf);
    boolean unicode = false;
    for (int i = 0; i < unescaped.length(); i++) {
        int c = unescaped.codePointAt(i);
        if (c == '"' && useDouble) {
            buf.append("\\\"");
        } else if (c == '\'' && !useDouble) {
            buf.append("\\'");
        } else if ((c == '\r' || c == '\n') && !useMulti) {
            if (c == '\r') {
                buf.append("\\r");
            } else if (c == '\n')
                buf.append("\\n");
        } else if (!encoder.canEncode(new String(Character.toChars(c)))) {
            if (c <= 0xff) {
                formatter.format("\\x%02x", c);
            } else if (c < 0xffff) {
                unicode = true;
                formatter.format("\\u%04x", c);
            } else {
                unicode = true;
                formatter.format("\\U%08x", c);
            }
        } else {
            buf.appendCodePoint(c);
        }
    }
    buf.append(quotes);
    if (unicode)
        buf.insert(0, "u");
    return createStringLiteralAlreadyEscaped(buf.toString());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Formatter(java.util.Formatter) Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)180 ByteBuffer (java.nio.ByteBuffer)88 Charset (java.nio.charset.Charset)51 CharBuffer (java.nio.CharBuffer)42 CoderResult (java.nio.charset.CoderResult)32 CharacterCodingException (java.nio.charset.CharacterCodingException)31 OutputStreamWriter (java.io.OutputStreamWriter)22 IOException (java.io.IOException)16 BufferedWriter (java.io.BufferedWriter)14 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