Java Array Split splitArray(byte[] src, int size)

Here you can find the source of splitArray(byte[] src, int size)

Description

Splits an array into more chunks with the specified maximum size for each array chunk.

License

Open Source License

Parameter

Parameter Description
src the original array.
size the max size for each array that has been split.

Return

the split byte array's no bigger than the maximum size.

Declaration

public static final byte[][] splitArray(byte[] src, int size) 

Method Source Code


//package com.java2s;
/*//from  w w w.ja  va  2  s . c o m
 *       _   _____            _      _   _          _   
 *      | | |  __ \          | |    | \ | |        | |  
 *      | | | |__) |   __ _  | | __ |  \| |   ___  | |_ 
 *  _   | | |  _  /   / _` | | |/ / | . ` |  / _ \ | __|
 * | |__| | | | \ \  | (_| | |   <  | |\  | |  __/ | |_ 
 *  \____/  |_|  \_\  \__,_| |_|\_\ |_| \_|  \___|  \__|
 *                                                  
 * The MIT License (MIT)
 *
 * Copyright (c) 2016, 2017 Trent "MarfGamer" Summerlin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.  
 */

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    /**
     * Splits an array into more chunks with the specified maximum size for each
     * array chunk.
     * 
     * @param src
     *            the original array.
     * @param size
     *            the max size for each array that has been split.
     * @return the split byte array's no bigger than the maximum size.
     */
    public static final byte[][] splitArray(byte[] src, int size) {
        int index = 0;
        ArrayList<byte[]> split = new ArrayList<byte[]>();
        while (index < src.length) {
            if (index + size <= src.length) {
                split.add(Arrays.copyOfRange(src, index, index + size));
                index += size;
            } else {
                split.add(Arrays.copyOfRange(src, index, src.length));
                index = src.length;
            }
        }
        return split.toArray(new byte[split.size()][size]);
    }
}

Related

  1. split(Collection collection, C[] array, int pageSize)
  2. split(final int[] array)
  3. split(int splitBefore, byte[] source)
  4. split(String string, String delim, String[] a)
  5. splitAndPad(byte[] byteArray, int blocksize)
  6. splitArray(int[] array, int limit)
  7. splitArray(T[] array, int capacity)
  8. splitArray(T[] array, int max)
  9. splitArray(T[] elementArray, int maxSubArraySize)