title: How to copy partially one byteBuffer to another one without byteBuffer.array()
body: <p>I have an empty byteBuffer allocated as</p>
  
  <p><code>data = ByteBuffer.allocateDirect(layerSize(0, faces - 1, 0, levels - 1) * layers);</code></p>
  
  <p>Following <a href="https://stackoverflow.com/a/570261/1047713">this answer</a>, I tried to use the <code>array()</code> method as following</p>
  
  <pre><code>public void setData(ByteBuffer data, int layer, int face, int level) {
      int offset = offset(layer, face, level);
      int levelSize = levelSize(level);
      this.data.put(data.array(), offset, levelSize);
  }
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>Caused by: java.lang.UnsupportedOperationException
      at java.nio.ByteBuffer.array(ByteBuffer.java:994)
  </code></pre>
  
  <p>The source bytebuffer I am trying to use is read in this way:</p>
  
  <pre><code>    File file = new File(Load.class.getResource(fileName).getFile());
      FileInputStream fileInputStream = new FileInputStream(file);
      FileChannel fileChannel = fileInputStream.getChannel();
  
      return loadKtx(fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) file.length()));
  
  public static Texture loadKtx(ByteBuffer byteBuffer) throws IOException {
      ...
                  byteBuffer.position(offset);
                  byteBuffer.limit(offset + faceSize);
  
                  ByteBuffer data = byteBuffer.slice();
                  texture.setData(data, layer, face, level);
  
                  byteBuffer.position(0);
                  byteBuffer.limit(byteBuffer.capacity());
  </code></pre>
  
  <p>Is there a better option than a simple</p>
  
  <pre><code>    for (int b = 0; b &lt; levelSize; b++) {
          this.data.put(offset + b, data.get(b));
      }
  </code></pre>
  
  <p>?</p>
  
  <p>However, whole project <a href="https://github.com/elect86/jgli/tree/master/Jgli/src/jgli" rel="nofollow noreferrer">here</a> for who is interested.</p>