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));
}
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());
}
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));
}
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());
}
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);
}
}
Aggregations