How to get current iterator position in ByteString?

I have an instance ByteString. To read data from it, I have to use a method iterator().

I read some data and then decided that I needed to create a view (a separate iterator of some piece of data).

I can't use the slice()original iterator because it would make it unusable because the docs say that:

After calling this method, you should discard the iterator to which it was called, and use only the returned iterator. Using the old iterator is undefined, it can be modified and can lead to changes in the new iterator.

So it seems to me that I need to call slice()on ByteString. But it slice()has parameters fromand until, and I do not know from. I need something like this:

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

Update:

Tried to do this with duplicate():

ByteIterator iteratorForView = originalIterator.duplicate()._2.take(length);
+4
source share
1 answer
Field

ByteIterator fromis private, and none of the methods seem to just return it. All I can offer is to use originalIterator.duplicateto get a safe copy, or to “trick” using reflection to read the field from, assuming reflection is available in your deployment environment.

+3
source

Source: https://habr.com/ru/post/1568287/


All Articles