code: public static ByteBuffer allocate(int capacity) {
          ByteBuffer buf = ByteBuffer.allocate(capacity);
          buf.limit(0);
          return buf;
      }
docs: Allocate ByteBuffer in flush mode.
  The position and limit will both be zero, indicating that the buffer is
  empty and must be flipped before any data is put to it.
  
  @param capacity capacity of the allocated ByteBuffer
  @return Buffer