Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Concatenates smaller fragments into entire buffers. * @param fragments An array of byte buffers (<code>byte[]</code>) * @return A byte buffer */ public static byte[] defragmentBuffer(byte[] fragments[]) { int total_length = 0; byte[] ret; int index = 0; if (fragments == null) return null; for (int i = 0; i < fragments.length; i++) { if (fragments[i] == null) continue; total_length += fragments[i].length; } ret = new byte[total_length]; for (int i = 0; i < fragments.length; i++) { if (fragments[i] == null) continue; System.arraycopy(fragments[i], 0, ret, index, fragments[i].length); index += fragments[i].length; } return ret; } }