title: Safe usage of glMapBufferRange() on Android/Java
body: <p>I have working code using <code>glMapBufferRange()</code> from OpenGL-ES 3.0 on Android that looks like this:</p>
  
  <pre><code>  glBindBuffer(GL_ARRAY_BUFFER, myVertexBufferName);
    glBufferData(GL_ARRAY_BUFFER, myVertexBufferSize, null, GL_STATIC_DRAW);
    ByteBuffer mappedBuffer = (ByteBuffer)glMapBufferRange(
      GL_ARRAY_BUFFER,
      0, myVertexBufferSize,
      GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
  
    // [fill buffer...]
  
    glUnmapBuffer(GL_ARRAY_BUFFER);
  </code></pre>
  
  <p>My question is about downcasting the result of <code>glMapBufferRange()</code> to <code>ByteBuffer</code> on the third line. <code>glMapBufferRange()</code> is <a href="http://developer.android.com/reference/android/opengl/GLES30.html#glMapBufferRange(int,%20int,%20int,%20int)">declared to return a <code>Buffer</code></a>:</p>
  
  <blockquote>
    <p><code>public static Buffer glMapBufferRange (int target, int offset, int length, int access)</code></p>
  </blockquote>
  
  <p>On my test platform the function returns a subclass of <code>ByteBuffer</code> so the cast works, but making this assumption for all platforms or Android versions supporting OpenGL-ES 3+ doesn't seem very safe. Although it seems reasonable, I haven't found any documentation that guarantees it, and if it were guaranteed it seems like the function should be declared as returning <code>ByteBuffer</code>.</p>
  
  <p>What is the correct way (preferably supported by documentation) of using the <code>Buffer</code> returned by <code>glMapBufferRange()</code>?</p>