Java String Split by Length splitArray(byte[] array, int len)

Here you can find the source of splitArray(byte[] array, int len)

Description

split Array

License

Apache License

Declaration

private static List<byte[]> splitArray(byte[] array, int len) 

Method Source Code

//package com.java2s;
/*/*from w  w w . j  a va2  s .  co m*/
 * Copyright 2015-2017 GenerallyCloud.com
 *  
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *  
 * 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 {

    private static List<byte[]> splitArray(byte[] array, int len) {

        int length = array.length;

        List<byte[]> list = new ArrayList<byte[]>();

        if (length <= len) {

            list.add(array);

            return list;
        }

        int size = length / len;

        for (int i = 0; i < size; i++) {
            byte[] a = new byte[len];
            System.arraycopy(array, i * len, a, 0, len);
            list.add(a);
        }

        int yu = array.length % len;

        if (yu > 0) {
            byte[] a = new byte[yu];
            System.arraycopy(array, length - yu, a, 0, yu);
            list.add(a);
        }

        return list;
    }
}

Related

  1. getSplitLines(String input, int maxLineLength)
  2. lineSplit(String text, int len)
  3. split(final String value, final int maxLength)
  4. split(String message, int maxPages, int pageSize)
  5. splitByLength(String string, int len)
  6. splitByteArray(byte[] inByteArray, int length)
  7. splitByteArray(final byte[] data, final int packetLength)
  8. splitByTrimmedDelimiterNonNestedInBracketsOrQuotesOrComments(final String s, final char delimiter, final char quoteChar, char bracketChar, final boolean includeIntermediaryEmptyElmts, final int maxElmts, String cmtStart, String cmtEnd)