Java List Split split(List data, int max)

Here you can find the source of split(List data, int max)

Description

Split a list of Byte in block of max length.

License

Open Source License

Parameter

Parameter Description
data a parameter
max a parameter

Declaration

public static List<List<Byte>> split(List<Byte> data, int max) 

Method Source Code


//package com.java2s;
/*/*from w  w  w.ja va  2s.co  m*/
 * #%L
 * Katsu Commons Utils
 * %%
 * Copyright (C) 2013 Katsu
 * %%
 * This program 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.
 * 
 * This program 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 this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

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

public class Main {
    /**
     * Split a list of Byte in block of max length. This method no do padding.
     * @param data
     * @param max
     * @return
     */
    public static List<List<Byte>> split(List<Byte> data, int max) {
        List<List<Byte>> result = new ArrayList<List<Byte>>();
        if (data.size() <= max) {
            result.add(data);
        } else {
            List<Byte> aux = data.subList(0, max);
            result.add(aux);
            result.addAll(split(data.subList(max, data.size()), max));
        }
        return result;
    }

    /**
     * Split a list by a token represent by a list.
     * <br />
     * Any occurency of token is erase in result.
     * <br />
     * In result may have List<Byte> with 0 length
     * @param data
     * @param token
     * @return
     */
    public static List<List<Byte>> split(List<Byte> data, List<Byte> token) {
        List<List<Byte>> result = new ArrayList<List<Byte>>();
        if (data.size() < token.size()) {
            result.add(data);
            return result;
        } else {
            int i = indexOf(data, token);
            if (i < 0) {
                result.add(data);
                return result;
            } else {
                List<Byte> aux = data.subList(0, i);
                result.add(aux);
                if (i + token.size() < data.size())
                    result.addAll(split(data.subList(i + token.size(), data.size()), token));
                return result;
            }
        }
    }

    /**
     * Return the index of first occuernce of token in data.
     * <br />
     * return -1 if token is no found.
     * @param data
     * @param token
     * @return
     */
    public static int indexOf(List<Byte> data, List<Byte> token) {
        int result = -1;
        if (data.size() > token.size()) {
            List<Byte> aux;
            for (int i = 0; i <= data.size() - token.size(); i++) {
                aux = data.subList(i, i + token.size());
                if (aux.equals(token))
                    return i;
            }
        }
        return result;
    }
}

Related

  1. split(final List list, final int count)
  2. split(final List list, final int index)
  3. split(final List orginalList, final int splitSize)
  4. split(final List original, final int maxListSize, final Class listImplementation)
  5. split(final String line, final char delimiter, final List parts)
  6. split(List list)
  7. split(List list, String separator)
  8. split(List list, String separator)
  9. split(List list)