I thought that the offsets are stored as short or int, depending on how big the offsets get? You won't know this in advance. Why not allocate an int[] where you just store the offset, then at the end of the loop, serialize them into the byteStream as you'll know how big they will be then?