title: InputStream.available() and reading file compeletly notes from oracle
body: <p>according to :</p>
  
  <blockquote>
    <p>Note that while some implementations of InputStream will return the
    total number of bytes in the stream, many will not. It is never
    correct to use the return value of this method to allocate a buffer
    intended to hold all data in this stream.</p>
  </blockquote>
  
  <p>from:</p>
  
  <p><a href="http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29</a></p>
  
  <p>and this note </p>
  
  <pre><code>In particular, code of the form
  
  int n = in.available();
  byte buf = new byte[n];
  in.read(buf); 
  
  is not guaranteed to read all of the remaining bytes from the given input stream.
  </code></pre>
  
  <p><a href="http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html" rel="nofollow">http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html</a></p>
  
  <p>dose it mean that using <code>below function</code> cause not to read file completely? </p>
  
  <pre><code>/**
   * Reads a file from /raw/res/ and returns it as a byte array
   * @param res Resources instance for Mosembro
   * @param resourceId ID of resource (ex: R.raw.resource_name)
   * @return byte[] if successful, null otherwise
   */
  public static byte[] readRawByteArray(Resources res, int resourceId)
  {
      InputStream is = null;
      byte[] raw = new byte[] {};
      try {
          is = res.openRawResource(resourceId);
          raw = new byte[is.available()];
          is.read(raw);
      }
      catch (IOException e) {
          e.printStackTrace();
          raw = null;
      }
      finally {
          try {
              is.close();
          }
          catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      return raw;
  }
  </code></pre>