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