/** * Prepare to write an element of {@code size} after {@code additional_bytes} have been written, * e.g. if you write a string, you need to align such the int length field is aligned to {@link * com.google.flatbuffers.Constants#SIZEOF_INT}, and the string data follows it directly. If all * you need to do is alignment, {@code additional_bytes} will be 0. * * @param size This is the of the new element to write * @param additional_bytes The padding size */ public void prep(int size, int additional_bytes) { // Track the biggest thing we've ever aligned to. if (size > minalign) minalign = size; // Find the amount of alignment needed such that `size` is properly // aligned after `additional_bytes` int align_size = ((~(bb.capacity() - space + additional_bytes)) + 1) & (size - 1); // Reallocate the buffer if needed. while (space < align_size + size + additional_bytes) { int old_buf_size = bb.capacity(); bb = growByteBuffer(bb); space += bb.capacity() - old_buf_size; } pad(align_size); }