title: DataInputStream and UTF-8
body: <p>I'm kind of a new programmer, and I'm having a couple of problems with the code I'm handling.</p>
  
  <p>Basically what the code does is receive a form from another JSP, read the bytes, parse the data, and submit the results to SalesForce, using DataInputStream.</p>
  
  <pre><code>   //Getting the parameters from request
   String contentType = request.getContentType();
   DataInputStream in = new DataInputStream(request.getInputStream());
   int formDataLength = request.getContentLength();
  
   //System.out.println(formDataLength);
   byte dataBytes[] = new byte[formDataLength];
   int byteRead = 0;
   int totalBytesRead = 0;
   while (totalBytesRead &lt; formDataLength) 
   {
    byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
    totalBytesRead += byteRead;
   }
  </code></pre>
  
  <p>It works fine, but only if the code handles normal characters. Whenever it tries to handle special characters (like french chars: àâäæçéèêëîïôùûü) I get the following gibberish as a result: </p>
  
  <p>Ã Ã¢Ã¤Ã¦Ã§Ã©Ã¨ÃªÃ«Ã®Ã¯Ã´Ã¹Ã»Ã¼</p>
  
  <p>I understand it could be an issue of DataInputStream, and how it doesn't return UTF-8 encoded text. Do you guys offer any suggestions on how to tackle this issue? </p>
  
  <p>All the .jsp files include &lt;%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> and Tomcat's settings are fine (URI = UTF-8, etc). I tried adding:</p>
  
  <p><code>request.setCharacterEncoding("UTF-8");</code> </p>
  
  <p>and </p>
  
  <p><code>response.setCharacterEncoding("UTF-8");</code></p>
  
  <p>to no avail.</p>
  
  <p>Here's an example of how it parses the data:</p>
  
  <pre><code>    //Getting the notes for the Case 
   String notes = new String(dataBytes);
   System.out.println(notes);
   String savenotes = casetype.substring(notes.indexOf("notes"));
   //savenotes = savenotes.substring(savenotes.indexOf("\n"), savenotes.indexOf("---"));
   savenotes = savenotes.substring(savenotes.indexOf("\n")+1);
   savenotes = savenotes.substring(savenotes.indexOf("\n")+1);
   savenotes = savenotes.substring(0,savenotes.indexOf("name=\"datafile"));
   savenotes = savenotes.substring(0,savenotes.lastIndexOf("\n------"));
   savenotes = savenotes.trim();
  </code></pre>
  
  <p>Thanks in advance.</p>