Search in sources :

Example 51 with CharsetEncoder

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) {
    }
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 52 with CharsetEncoder

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) {
    }
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 53 with CharsetEncoder

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);
}
Also used : CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 54 with CharsetEncoder

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

the class CharsetTest method test_allAvailableCharsets.

public void test_allAvailableCharsets() throws Exception {
    // Check that we can instantiate every Charset, CharsetDecoder, and CharsetEncoder.
    for (String charsetName : Charset.availableCharsets().keySet()) {
        if (charsetName.equals("UTF-32")) {
            // TODO: remove this hack when UTF-32 is fixed.
            continue;
        }
        Charset cs = Charset.forName(charsetName);
        assertNotNull(cs.newDecoder());
        if (cs.canEncode()) {
            CharsetEncoder enc = cs.newEncoder();
            assertNotNull(enc);
            assertNotNull(enc.replacement());
        }
    }
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 55 with CharsetEncoder

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

the class OldOutputStreamWriterTest method test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder.

public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder() throws IOException {
    OutputStreamWriter writer;
    Support_OutputStream out = new Support_OutputStream();
    Charset cs = Charset.forName("ascii");
    CharsetEncoder enc = cs.newEncoder();
    try {
        writer = new OutputStreamWriter(null, enc);
        fail("Test 1: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    try {
        writer = new OutputStreamWriter(out, (CharsetEncoder) null);
        fail("Test 2: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected
    }
    writer = new OutputStreamWriter(out, cs);
    assertEquals("Test 3: CharacterEncoder not set correctly. ", cs, Charset.forName(writer.getEncoding()));
    writer.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) 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