Java ByteBuffer Split split(ByteBuffer buffer, byte tag)

Here you can find the source of split(ByteBuffer buffer, byte tag)

Description

Split buffer by tag

License

Apache License

Parameter

Parameter Description
buffer a parameter
tag a parameter

Declaration

public static List<ByteBuffer> split(ByteBuffer buffer, byte tag) 

Method Source Code


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

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//ww  w  .  j  a  v  a  2  s .co m
     * Split buffer by tag
     * 
     * @param buffer
     * @param tag
     * @return
     */
    public static List<ByteBuffer> split(ByteBuffer buffer, byte tag) {
        List<ByteBuffer> list = new ArrayList<ByteBuffer>();
        byte[] srcArray = buffer.array();
        trim(buffer, tag);
        for (int i = 0, start = 0, end = 0; i < buffer.limit(); i++) {
            if (srcArray[i] != tag) {
                end = i;
            } else {
                int len = end - start + 1;
                if (len != 1 || srcArray[start] != tag) {
                    ByteBuffer tmp = ByteBuffer.allocate(len);
                    tmp.put(srcArray, start, len);
                    tmp.flip();
                    list.add(tmp);
                }
                start = i + 1;
                end = start;
            }
            if (i == buffer.limit() - 1 && start != end) {
                int len = end - start + 1;
                ByteBuffer tmp = ByteBuffer.allocate(len);
                tmp.put(srcArray, start, len);
                tmp.flip();
                list.add(tmp);
            }
        }
        return list;
    }

    /**
     * Trim tag in buffer
     * 
     * @param buffer
     * @param tag
     */
    public static void trim(ByteBuffer buffer, byte tag) {
        ltrim(buffer, tag);
        rtrim(buffer, tag);
    }

    /**
     * Left trim tag in buffer
     * 
     * @param buffer
     * @param tag
     */
    public static void ltrim(ByteBuffer buffer, byte tag) {
        byte[] srcArray = buffer.array();
        int p = 0;
        for (int i = 0; i < buffer.limit(); i++) {
            if (srcArray[i] != tag) {
                p = i;
                break;
            }
        }
        byte[] desArray = new byte[buffer.limit() - p];
        System.arraycopy(srcArray, p, desArray, 0, buffer.limit() - p);
        buffer.clear();
        buffer.put(desArray);
        buffer.flip();
    }

    /**
     * Right trim tag in buffer
     * 
     * @param buffer
     * @param tag
     */
    public static void rtrim(ByteBuffer buffer, byte tag) {
        byte[] srcArray = buffer.array();
        int p = buffer.limit() - 1;
        for (int i = buffer.limit() - 1; i >= 0; i--) {
            if (srcArray[i] != tag) {
                p = i;
                break;
            }
        }
        byte[] desArray = new byte[p + 1];
        System.arraycopy(srcArray, 0, desArray, 0, p + 1);
        buffer.clear();
        buffer.put(desArray);
        buffer.flip();
    }
}

Related

  1. split(ByteBuffer buffer, int position)
  2. split(ByteBuffer src, int unitSize)
  3. splitLongToBuffers(ByteBuffer buffer, ByteBuffer buffer1, long iValue)
  4. splitShortToBuffers(ByteBuffer buffer, ByteBuffer buffer1, short iValue)