Here you can find the source of split(final byte[] array, final int chunkSize)
Parameter | Description |
---|---|
array | the array to split. |
chunkSize | the size of a chunk. |
public static List<byte[]> split(final byte[] array, final int chunkSize)
//package com.java2s; /*/*from www .j a va 2 s. co m*/ * Cacheonix systems licenses this file to You under the LGPL 2.1 * (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.cacheonix.com/products/cacheonix/license-lgpl-2.1.htm * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits an array into a list of arrays. * * @param array * the array to split. * @param chunkSize * the size of a chunk. * @return a list of arrays. */ public static List<byte[]> split(final byte[] array, final int chunkSize) { if (array == null || array.length == 0) { return new ArrayList<byte[]>(0); } else { // Get counters final int fullChunkCount = array.length / chunkSize; final int lastChunkSize = array.length % chunkSize; final List<byte[]> result = new ArrayList<byte[]>(fullChunkCount + (lastChunkSize > 0 ? 1 : 0)); // Get full chunks for (int i = 0; i < fullChunkCount; i++) { final int from = i * chunkSize; final int to = from + chunkSize; result.add(copyOfRange(array, from, to)); } // ... Get last chunk return result; } } /** * @since 1.6 */ public static byte[] copyOfRange(final byte[] original, final int from, final int to) { final int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); } final byte[] arr = new byte[newLength]; final int ceil = original.length - from; final int len = ceil < newLength ? ceil : newLength; System.arraycopy(original, from, arr, 0, len); return arr; } }