微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个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字段来“欺骗”,假设您的部署环境中有可用的反射.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐