title: How to find out total number of bytes stored in ByteBuffer?
body: <p>I am trying to extract total number of bytes I have stored in my ByteBuffer <code>data_record_value</code>.</p>
  
  <p>Below is how we represent<code>data_record_value</code> as one Byte Array and then write to our database from Java code.</p>
  
  <pre><code>Offset   Length (in bytes)          Purpose
  
  0           2                       - clientId byte array
  2           8                       - value of lastModifiedDate
  2+8         4                       - length of avroBinaryValue array
  2+8+4       Y                       - avroBinaryValue array
  </code></pre>
  
  <p>And this is the way I am extracting it from database - </p>
  
  <pre><code>ByteBuffer data_record_value = r.getBytes("data_record_value");
  
  // now how do I extract total number of bytes I have stored in data_record_value?
  </code></pre>
  
  <p>After doing some research I found out multiple ways of extracting the total number of bytes stored in my ByteBuffer <code>data_record_value</code> and I am not sure which one is right?</p>
  
  <p><strong>First way is :</strong></p>
  
  <pre><code>byte[] b = new byte[data_record_value.remaining()];
  record_value.get(b);
  
  // this will be in bytes right?
  System.out.println("Byte Array Length: " + b.length);
  </code></pre>
  
  <p><strong>Second way is :</strong> </p>
  
  <pre><code>int numberOfBytesInRecord = data_record_value.limit();
  </code></pre>
  
  <p><strong>Third way is :</strong> </p>
  
  <pre><code>int numberOfBytesInRecord = data_record_value.remaining();
  </code></pre>
  
  <p>But the numbers from all the above ways doesn't match at all with each other? I am not sure which one I should use? I need to extract total number of bytes stored in <code>data_record_value</code>.</p>
  
  <p>To cross check, we can extract individual fields from <code>data_record_value</code> ByteBuffer and calculate the total bytes we are storing and compare with any of the above ways.</p>
  
  <pre><code>// clientId (of two bytes by using short)
  short extractClientId = data_record_value.getShort();
  
  // lastModifiedDate ( of 8 bytes which can be long )
  long extractLastModifiedDate = data_record_value.getLong();
  
  int extractAvroBinaryValueLength = data_record_value.getInt();
  
  byte[] extractAvroBinaryValue = new byte[extractAvroBinaryValueLength];
  
  data_record_value.get(extractAvroBinaryValue); // read attributeValue from the remaining buffer
  
  System.out.println(extractClientId);
  System.out.println(extractLastModifiedDate);
  System.out.println(new String(extractAvroBinaryValue));
  </code></pre>
  
  <p><strong>Update:-</strong></p>
  
  <p>On one of the ByteBuffer <code>data_record_value</code>, this is what I printed out - </p>
  
  <pre><code>System.out.println("Record Value Capacity: " + data_record_value.capacity());
  System.out.println("Record Value Position: " + data_record_value.position());
  System.out.println("Record Value Limit: " + data_record_value.limit());
  System.out.println("Record Value Remaining: " + data_record_value.remaining());
  System.out.println("Record Value: " + data_record_value);
  </code></pre>
  
  <p>And this is what got printed - </p>
  
  <pre><code>Record Value Capacity: 387
  Record Value Position: 185
  Record Value Limit: 250
  Record Value Remaining: 65
  Record Value: java.nio.HeapByteBuffer[pos=185 lim=250 cap=387]
  </code></pre>