title: Socket InputStream and UTF-8
body: <p>I'm trying to make a chat with Java. Everything works fine, except that special characters doesn't work. I think that it's an encoding problem because in my <code>Outputstream</code> I encode the string in UTF-8 like this:</p>
  <pre><code>  protected void send(String msg) {
      
          try {
            msg+=&quot;\r\n&quot;;            
            OutputStream outStream = socket.getOutputStream();              
            outStream.write(msg.getBytes(&quot;UTF-8&quot;));
            System.out.println(msg.getBytes(&quot;UTF-8&quot;));
            outStream.flush();
          }
          catch(IOException ex) {
            ex.printStackTrace();
          }
        }
  </code></pre>
  <p>But in my <code>receive</code> method I didn't find a way to do this:</p>
  <pre><code>public String receive() throws IOException {
     
      String line = &quot;&quot;;
      InputStream inStream = socket.getInputStream();    
                  
      int read = inStream.read();
      while (read!=10 &amp;&amp; read &gt; -1) {
        line+=String.valueOf((char)read);
        read = inStream.read();
      }
      if (read==-1) return null;
      line+=String.valueOf((char)read);       
      return line; 
      
    }
  </code></pre>
  <p>So there is a quick way to specify that the bytes read by the buffer are encoded with UTF-8?</p>
  <p>EDIT: Okay, I tried with the <code>BufferedReader</code> like this:</p>
  <pre><code> public String receive() throws IOException {
      
      String line = &quot;&quot;;           
      in = new BufferedReader(new InputStreamReader(socket.getInputStream(), &quot;UTF-8&quot;));           
      String readLine = &quot;&quot;;   
      
      while ((readLine = in.readLine()) != null) {
          line+=readLine;
      }
      
      System.out.println(&quot;Line:&quot;+line);
      
      return line;
     
    }
  </code></pre>
  <p>But it doesn't work. It seems that the socket doesn't receive anything.</p>