code: public static byte[] toArray(ByteBuffer buffer) {
          if (buffer.hasArray()) {
              byte[] array = buffer.array();
              int from = buffer.arrayOffset() + buffer.position();
              return Arrays.copyOfRange(array, from, from + buffer.remaining());
          } else {
              byte[] to = new byte[buffer.remaining()];
              buffer.slice().get(to);
              return to;
          }
      }
docs: Convert a ByteBuffer to a byte array.
  
  @param buffer The buffer to convert in flush mode. The buffer is not altered.
  @return An array of bytes duplicated from the buffer.