code: public static char[] copyToBuffer(final String str, final char[] currentBuffer) {
          assert str != null;
          assert currentBuffer != null;
          assert currentBuffer.length > 0;
  
          final int strLen = str.length();
          char[] buffer = currentBuffer;
          int bufferLen = buffer.length;
  
          while(strLen > bufferLen) {
              bufferLen *= 2;
          }
          if (bufferLen > buffer.length) {
              buffer = new char[bufferLen];
          }
          str.getChars(0, strLen, buffer, 0);
          return buffer;
      }
docs: Copies the contents of {@code str} into the {@code currentBuffer}. If the size of
  the buffer is not sufficient to store the string then a new buffer is allocated.
  {@code copyToBuffer} is intended to be used as shown in the example:
  <pre>{@code
  final int INITIAL_SIZE = 10;
  char[] myBuffer = new char[INITIAL_SIZE];
  
  List<String> strings = getAListOfStringsFromSomewhere();
  for (String str : strings) {
  myBuffer = StringUtil.copyToBuffer(str, myBuffer);
  dataLen = str.length();
  // Process data in myBuffer in the range from 0 to dataLen
  }
  myBuffer = null;
  }</pre>
  
  This allows the buffer to be reused but at the same time frees the user from
  having to manage the size of the buffer.
  
  @param str string to copy. Must not be null.
  @param currentBuffer array to store the string in. If it is too small a new buffer
  is allocated.  currentBuffer must not be null.
  The length of the current buffer must not be 0.
  @return either currentBuffer or a new buffer if one was allocated. The returned
  buffer may be larger than the string. Users should call
  {@code str.length()} to retrieve the actual length of the
  data in the returned buffer.