首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较ByteBuffer内容?

比较ByteBuffer内容?
EN

Stack Overflow用户
提问于 2010-09-22 21:48:52
回答 2查看 8.4K关注 0票数 12

在Java语言中,比较两个ByteBuffers的内容以检查相等性的最简单方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-22 21:53:13

您也可以检查equals()方法。

告诉我们这个缓冲区是否等于另一个对象。

两个字节的缓冲区相等当且仅当,

  1. 它们具有相同的元素类型,
  2. 它们具有相同数量的剩余元素,而
  3. 两个剩余元素序列(独立于它们的起始位置)按点相等。

字节缓冲区不等于任何其他类型的对象。

票数 14
EN

Stack Overflow用户

发布于 2018-08-13 02:23:36

或者,使用JDK/11,有另一种比较ByteBuffer与另一a的方法。主要专注于查找两者之间的不匹配的API可以用作-

代码语言:javascript
复制
int mismatchBetweenTwoBuffers = byteBuffer1.mismatch(byteBuffer2);
if(mismatchBetweenTwoBuffers == -1) {
    System.out.println("The buffers are equal!");
} else {
    System.out.println("The buffers are mismatched at - " + mismatchBetweenTwoBuffers);
}

其文档内容如下:

代码语言:javascript
复制
/**
 * Finds and returns the relative index of the first mismatch between this
 * buffer and a given buffer.  The index is relative to the
 * {@link #position() position} of each buffer and will be in the range of
 * 0 (inclusive) up to the smaller of the {@link #remaining() remaining}
 * elements in each buffer (exclusive).
 *
 * <p> 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.
 * 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.
 *
 * @return  The relative index of the first mismatch between this and the
 *          given buffer, otherwise -1 if no mismatch.
 *
 * @since 11
 */
public int mismatch(ByteBuffer that)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3769891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档