Java String Split split(byte[] input)

Here you can find the source of split(byte[] input)

Description

Spits the input array into separate byte arrays.

License

Open Source License

Parameter

Parameter Description
input the input array

Return

a new array of byte arrays

Declaration

public static byte[][] split(byte[] input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    /**/*from   ww  w.j a  v a2  s  .  co m*/
     * Spits the input array into separate byte arrays. Works similarly to <code>String.split()</code>, but always splits on a null byte (0x00).
     * @param input the input array
     * @return a new array of byte arrays
     */
    public static byte[][] split(byte[] input) {
        ArrayList<byte[]> temp = new ArrayList<byte[]>();

        byte[][] output;
        //excessively large, but this is the maximum size it can be (actually, less, but it's a good upper bound)
        output = new byte[input.length][input.length];
        int index_cache = 0;
        for (int i = 0; i < input.length; i++) {
            if (input[i] == 0x00) {
                byte[] b = subarray(input, index_cache, i - 1);
                temp.add(b);
                //note, this is the index *after* the null byte
                index_cache = i + 1;
            }
        }
        //get the remaining part
        //prevent duplication if there are no null bytes
        if (index_cache != 0) {
            byte[] b = subarray(input, index_cache, input.length - 1);
            temp.add(b);
        }

        output = new byte[temp.size()][input.length];
        for (int i = 0; i < temp.size(); i++) {
            output[i] = temp.get(i);
        }

        return output;
    }

    /**
     * Creates and returns a new array with the values of the original from index <code>a</code> to index <code>b</code>
     * and of size <code>(b-a)</code>.
     * @param in input array
     * @param a first index
     * @param b last index
     * @return a new array based on the desired range of the input
     */
    public static byte[] subarray(byte[] in, int a, int b) {
        if (b - a > in.length)
            return in;

        byte[] out = new byte[(b - a) + 1];

        for (int i = a; i <= b; i++) {
            out[i - a] = in[i];
        }
        return out;
    }
}

Related

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