Search in sources :

Example 66 with CharsetEncoder

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

the class CharsetEncoderTest method testFlushWithoutEndOfInput.

public void testFlushWithoutEndOfInput() throws Exception {
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, bb.position());
    try {
        cr = e.flush(bb);
        fail();
    } catch (IllegalStateException expected) {
    // You must call encode with endOfInput true before you can flush.
    }
    // We had a bug where we wouldn't reset inEnd before calling encode in implFlush.
    // That would result in flush outputting garbage.
    cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, true);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(8, bb.position());
    cr = e.flush(bb);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(8, bb.position());
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 67 with CharsetEncoder

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

the class CharsetEncoderTest method testMalformedSurrogatePair.

public void testMalformedSurrogatePair() throws Exception {
    // malformed: low surrogate first is detected as an error.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
    assertTrue(cr.toString(), cr.isMalformed());
    assertEquals(1, cr.length());
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 68 with CharsetEncoder

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

the class CharsetEncoderTest method testCharsetEncoderSplitSurrogates.

private void testCharsetEncoderSplitSurrogates(CodingErrorAction cea) throws Exception {
    // Writing the two halves of the surrogate pair in separate writes should work just fine.
    // This is true of Android and ICU, but not of the RI.
    // On the RI, writing the two halves of the surrogate pair in separate writes
    // is an error because the CharsetEncoder doesn't remember it's half-way through a
    // surrogate pair across the two calls!
    // IGNORE just ignores both characters, REPORT complains that the second is
    // invalid (because it doesn't remember seeing the first), and REPLACE inserts a
    // replacement character U+fffd when it sees the second character (because it too
    // doesn't remember seeing the first).
    // Android just does the right thing.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    e.onMalformedInput(cea);
    e.onUnmappableCharacter(cea);
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(0, bb.position());
    cr = e.encode(CharBuffer.wrap(new char[] { '�' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    int expectedPosition = 4;
    assertEquals(expectedPosition, bb.position());
    System.err.println(Arrays.toString(Arrays.copyOfRange(bb.array(), 0, bb.position())));
    assertEquals((byte) 0x00, bb.get(0));
    assertEquals((byte) 0x02, bb.get(1));
    assertEquals((byte) 0x0b, bb.get(2));
    assertEquals((byte) 0x9f, bb.get(3));
    cr = e.encode(CharBuffer.wrap(new char[] {}), bb, true);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(expectedPosition, bb.position());
    cr = e.flush(bb);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(expectedPosition, bb.position());
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 69 with CharsetEncoder

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

the class Charset_TestGenerator method genEncoded.

static void genEncoded(Charset charset, CharBuffer cb) {
    System.out.println(charset.name());
    Dumper out = new Dumper1();
    CharsetEncoder encoder = charset.newEncoder();
    encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    try {
        ByteBuffer bb = encoder.encode(cb);
        //            bb.rewind();
        while (bb.hasRemaining()) {
            out.consume(bb.get());
        }
    } catch (CharacterCodingException e) {
        System.out.println(e);
    //                e.printStackTrace();
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 70 with CharsetEncoder

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

the class OldCharsetEncoderDecoderBufferTest method testEncoderOutputBuffer.

/* Checks for a buffer corruption that happens in ICU
     * (CharsetEncoderICU) when an encode operation
     * is done first with an out-buffer with hasArray()==true, and next with an out-buffer with
     * hasArray()==false. In that situation ICU may overwrite the first out-buffer.
     */
public void testEncoderOutputBuffer() {
    CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    byte[] buffer = new byte[10];
    ByteBuffer out = ByteBuffer.wrap(buffer);
    assertTrue(out.hasArray());
    encoder.encode(CharBuffer.wrap("ab"), out, false);
    assertEquals('a', buffer[0]);
    assertEquals('b', buffer[1]);
    assertEquals(0, buffer[2]);
    out = ByteBuffer.allocateDirect(10);
    // It's no longer possible to get a byte buffer without a backing byte[] on Android.
    // This test is useless on Android, unless that changes again. (You can't even
    // subclass ByteBuffer because -- although it's non-final -- both the RI and Android
    // have [different] package-private abstract methods you'd need to implement but can't.)
    //assertFalse(out.hasArray());
    encoder.encode(CharBuffer.wrap("x"), out, true);
    // check whether the second decode corrupted the first buffer
    assertEquals('a', buffer[0]);
    assertEquals('b', buffer[1]);
    assertEquals(0, buffer[2]);
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)84 ByteBuffer (java.nio.ByteBuffer)43 Charset (java.nio.charset.Charset)27 CharacterCodingException (java.nio.charset.CharacterCodingException)16 CharBuffer (java.nio.CharBuffer)15 CoderResult (java.nio.charset.CoderResult)13 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 IStatus (org.eclipse.core.runtime.IStatus)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)5 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)5 Attributes (java.util.jar.Attributes)5 CoreException (org.eclipse.core.runtime.CoreException)5 Status (org.eclipse.core.runtime.Status)5 OutputStreamWriter (java.io.OutputStreamWriter)4 SequenceInputStream (java.io.SequenceInputStream)4 BufferedWriter (java.io.BufferedWriter)3 OutputStream (java.io.OutputStream)3