/** * Doubles the size of the backing {@link ByteBuffer} and copies the old data towards the * end of the new buffer (since we build the buffer backwards). * * @param bb The current buffer with the existing data. * @param bb_factory The factory to be used for allocating the new internal buffer * @return A new byte buffer with the old data copied copied to it. The data is * located at the end of the buffer. */ static ByteBuffer growByteBuffer(ByteBuffer bb, ByteBufferFactory bb_factory) { int old_buf_size = bb.capacity(); if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int. throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes."); int new_buf_size = old_buf_size == 0 ? 1 : old_buf_size << 1; bb.position(0); ByteBuffer nbb = bb_factory.newByteBuffer(new_buf_size); nbb.position(new_buf_size - old_buf_size); nbb.put(bb); return nbb; }
/** * Prepare to write an element of `size` after `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, `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(); ByteBuffer old = bb; bb = growByteBuffer(old, bb_factory); if (old != bb) { bb_factory.releaseByteBuffer(old); } space += bb.capacity() - old_buf_size; } pad(align_size); }
/** * Prepare to write an element of `size` after `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, `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(); ByteBuffer old = bb; bb = growByteBuffer(old, bb_factory); if (old != bb) { bb_factory.releaseByteBuffer(old); } space += bb.capacity() - old_buf_size; } pad(align_size); }
/** * Prepare to write an element of `size` after `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, `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(); ByteBuffer old = bb; bb = growByteBuffer(old, bb_factory); if (old != bb) { bb_factory.releaseByteBuffer(old); } space += bb.capacity() - old_buf_size; } pad(align_size); }
/** * Doubles the size of the backing {@link ByteBuffer} and copies the old data towards the * end of the new buffer (since we build the buffer backwards). * * @param bb The current buffer with the existing data. * @param bb_factory The factory to be used for allocating the new internal buffer * @return A new byte buffer with the old data copied copied to it. The data is * located at the end of the buffer. */ static ByteBuffer growByteBuffer(ByteBuffer bb, ByteBufferFactory bb_factory) { int old_buf_size = bb.capacity(); if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int. throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes."); int new_buf_size = old_buf_size == 0 ? 1 : old_buf_size << 1; bb.position(0); ByteBuffer nbb = bb_factory.newByteBuffer(new_buf_size); nbb.position(new_buf_size - old_buf_size); nbb.put(bb); return nbb; }
/** * Doubles the size of the backing {@link ByteBuffer} and copies the old data towards the * end of the new buffer (since we build the buffer backwards). * * @param bb The current buffer with the existing data. * @param bb_factory The factory to be used for allocating the new internal buffer * @return A new byte buffer with the old data copied copied to it. The data is * located at the end of the buffer. */ static ByteBuffer growByteBuffer(ByteBuffer bb, ByteBufferFactory bb_factory) { int old_buf_size = bb.capacity(); if ((old_buf_size & 0xC0000000) != 0) // Ensure we don't grow beyond what fits in an int. throw new AssertionError("FlatBuffers: cannot grow buffer beyond 2 gigabytes."); int new_buf_size = old_buf_size == 0 ? 1 : old_buf_size << 1; bb.position(0); ByteBuffer nbb = bb_factory.newByteBuffer(new_buf_size); nbb.position(new_buf_size - old_buf_size); nbb.put(bb); return nbb; }
/** * Start with a buffer of size `initial_size`, then grow as required. * * @param initial_size The initial size of the internal buffer to use. * @param bb_factory The factory to be used for allocating the internal buffer */ public FlatBufferBuilder(int initial_size, ByteBufferFactory bb_factory) { if (initial_size <= 0) initial_size = 1; space = initial_size; this.bb_factory = bb_factory; bb = bb_factory.newByteBuffer(initial_size); }
/** * Start with a buffer of size `initial_size`, then grow as required. * * @param initial_size The initial size of the internal buffer to use. * @param bb_factory The factory to be used for allocating the internal buffer */ public FlatBufferBuilder(int initial_size, ByteBufferFactory bb_factory) { if (initial_size <= 0) initial_size = 1; space = initial_size; this.bb_factory = bb_factory; bb = bb_factory.newByteBuffer(initial_size); }
/** * Start with a buffer of size `initial_size`, then grow as required. * * @param initial_size The initial size of the internal buffer to use. * @param bb_factory The factory to be used for allocating the internal buffer */ public FlatBufferBuilder(int initial_size, ByteBufferFactory bb_factory) { if (initial_size <= 0) initial_size = 1; space = initial_size; this.bb_factory = bb_factory; bb = bb_factory.newByteBuffer(initial_size); }