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

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...