Replies: 2 comments 1 reply
-
I would read one byte at a time until you match the first byte in your byte string. When you do, use rangeEquals to check if the remaining bytes match at the head of the source. If they do, you've found your march. If they don't, continue looping and reading one byte at a time. Roughly: fun BufferedSource.seekPast(bytes: ByteString) {
require(bytes.isNotEmpty())
val firstByte = bytes[0]
while (true) {
if (readByte() == firstByte && rangeEquals(0, bytes, 1, bytes.length - 1) {
return true
}
}
} Note: untested and written on mobile! |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thanks to Jake's suggestion, I used fun BufferedSource.skipTo(bytes: ByteString): Long {
require(bytes.size != 0)
val firstByte = bytes[0]
var index = 0L
while (true) {
if (exhausted()) {
return -1L
}
require(1)
if (buffer[0] == firstByte && rangeEquals(0L, bytes, 1, bytes.size - 1)) {
return index
}
skip(1)
index++
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a document that may or may not have a sequence of bytes I'm looking for. I don't care about the data before this sequence of bytes, but I do care about the data after this sequence of bytes, if it exists.
First, I thought to do this:
Unfortunately, indexOf expands the buffer and will even buffer my entire document in memory if the ByteString is not in the document, when I really just want to skip through the document.
To fix that, I wrote up my own skip function, checking eight kilobytes at a time:
Is there an easier way to do this with Okio?
Beta Was this translation helpful? Give feedback.
All reactions