private static Buffer concat(List<Buffer> buffers){
  int totalLength=0;
  Buffer largestBuffer=null;
  for (  Buffer buffer : buffers) {
    totalLength+=buffer.length;
    if (largestBuffer == null || buffer.array.length > largestBuffer.array.length) {
      largestBuffer=buffer;
    }
  }
  if (largestBuffer == null) {
    return new Buffer(1);
  }
  int[] docs=largestBuffer.array;
  if (docs.length < totalLength + 1) {
    docs=Arrays.copyOf(docs,totalLength + 1);
  }
  totalLength=largestBuffer.length;
  for (  Buffer buffer : buffers) {
    if (buffer != largestBuffer) {
      System.arraycopy(buffer.array,0,docs,totalLength,buffer.length);
      totalLength+=buffer.length;
    }
  }
  return new Buffer(docs,totalLength);
}

private static Buffer concat ( List < Buffer > buffers ) { int totalLength = 0 ; Buffer largestBuffer = null ; for ( Buffer buffer : buffers ) { totalLength += buffer . length ; if ( largestBuffer == null || buffer . array . length > largestBuffer . array . length ) { largestBuffer = buffer ; } } if ( largestBuffer == null ) { return new Buffer ( 1 ) ; } int [ ] docs = largestBuffer . array ; if ( docs . length < totalLength + 1 ) { docs = Arrays . copyOf ( docs , totalLength + 1 ) ; } totalLength = largestBuffer . length ; for ( Buffer buffer : buffers ) { if ( buffer != largestBuffer ) { System . arraycopy ( buffer . array , 0 , docs , totalLength , buffer . length ) ; totalLength += buffer . length ; } } return new Buffer ( docs , totalLength ) ; }
concatenate the buffers in any order , leaving at least one empty slot in the end note : this method might reuse one of the arrays
