Search in sources :

Example 11 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project sessdb by ppdai.

the class Slices method encodeString.

public static ByteBuffer encodeString(CharBuffer src, Charset charset) {
    final CharsetEncoder encoder = getEncoder(charset);
    final ByteBuffer dst = ByteBuffer.allocate((int) ((double) src.remaining() * encoder.maxBytesPerChar()));
    try {
        CoderResult cr = encoder.encode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
    dst.flip();
    return dst;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 12 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project sessdb by ppdai.

the class Slices method getEncoder.

/**
     * Returns a cached thread-local {@link CharsetEncoder} for the specified
     * <tt>charset</tt>.
     */
private static CharsetEncoder getEncoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }
    Map<Charset, CharsetEncoder> map = encoders.get();
    CharsetEncoder e = map.get(charset);
    if (e != null) {
        e.reset();
        e.onMalformedInput(CodingErrorAction.REPLACE);
        e.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return e;
    }
    e = charset.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, e);
    return e;
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 13 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project hadoop by apache.

the class Text method encode.

/**
   * Converts the provided String to bytes using the
   * UTF-8 encoding. If <code>replace</code> is true, then
   * malformed input is replaced with the
   * substitution character, which is U+FFFD. Otherwise the
   * method throws a MalformedInputException.
   * @return ByteBuffer: bytes stores at ByteBuffer.array() 
   *                     and length is ByteBuffer.limit()
   */
public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException {
    CharsetEncoder encoder = ENCODER_FACTORY.get();
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray()));
    if (replace) {
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return bytes;
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 14 with CharsetEncoder

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

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 15 with CharsetEncoder

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

the class CharsetEncoderTest method test_replaceWith.

// None of the harmony or jtreg tests actually check that replaceWith does the right thing!
public void test_replaceWith() throws Exception {
    Charset ascii = Charset.forName("US-ASCII");
    CharsetEncoder e = ascii.newEncoder();
    e.onMalformedInput(CodingErrorAction.REPLACE);
    e.onUnmappableCharacter(CodingErrorAction.REPLACE);
    e.replaceWith("=".getBytes("US-ASCII"));
    String input = "hello٦world";
    String output = ascii.decode(e.encode(CharBuffer.wrap(input))).toString();
    assertEquals("hello=world", output);
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)177 ByteBuffer (java.nio.ByteBuffer)87 Charset (java.nio.charset.Charset)50 CharBuffer (java.nio.CharBuffer)42 CharacterCodingException (java.nio.charset.CharacterCodingException)31 CoderResult (java.nio.charset.CoderResult)31 OutputStreamWriter (java.io.OutputStreamWriter)21 IOException (java.io.IOException)15 BufferedWriter (java.io.BufferedWriter)13 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