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 robovm by robovm.
the class CharsetEncoderTest method testCharsetEncoderCharsetfloatfloatbyteArray.
/*
* Class under test for constructor CharsetEncoder(Charset, float, float,
* byte[])
*/
public void testCharsetEncoderCharsetfloatfloatbyteArray() {
byte[] ba = getLegalByteArray();
// normal case
CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, ba);
assertSame(ec.charset(), cs);
assertEquals(1.0, ec.averageBytesPerChar(), 0.0);
assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
assertSame(ba, ec.replacement());
// NullPointerException: null charset
try {
ec = new MockCharsetEncoder(null, 1, MAX_BYTES, ba);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
// Illegal Argument: null byte array
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, null);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: empty byte array
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[0]);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: byte array is longer than max length
try {
ec = new MockCharsetEncoder(cs, 1, MAX_BYTES, new byte[] { 1, 2, MAX_BYTES, 4 });
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: zero length
try {
ec = new MockCharsetEncoder(cs, 0, MAX_BYTES, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, 0, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: negative length
try {
ec = new MockCharsetEncoder(cs, -1, MAX_BYTES, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, -1, ba);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testCharsetEncoderCharsetfloatfloat.
/*
* Class under test for constructor CharsetEncoder(Charset, float, float)
*/
public void testCharsetEncoderCharsetfloatfloat() {
// default value
encoder = new MockCharsetEncoder(cs, (float) AVER_BYTES, MAX_BYTES);
assertSame(encoder.charset(), cs);
assertTrue(encoder.averageBytesPerChar() == AVER_BYTES);
assertTrue(encoder.maxBytesPerChar() == MAX_BYTES);
assertEquals(CodingErrorAction.REPORT, encoder.malformedInputAction());
assertEquals(CodingErrorAction.REPORT, encoder.unmappableCharacterAction());
assertEquals(new String(encoder.replacement()), new String(defaultReplacement));
assertSame(encoder, encoder.onMalformedInput(CodingErrorAction.IGNORE));
assertSame(encoder, encoder.onUnmappableCharacter(CodingErrorAction.IGNORE));
// normal case
CharsetEncoder ec = new MockCharsetEncoder(cs, 1, MAX_BYTES);
assertSame(ec.charset(), cs);
assertEquals(1.0, ec.averageBytesPerChar(), 0);
assertTrue(ec.maxBytesPerChar() == MAX_BYTES);
// NullPointerException: null charset
try {
ec = new MockCharsetEncoder(null, 1, MAX_BYTES);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
ec = new MockCharsetEncoder(new MockCharset("mock", new String[0]), 1, MAX_BYTES);
// Illegal Argument: zero length
try {
ec = new MockCharsetEncoder(cs, 0, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, 0);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
// Illegal Argument: negative length
try {
ec = new MockCharsetEncoder(cs, -1, MAX_BYTES);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
try {
ec = new MockCharsetEncoder(cs, 1, -1);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class CharsetEncoderTest method testEncodeCharBuffer.
/*
* Class under test for ByteBuffer encode(CharBuffer)
*/
public void testEncodeCharBuffer() throws CharacterCodingException {
// Null pointer
try {
encoder.encode(null);
fail("should throw null pointer exception");
} catch (NullPointerException e) {
}
// empty input buffer
ByteBuffer out = encoder.encode(CharBuffer.wrap(""));
assertEquals(out.position(), 0);
assertByteArray(out, new byte[0]);
// assertByteArray(out, surrogate);
// normal case
out = encoder.encode(CharBuffer.wrap(unistr));
assertEquals(out.position(), 0);
assertByteArray(out, addSurrogate(unibytes));
// Regression test for harmony-3378
Charset cs = Charset.forName("UTF-8");
CharsetEncoder encoder = cs.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder = encoder.replaceWith(new byte[] { (byte) 0xef, (byte) 0xbf, (byte) 0xbd });
CharBuffer in = CharBuffer.wrap("�");
out = encoder.encode(in);
assertNotNull(out);
}
Aggregations