Code example for CharsetEncoder

Methods: encodeonUnmappableCharacter

0
Charset charset = Charset.forName(charsetName);
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    // Convert a string to charsetName bytes in a ByteBuffer 
    // The new ByteBuffer is ready to be read. 
    ByteBuffer buf = encoder.encode(CharBuffer.wrap(str));
    // Convert charsetName bytes in a ByteBuffer to a character ByteBuffer 
    // and then to a string. The new ByteBuffer is ready to be read. 
    CharBuffer cbuf = decoder.decode(buf);
    return cbuf.toString();
  } 
 
  /** 
   * concatenate nio-ByteBuffers 
   *  
   * @param buffers 
   *          ArrayList<ByteBuffer> 
Stop searching for code, let great code find you!  Add Codota to your java IDE