Search in sources :

Example 46 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project j2objc by google.

the class CharsetEncoderTest method testSurrogatePairAllAtOnce.

public void testSurrogatePairAllAtOnce() throws Exception {
    // okay: surrogate pair seen all at once is decoded to U+20b9f.
    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);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, 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));
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 47 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project j2objc by google.

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);
    } 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 48 with CharsetEncoder

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

the class CharsetEncoderTest method test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB.

/*
     * Test reserve bytes encode(CharBuffer,ByteBuffer,boolean)
     */
public void test_EncodeLjava_nio_CharBufferLjava_nio_ByteBufferB() throws Exception {
    Charset utf8 = Charset.forName("utf-8");
    CharsetEncoder encoder = utf8.newEncoder();
    CharBuffer char1 = CharBuffer.wrap("�");
    CharBuffer char2 = CharBuffer.wrap("�");
    ByteBuffer bytes = ByteBuffer.allocate(4);
    encoder.reset();
    // If we supply just the high surrogate...
    CoderResult result = encoder.encode(char1, bytes, false);
    // ...we're not done...
    assertTrue(result.isUnderflow());
    assertEquals(4, bytes.remaining());
    // ...but if we then supply the low surrogate...
    result = encoder.encode(char2, bytes, true);
    assertTrue(result.isUnderflow());
    // ...we're done. Note that the RI loses its state in
    // between the two characters, so it can't do this.
    assertEquals(0, bytes.remaining());
    // Did we get the UTF-8 for U+10000?
    assertEquals(4, bytes.limit());
    assertEquals((byte) 0xf0, bytes.get(0));
    assertEquals((byte) 0x90, bytes.get(1));
    assertEquals((byte) 0x80, bytes.get(2));
    assertEquals((byte) 0x80, bytes.get(3));
    // See what we got in the output buffer by decoding and checking that we
    // get back the same surrogate pair.
    bytes.flip();
    CharBuffer chars = utf8.newDecoder().decode(bytes);
    assertEquals(0, bytes.remaining());
    assertEquals(2, chars.limit());
    assertEquals(0xd800, chars.get(0));
    assertEquals(0xdc00, chars.get(1));
}
Also used : CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 49 with CharsetEncoder

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

the class CharsetEncoderTest method test_ConstructorLjava_nio_charset_CharsetNull.

/**
     * @tests java.nio.charset.CharsetEncoder.CharsetEncoder(
     *        java.nio.charset.Charset, float, float)
     */
public void test_ConstructorLjava_nio_charset_CharsetNull() {
    // Regression for HARMONY-491
    CharsetEncoder ech = new MockCharsetEncoderForHarmony491(null, 1, 1);
    assertNull(ech.charset());
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 50 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project nanohttpd by NanoHttpd.

the class Response method newFixedLengthResponse.

/**
     * Create a text response with known length.
     */
public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) {
    ContentType contentType = new ContentType(mimeType);
    if (txt == null) {
        return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0);
    } else {
        byte[] bytes;
        try {
            CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder();
            if (!newEncoder.canEncode(txt)) {
                contentType = contentType.tryUTF8();
            }
            bytes = txt.getBytes(contentType.getEncoding());
        } catch (UnsupportedEncodingException e) {
            NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e);
            bytes = new byte[0];
        }
        return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length);
    }
}
Also used : ContentType(org.nanohttpd.protocols.http.content.ContentType) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CharsetEncoder(java.nio.charset.CharsetEncoder)

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