把byte数组 以某个字符分割为多个数组(参考 golang的实现)

    /**
     // Split slices s into all subslices separated by sep and returns a slice of
     // the subslices between those separators.
     // If sep is empty, Split splits after each UTF-8 sequence.
     // It is equivalent to SplitN with a count of -1.
      *
     * @param s
     * @param sep
     * @return
     */
    public static List<byte[]> split(byte[] s,byte[] sep){
         return genSplit(s, sep, 0, -1);
     }    
    

    /**
     // SplitN slices s into subslices separated by sep and returns a slice of
     // the subslices between those separators.
     // If sep is empty, SplitN splits after each UTF-8 sequence.
     // The count determines the number of subslices to return:
     //   n > 0: at most n subslices; the last subslice will be the unsplit remainder.
     //   n == 0: the result is nil (zero subslices)
     //   n < 0: all subslices
      *
     * @param s
     * @param sep
     * @param n
     * @return
     */
    public static List<byte[]> splitN(byte[] s,byte[] sep ,int n ) {
         return genSplit(s, sep, 0, n);
     }
    

    /**
     // Generic split: splits after each instance of sep,
     // including sepSave bytes of sep in the subslices.
     * @param s
     * @param sep
     * @param sepSave
     * @param n
     * @return
     */
    private static List<byte[]> genSplit(byte[] s,byte[] sep,int sepSave,int n){
        
        if( n == 0 ){
            return null;
        }
        if(null==sep || sep.length == 0 ){
            return split(s, n);
        }
    
        List<byte[]> a = new ArrayList<byte[]>();
        int i = 0;
        while(i<n || n<0){
            int m = -1;
            for(int j =0;j<s.length;j++){
                boolean isFound = true;
                for(int k = 0;k<sep.length;k++){
                    if(s[j+k]!=sep[k]){
                        isFound = false;
                        break;
                    }
                }
                if(isFound){
                    m = j;
                    break;
                }
            }

            if( m < 0 ){
                break;
            }
            a.add(subArray(s,0,m+sepSave));
            s = subArray(s,m+sep.length);
            i++;            
        }
        a.add( s );
        return a;
    }


    /**
     * Generate a subarray of a given byte array.
     *
     * @param input the input byte array
     * @param start the start index
     * @param end   the end index
     * @return a subarray of <tt>input</tt>, ranging from <tt>start</tt>
     *         (inclusively) to <tt>end</tt> (exclusively)
     */
    public static byte[] subArray(byte[] input, int start, int end)
    {
        byte[] result = new byte[end - start];
        System.arraycopy(input, start, result, 0, end - start);
        return result;
    }

    /**
     * Generate a subarray of a given byte array.
     *
     * @param input the input byte array
     * @param start the start index
     * @return a subarray of <tt>input</tt>, ranging from <tt>start</tt> to
     *         the end of the array
     */
    public static byte[] subArray(byte[] input, int start)
    {
        return subArray(input, start, input.length);
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值