Search in sources :

Example 96 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project android_frameworks_base by AOSPA.

the class StrictJarManifest method write.

/**
     * Writes out the attribute information of the specified manifest to the
     * specified {@code OutputStream}
     *
     * @param manifest
     *            the manifest to write out.
     * @param out
     *            The {@code OutputStream} to write to.
     * @throws IOException
     *             If an error occurs writing the {@code StrictJarManifest}.
     */
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
    CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
    ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
    Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
    String version = manifest.mainAttributes.getValue(versionName);
    if (version == null) {
        versionName = Attributes.Name.SIGNATURE_VERSION;
        version = manifest.mainAttributes.getValue(versionName);
    }
    if (version != null) {
        writeEntry(out, versionName, version, encoder, buffer);
        Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            if (!name.equals(versionName)) {
                writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
            }
        }
    }
    out.write(LINE_SEPARATOR);
    Iterator<String> i = manifest.getEntries().keySet().iterator();
    while (i.hasNext()) {
        String key = i.next();
        writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
        Attributes attributes = manifest.entries.get(key);
        Iterator<?> entries = attributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            writeEntry(out, name, attributes.getValue(name), encoder, buffer);
        }
        out.write(LINE_SEPARATOR);
    }
}
Also used : Attributes(java.util.jar.Attributes) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 97 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 98 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 99 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 100 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)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)180 ByteBuffer (java.nio.ByteBuffer)88 Charset (java.nio.charset.Charset)51 CharBuffer (java.nio.CharBuffer)42 CoderResult (java.nio.charset.CoderResult)32 CharacterCodingException (java.nio.charset.CharacterCodingException)31 OutputStreamWriter (java.io.OutputStreamWriter)22 IOException (java.io.IOException)16 BufferedWriter (java.io.BufferedWriter)14 OutputStream (java.io.OutputStream)10 FileOutputStream (java.io.FileOutputStream)7 InputStream (java.io.InputStream)7 IStatus (org.eclipse.core.runtime.IStatus)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 CoreException (org.eclipse.core.runtime.CoreException)6 Status (org.eclipse.core.runtime.Status)6 ArrayEncoder (sun.nio.cs.ArrayEncoder)6 Writer (java.io.Writer)5