Here you can find the source of splitArray(byte[] src, int size)
size
.
Parameter | Description |
---|---|
src | the source array |
size | the maximum size of the chunks |
public static List<byte[]> splitArray(byte[] src, int size)
//package com.java2s; /*/*from www . ja v a2 s . c om*/ * Copyright 2004 - 2008 Christian Sprajc. All rights reserved. * * This file is part of PowerFolder. * * PowerFolder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * PowerFolder is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PowerFolder. If not, see <http://www.gnu.org/licenses/>. * * $Id: Util.java 20555 2012-12-25 04:15:08Z glasgow $ */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits an array into a list of smaller arrays with the maximum size of * <code>size</code>. * * @param src * the source array * @param size * the maximum size of the chunks * @return the list of resulting arrays */ public static List<byte[]> splitArray(byte[] src, int size) { int nChunks = src.length / size; List<byte[]> chunkList = new ArrayList<byte[]>(nChunks + 1); if (size >= src.length) { chunkList.add(src); return chunkList; } for (int i = 0; i < nChunks; i++) { byte[] chunk = new byte[size]; System.arraycopy(src, i * size, chunk, 0, chunk.length); chunkList.add(chunk); } int lastChunkSize = src.length % size; if (lastChunkSize > 0) { byte[] lastChunk = new byte[lastChunkSize]; System.arraycopy(src, nChunks * size, lastChunk, 0, lastChunk.length); chunkList.add(lastChunk); } return chunkList; } }