Java Array Split split(byte[] array, int index, int len, byte value)

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

Description

split

License

Apache License

Declaration

public static byte[][] split(byte[] array, int index, int len, byte value) 

Method Source Code


//package com.java2s;
/*//from ww w .jav  a2 s  .  c o m
 * BRCache http://brcache.brandao.org/
 * Copyright (C) 2015 Afonso Brandao. (afonso.rbn@gmail.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.Arrays;
import java.util.List;

public class Main {

    public static byte[][] split(byte[] array, int index, int len, byte value) {
        int maxIndex = index + len;
        int start = index;
        int end = 0;
        byte[][] result = new byte[10][];
        int resultIndex = 0;

        int limit = maxIndex - 1;

        for (int i = index; i < maxIndex; i++) {

            if (array[i] == value) {
                end = i;
                byte[] item = copy(array, start, end);

                if (resultIndex >= result.length) {
                    result = Arrays.copyOf(result, result.length + 10);
                }
                result[resultIndex++] = item;

                start = end + 1;
                end = start;
            }

        }

        if (start != limit || (start == limit && array[limit] != 32)) {
            byte[] item = copy(array, start, limit + 1);

            if (resultIndex >= result.length) {
                result = Arrays.copyOf(result, result.length + 10);
            }
            result[resultIndex++] = item;
        }

        return Arrays.copyOf(result, resultIndex);
    }

    public static byte[][] split(byte[] array, int index, byte value) {
        int start = index;
        int end = 0;
        List<byte[]> result = new ArrayList<byte[]>();
        int limit = array.length - 1;

        for (int i = index; i < array.length; i++) {

            if (array[i] == value) {
                end = i;
                byte[] item = copy(array, start, end);
                result.add(item);
                start = end + 1;
                end = start;
            }

        }

        if (start != limit || (start == limit && array[limit] != 32)) {
            byte[] item = copy(array, start, limit + 1);
            result.add(item);
        }

        return result.toArray(new byte[0][]);
    }

    private static byte[] copy(byte[] origin, int start, int end) {
        int len = end - start;
        byte[] item = new byte[end - start];
        System.arraycopy(origin, start, item, 0, len);
        return item;
    }
}

Related

  1. split(byte[] pattern, byte[] src)
  2. split(byte[] source, int c)
  3. split(Collection collection, C[] array, int pageSize)
  4. split(final int[] array)