Do this instead (it'll write the value as a vint and return how many bytes need to be added to the offset in one shot):
<pre><code>offset += ByteUtil.vintToBytes(buffer, offset, key.length);
</code></pre>
and then just write the key bytes as you're doing