Java String Split by Length splitByteArray(final byte[] data, final int packetLength)

Here you can find the source of splitByteArray(final byte[] data, final int packetLength)

Description

Splits a byte array into smaller chunks

License

Open Source License

Parameter

Parameter Description
data array to split
packetLength chunk size

Return

to document

Declaration

public static List<byte[]> splitByteArray(final byte[] data, final int packetLength) 

Method Source Code

//package com.java2s;
/**//from   www .jav  a2 s .c om
 *     Copyright (c) 2013, Will Szumski
 *
 *     This file is part of formicidae.
 *
 *     formicidae 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, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     formicidae 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 formicidae.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
       * Splits a byte array into smaller chunks
       * @param data array to split
       * @param packetLength chunk size
       * @return to document
       */
    public static List<byte[]> splitByteArray(final byte[] data, final int packetLength) {
        List<byte[]> split = new ArrayList<byte[]>();
        int wholePackets = (int) (data.length / packetLength);

        int index = 0;
        for (int i = 0; i < wholePackets; i++) {
            byte[] dataPacket = new byte[packetLength];
            for (int j = 0; j < packetLength; j++) {
                dataPacket[j] = data[index];
                index++;
            }
            split.add(dataPacket);
        }
        // pad partial
        if (data.length == 0 || index != data.length) {
            byte[] dataPacket = new byte[packetLength];
            int i = 0;
            while (index < data.length) {
                dataPacket[i] = data[index];
                index++;
                i++;
            }
            split.add(dataPacket);
        }

        return split;

    }
}

Related

  1. split(final String value, final int maxLength)
  2. split(String message, int maxPages, int pageSize)
  3. splitArray(byte[] array, int len)
  4. splitByLength(String string, int len)
  5. splitByteArray(byte[] inByteArray, int length)
  6. splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)
  7. splitByWholeSeparatorWorker(final String str, final String separator, final int max, final boolean preserveAllTokens)
  8. splitEqually(String _text, int _len)
  9. splitEscapedString(String str, char sep, char esc, int maxPart)