title: Java:How can i get the encoding from inputStream?
body: <p>I want get the encoding from a stream.</p>
  
  <p><em><strong>1st method - to use the InputStreamReader.</em></strong></p>
  
  <p>But it always return OS encode.</p>
  
  <pre><code>InputStreamReader reader = new InputStreamReader(new FileInputStream("aa.rar"));
  System.out.println(reader.getEncoding());
  </code></pre>
  
  <blockquote>
    <p>output:GBK</p>
  </blockquote>
  
  <p><em><strong>2nd method - to use the UniversalDetector.</em></strong></p>
  
  <p>But it always return null.</p>
  
  <pre class="lang-java prettyprint-override"><code>    FileInputStream input = new FileInputStream("aa.rar");
  
      UniversalDetector detector = new UniversalDetector(null);
      byte[] buf = new byte[4096];
  
      int nread;
      while ((nread = input.read(buf)) &gt; 0 &amp;&amp; !detector.isDone()) {
          detector.handleData(buf, 0, nread);
      }
  
      // (3)
      detector.dataEnd();
  
      // (4)
      String encoding = detector.getDetectedCharset();
  
      if (encoding != null) {
          System.out.println("Detected encoding = " + encoding);
      } else {
          System.out.println("No encoding detected.");
      }
  
      // (5)
      detector.reset();
  </code></pre>
  
  <blockquote>
    <p>output:null</p>
  </blockquote>
  
  <p>How can I get the right?  :(</p>