title: Why does my Unicode String get corrupted, when passed from Java Applet to Java Script?
body: <p>I'm pretty new, so don't be too harsh :)</p>
  
  <h1>Question(tl;dr)</h1>
  
  <p>I'm facing a problem passing an unicode <code>String</code> from an embedded <code>javax.swing.JApplet</code> in a web page to the Java Script part. I'm not sure this is whether a bug or a misunderstanding of the involved technologies:</p>
  
  <h1>Problem</h1>
  
  <p>I want to pass a unicode string from a Java Applet to Java Script, but the String gets messed up. Strangely, the problem doesn't occur not in <em>Internet Explorer 10</em> but in <em>Chrome (v26)</em> and <em>Firefox (v20)</em>. I haven't tested other browsers though.</p>
  
  <p>The returned String seems to be okay, except for the last unicode character. The result in the Java Script Debugger and Web Page would be:</p>
  
  <ul>
  <li>abc → abc    </li>
  <li>表示 → 表��</li>
  <li>ま → ま</li>
  <li>ウォッチリスト → ウォッチリス��</li>
  <li>アップロード → アップロー��</li>
  <li>ホ → ��</li>
  <li>ホ → ホ (Not deterministic)</li>
  <li>アップロードabc → アップロードabc </li>
  </ul>
  
  <p>The string seems to get corrupted at the last bytes. If it ends with an ASCII character the string is okay. Additionally the problem doesn't occur within every combination and also not every time (not sure on this). Therefore I suspect a bug and I'm afraid I might be posting an invalid question.</p>
  
  <h1>Test Set Up</h1>
  
  <p>A minimalistic set up includes an applet that returns some unicode (UTF-8) strings:</p>
  
  <pre><code>/* TestApplet.java */
  import javax.swing.*;
  
  public class TestApplet extends JApplet {
  
  private String[] testStrings = {
              "abc", // OK (because ASCII only)
              "表示", // Error on last Character
              "表示", // Error on last Character
              "ホーム ", // OK (because of *space* after ム)
              "アップロード", ... }; 
      public TestApplet() {...};     // Applet specific stuff
  
      ...
  
      public int getLength() { return testStrings.length;};
  
      String getTestString(int i) {
          return testStrings[i];    // Build-in array functionality because of IE. 
      }
  }
  </code></pre>
  
  <p>The corresponding web page with java script could look like this:</p>
  
  <pre><code> /* test.html */
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;span id="output"/&gt;
          &lt;applet id='output' archive='test.jar' code=testApplet/&gt;
      &lt;/body&gt;
  
      &lt;script type="text/javascript" charset="utf-8"&gt;
          var applet = document.getElementById('output');
          var node = document.getElementById("1");
          for(var i = 0; i &lt; applet.getLength(); i++) {
               var text = applet.getTestString(i);
           var paragraphNode = document.createElement("p");
           paragraphNode.innerHTML = text;
           node.appendChild(paragraphNode);
          }
      &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h1>Environment</h1>
  
  <p>I'm working on Windows 7 32-Bit with the current Java Version 1.7.0_21 using the "Next Generation Java Plug-in 10.21.2 for Mozilla browsers". I had some problems with my operating system locale, but I tried several (English, Japanese, Chinese) regional settings.</p>
  
  <p>In case of an corrupt String chrome shows invalid characters (e.g. ��). Firefox, on the other hand, drops the string completly, if it would be ending with ��.</p>
  
  <p>Internet explorer manages to display the strings correctly.</p>
  
  <h1>Solutions?</h1>
  
  <p>I can imagine several workarounds, including escaping/unescaping and adding a "final char" which then is removed via java script. Actually I'm planning to write against Android's Webkit, and I haven't tested it there.</p>
  
  <p>Since I would like to continue testing in Chrome, (because of Webkit technology and comfort) I hope there is a trivial solution to the problem, which I might have overlooked.</p>