ByteBuffer mismatch() in Java

mismatch(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

int java.nio.ByteBuffer.mismatch(ByteBuffer that)

This method takes one argument of type ByteBuffer as its parameter. This method finds and returns the relative index of the first mismatch between this buffer and a given buffer.

Note:

1. If the two buffers share a common prefix then the returned index is the length of the common prefix and it follows that there is a mismatch between the two buffers at that index within the respective buffers

2. If one buffer is a proper prefix of the other then the returned index is the smaller of the remaining elements in each buffer, and it follows that the index is only valid for the buffer with the larger number of remaining elements Otherwise, there is no mismatch.

Parameters: One parameter is required for this method.

that: The byte buffer to be tested for a mismatch with this buffer.

Returns: The relative index of the first mismatch between this and the given buffer, otherwise -1 if no mismatch.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBuffermismatch {
    public static void main(String[] args) {

        byte array[] = { 1234 };

        byte array2[] = { 1256 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        ByteBuffer that = ByteBuffer.wrap(array2);
        System.out.println(bb.mismatch(that));

    }
}

Output:

2

No comments:

Post a Comment