Java String Split split(byte[] bytes, byte[] p)

Here you can find the source of split(byte[] bytes, byte[] p)

Description

Split bytes on pattern p.

License

Apache License

Declaration

public static List<byte[]> split(byte[] bytes, byte[] p) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from  www. j a v  a2 s  . co  m
     * Split bytes on pattern p.
     * */
    public static List<byte[]> split(byte[] bytes, byte[] p) {
        List<byte[]> result = new ArrayList<byte[]>();
        byte[] tem = bytes;
        for (int index = index(tem, p); index != -1; index = index(tem, p)) {
            result.add(subBytes(tem, 0, index));
            tem = subBytes(tem, index + p.length, tem.length - index - p.length);
            if (tem.length == 0) {
                break;
            }
        }
        if (tem.length > 0) {
            result.add(tem);
        }

        return result;
    }

    /**
     * p's index of bytes.
     * */
    public static int index(byte[] bytes, byte[] p) {
        for (int i = 0; i + p.length <= bytes.length; i++) {
            boolean match = true;
            for (int j = 0; j < p.length; j++) {
                if (bytes[i + j] != p[j]) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Sub bytes.
     * */
    public static byte[] subBytes(byte[] bytes, int index, int length) {
        byte[] result = new byte[length];
        System.arraycopy(bytes, index, result, 0, length);
        return result;
    }
}

Related

  1. getInputs(String str)
  2. getParameters(String[] split)
  3. getUsedInputs(String expression)
  4. recursiveSplit(String str, String splitor)
  5. safeSplitParameters(String parameters)
  6. split(byte[] data)
  7. split(byte[] input)
  8. split(byte[] input)
  9. split(final byte[] array, final int chunkSize)