title: Insufficient buffer size when trying to create a texture with openGL
body: <p>I'm currently trying to generate a textured polygonal surface using JOGL, and I'm getting an error message I don't understand.  Eclipse tells me "java.lang.IndexOutOfBoundsException: Required 430233 remaining bytes in buffer, only had 428349".  As far as I can see, the buffered image being generated by the readTexture method is not of sufficient size to use with the glTex2D() method.  However, I'm not sure how to go about resolving the issue.  The relevant sections of code are below, and any help would be much appreciated.</p>
  
  <pre><code>public void init(GLAutoDrawable drawable) 
  {
      final GL2 gl = drawable.getGL().getGL2();
      GLU glu = GLU.createGLU();
      //Create the glu object which allows access to the GLU library\
  
      gl.glShadeModel(GL2.GL_SMOOTH);              // Enable Smooth Shading
      gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
      gl.glClearDepth(1.0f);                      // Depth Buffer Setup
      gl.glEnable(GL.GL_DEPTH_TEST);              // Enables Depth Testing
      gl.glDepthFunc(GL.GL_LEQUAL);               // The Type Of Depth Testing To Do
      gl.glEnable(GL.GL_TEXTURE_2D);
      texture = genTexture(gl);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
      TextureReader.Texture texture = null;
      try {
          texture = TextureReader.readTexture   ("/C:/Users/Alex/Desktop/boy_reaching_up_for_goalpost_stencil.png");
      } catch (IOException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
      }
      makeRGBTexture(gl, glu, texture, GL.GL_TEXTURE_2D, false);
      gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
      gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
  
  }
  
  private void makeRGBTexture(GL gl, GLU glu, TextureReader.Texture img, 
          int target, boolean mipmapped) {
  
      if (mipmapped) {
          glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), 
                  img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
      } else {
          gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), 
                  img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, img.getPixels());
      }
  }
  
  private int genTexture(GL gl) {
      final int[] tmp = new int[1];
      gl.glGenTextures(1, tmp, 0);
      return tmp[0];
  }
  
      //Within the TextureReader class
      public static Texture readTexture(String filename, boolean storeAlphaChannel)
  
                      throws IOException {
  
              BufferedImage bufferedImage;
  
              if (filename.endsWith(".bmp")) {
  
                      bufferedImage = BitmapLoader.loadBitmap(filename);
  
              } else {
  
                      bufferedImage = readImage(filename);
  
              }
  
              return readPixels(bufferedImage, storeAlphaChannel);
  
      }
  </code></pre>
  
  <p>The error is being generated by the call to glTexImage2D() inside the makeRGBTexture() method.</p>