Search in sources :

Example 16 with CharsetEncoder

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

the class OldCharsetEncoderDecoderBufferTest method testEncoderInputBuffer.

/* Checks for a buffer corruption that happens in ICU
     * (CharsetEncoderICU) when an encode operation
     * is done first with an in-buffer with hasArray()==true, and next with an in-buffer with
     * hasArray()==false. In that situation ICU may overwrite the array of the first in-buffer.
     */
public void testEncoderInputBuffer() {
    CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    ByteBuffer out = ByteBuffer.wrap(new byte[10]);
    char[] inArray = { 'a', 'b' };
    CharBuffer inWithArray = CharBuffer.wrap(inArray);
    assertTrue(inWithArray.hasArray());
    encoder.encode(inWithArray, out, false);
    assertEquals('a', inArray[0]);
    assertEquals('b', inArray[1]);
    CharBuffer inWithoutArray = CharBuffer.wrap("x");
    assertFalse(inWithoutArray.hasArray());
    encoder.encode(inWithoutArray, out, true);
    // check whether the second decode corrupted the first buffer
    assertEquals('a', inArray[0]);
    assertEquals('b', inArray[1]);
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 17 with CharsetEncoder

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

the class EncodedText method stringToUnicodeBytes.

public static byte[] stringToUnicodeBytes(String s, String characterSet) {
    Charset charset = Charset.forName(characterSet);
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer byteBuffer;
    try {
        byteBuffer = encoder.encode(CharBuffer.wrap(s));
        return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit());
    } catch (CharacterCodingException e) {
        return stringToUTF8Bytes(s);
    }
}
Also used : Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 18 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.

the class ZipCoder method encoder.

private CharsetEncoder encoder() {
    CharsetEncoder enc = encTL.get();
    if (enc == null) {
        enc = cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
        encTL.set(enc);
    }
    return enc;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 19 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.

the class ZipCoder method getBytes.

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int) (ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (// defensive copy?
    bb.position() == ba.length)
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 20 with CharsetEncoder

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

the class FlexDefinitionProcessor method parseCPoolAndRename.

private static void parseCPoolAndRename(String from, ByteBuffer buffer) {
    buffer.position(buffer.position() + 4);
    int n = AbcUtil.readU32(buffer);
    while (n-- > 1) {
        AbcUtil.readU32(buffer);
    }
    n = AbcUtil.readU32(buffer);
    while (n-- > 1) {
        AbcUtil.readU32(buffer);
    }
    n = AbcUtil.readU32(buffer);
    if (n != 0) {
        buffer.position(buffer.position() + ((n - 1) * 8));
    }
    n = AbcUtil.readU32(buffer);
    final CharsetEncoder charsetEncoder = StandardCharsets.UTF_8.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    o: while (n-- > 1) {
        int l = AbcUtil.readU32(buffer);
        buffer.limit(buffer.position() + l);
        buffer.mark();
        final CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer);
        buffer.limit(buffer.capacity());
        int index = 0;
        do {
            index = CharArrayUtil.indexOf(charBuffer, from, index);
            if (index == -1 || (index > 0 && !isSpecialSymbol(charBuffer.get(index - 1)))) {
                continue o;
            }
            charBuffer.put(index, OVERLOADED_AND_BACKED_CLASS_MARK);
            index += from.length();
        } while (index < charBuffer.length());
        buffer.reset();
        charsetEncoder.encode(charBuffer, buffer, true);
        charsetEncoder.reset();
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder)

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