加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 如何在ByteString中获取迭代器的当前位置?

发布时间:2020-12-16 18:02:23 所属栏目:安全 来源:网络整理
导读:我有一个ByteString的实例.要从中读取数据,我应该使用它的iterator()方法. 我读了一些数据,然后我决定创建一个视图(一些数据块的独立迭代器). 我不能使用原始迭代器的slice(),因为这会使它无法使用,因为docs说: After calling this method,one should disca
我有一个ByteString的实例.要从中读取数据,我应该使用它的iterator()方法.

我读了一些数据,然后我决定创建一个视图(一些数据块的独立迭代器).

我不能使用原始迭代器的slice(),因为这会使它无法使用,因为docs说:

After calling this method,one should discard the iterator it was called on,and use only the iterator that was returned. Using the old
iterator is undefined,subject to change,and may result in changes to
the new iterator as well.

所以,似乎我需要在ByteString上调用slice().但是slice()有from和until参数,我不知道.我需要这样的东西:

ByteString originalByteString = ...; // <-- This is my input data
ByteIterator originalIterator = originalByteString .iterator();
...
read some data from originalIterator
...
int length = 100; // < -- Size of the view
int from = originalIterator.currentPosition(); // <-- I need this
int until = from + length;
ByteString viewOfOriginalByteString = originalByteString.slice(from,until);
ByteIterator iteratorForView = viewOfOriginalByteString.iterator(); // <-- This is my goal

更新:

尝试使用duplicate()执行此操作:

ByteIterator iteratorForView = originalIterator.duplicate()._2.take(length);

解决方法

字段中的ByteIterator是私有的,并且没有一种方法似乎只是简单地返回它.我所能建议的是使用originalIterator.duplicate来获取安全副本,或者使用反射来读取from字段来“欺骗”,假设您的部署环境中有可用的反射.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读