If one ByteBuffer contains another ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer

Description

If one ByteBuffer contains another ByteBuffer

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public final static boolean contains(final ByteBuffer bb1,
            final ByteBuffer bb2) {
        return contains(bb1.array(), 0, bb1.limit(), bb2.array(), 0,
                bb2.limit());/*from w  w  w  . ja v  a  2s .  com*/
    }

    /**
     *
     * @param text
     * @param offset1
     * @param end1
     *            absolute
     * @param s
     * @return
     */
    public final static boolean contains(final byte[] array1,
            final int offset1, final int end1, final byte[] array2,
            final int offset2, final int end2) {
        final int len1 = end1 - offset1;
        final int len2 = end2 - offset2;
        if (len2 == 0) {
            return true;
        } else if (len1 >= len2) {
            byte b;
            int idx = offset2;
            for (int i = offset1; i < end1; i++) {
                b = array1[i];
                if (b == array2[idx]) {
                    if (++idx >= end2) {
                        return true;
                    }
                } else {
                    idx = offset2;
                }
            }
        }
        return false;
    }

    public final static boolean contains(final byte[] array1,
            final int offset1, final int end2, final byte[] array2) {
        return contains(array1, offset1, end2, array2, 0, array2.length);
    }
}

Related Tutorials