title: How to use Java to encode/decode/transcode byte array using a charset directly
body: <p>I have a malformed string which may be caused by a <em>bug</em> of MySQL JDBC driver,</p>
  
  <p>The bytes of a sample malformed string (<code>malformed_string.getBytes("UTF-8")</code>) is this:</p>
  
  <pre><code>C3 A4 C2 B8 C2 AD C3 A6 E2 80 93 E2 80 A1  (UTF-8 twice)
  </code></pre>
  
  <p>which should encoded the following bytes (it's already UTF-8 encoded, but treat them as ISO-8859-1 enoded)</p>
  
  <pre><code>----- ----- ----- ----- -------- --------
    E4   B8    AD     E6     96       87     (UTF-8)
  </code></pre>
  
  <p>which should encoded the following Unicode BigEndian bytes</p>
  
  <pre><code>---------------     ---------------------
       4E 2D                65 87            (Unicode BigEndian)
  </code></pre>
  
  <p>I want to decode the 1st one to the 2nd one, I tried <code>new String(malformed_string.getBytes("UTF-8"), "ISO-8859-1")</code>, but it does not transcode as expected. I'm wondering if there's something like <code>byte[] encode/decode (byte[] src, String charsetName)</code>, or how to achieve the transcode above in java?</p>
  
  <p><strong>Background:</strong></p>
  
  <p>I have a MySQL table which have Chinese column names, when I update such columns with long data, MySQL JDBC driver thrown an exception like this:</p>
  
  <pre><code>com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'ä¸­æ–‡' at row 1
  </code></pre>
  
  <p>The column name in the exception is malformed, it should be "中文", and it must be correctly displayed to user as the following.</p>
  
  <pre><code>com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column '中文' at row 1
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's MySQL statement to demonstrate how the malformed string occured, and how to restore it to correct string</p>
  
  <pre><code>show variables like 'char%';
  +--------------------------+--------------------------+
  | Variable_name            | Value                    |
  +--------------------------+--------------------------+
  | character_set_client     | utf8                     |
  | character_set_connection | utf8                     |
  | character_set_database   | utf8                     |
  | character_set_filesystem | binary                   |
  | character_set_results    | utf8                     |
  | character_set_server     | utf8                     |
  | character_set_system     | utf8                     |
  | character_sets_dir       | C:\mysql\share\charsets\ |
  +--------------------------+--------------------------+
  
  -- encode
  select
      hex(convert(convert(unhex('E4B8ADE69687') using UTF8) using ucs2)) as `hex(src in UNICODE)`,
      unhex('E4B8ADE69687') `src in UTF8`,
      'E4B8ADE69687' `hex(src in UTF8)`,
      hex(convert(convert(unhex('E4B8ADE69687') using latin1) using UTF8)) as `hex(src in UTF8-&gt;Latin1-&gt;UTF8)`;
  +---------------------+-------------+------------------+--------------------------------+
  | hex(src in UNICODE) | src in UTF8 | hex(src in UTF8) | hex(src in UTF8-&gt;Latin1-&gt;UTF8) |
  +---------------------+-------------+------------------+--------------------------------+
  | 4E2D6587            | 中文        | E4B8ADE69687     | C3A4C2B8C2ADC3A6E28093E280A1   |
  +---------------------+-------------+------------------+--------------------------------+
  1 row in set (0.00 sec)
  
  
  -- decode
  select
      unhex('C3A4C2B8C2ADC3A6E28093E280A1') as `malformed`,
      'C3A4C2B8C2ADC3A6E28093E280A1' as `hex(malformed)`,
      hex(convert(convert(unhex('C3A4C2B8C2ADC3A6E28093E280A1') using utf8) using latin1)) as `hex(malformed-&gt;UTF8-&gt;Latin1)`,
      convert(convert(convert(convert(unhex('C3A4C2B8C2ADC3A6E28093E280A1') using utf8) using latin1) using binary)using utf8) `malformed-&gt;UTF8-&gt;Latin1-&gt;binary-&gt;UTF8`;
  +----------------+------------------------------+------------------------------+---------------------------------------+
  | malformed      | hex(malformed)               | hex(malformed-&gt;UTF8-&gt;Latin1) | malformed-&gt;UTF8-&gt;Latin1-&gt;binary-&gt;UTF8 |
  +----------------+------------------------------+------------------------------+---------------------------------------+
  | ä¸­æ–‡         | C3A4C2B8C2ADC3A6E28093E280A1 | E4B8ADE69687                 | 中文                                  |
  +----------------+------------------------------+------------------------------+---------------------------------------+
  1 row in set (0.00 sec)
  </code></pre>