# Is seeking a binary sequence on \`IOStream\` built in?

**URL:** <https://discourse.julialang.org/t/is-seeking-a-binary-sequence-on-iostream-built-in/10184>\
**Category:** Internals & Design\
**Tags:** binaryio\
**Created:** [April 5, 2018, 5:12pm UTC](https://discourse.julialang.org/t/is-seeking-a-binary-sequence-on-iostream-built-in/10184 "2018-04-05T17:12:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)\
**Post date:** [April 5, 2018, 5:12pm UTC](https://discourse.julialang.org/t/is-seeking-a-binary-sequence-on-iostream-built-in/10184/1 "2018-04-05T17:12:55Z")

</div>

I needed a function that I’m calling “seek\_binary\_sequence”. I’m writing binary data files, and looking for a “magic sequence” of bytes. I think a reasonable implementation is as follows

```julia
function seek_binary_sequence(io::IOStream, seq::AbstractArray{<:Number,1})
    atype = eltype(seq)
    ix=1
    while !eof(io) && ix <= length(seq)
        anum = read(io, atype)
        ix = (anum == seq[ix]) ? ix+1 : 1
    end
end

```

My hunch is others will want to do this as well, which prompts 2 questions,

1. Have I missed this implementation elsewhere in the core parts of Julia?
2. Is this something people should implement on their own whenever they need it?
3. Is this a reasonable candidate for adding to `Base` ?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)\
**Post date:** [April 6, 2018, 6:49am UTC](https://discourse.julialang.org/t/is-seeking-a-binary-sequence-on-iostream-built-in/10184/2 "2018-04-06T06:49:06Z")

</div>

This is not the same thing of what you want, but you can mimic the behavior with `readuntil` as follows:

```julia
julia> buf = IOBuffer("foobarbaz")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=9, maxsize=Inf, ptr=1, mark=-1)

julia> readuntil(buf, b"ob") # find magic bytes "ob"
2-element Array{UInt8,1}:
 0x66
 0x6f

julia> read(buf, String)
"arbaz"

```

I don’t know other simple ways. So, perhaps adding such a function, say `seekuntil`, to the Base may be useful.

Also you must be careful when reading multiple bytes from an I/O stream, because the stream may assume the different byte order as you expect.

* * *

[EDIT] The code above is for Julia 0.7-dev. On Julia 0.6, you may write:

```julia
julia> buf = IOBuffer("foobarbaz")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=9, maxsize=Inf, ptr=1, mark=-1)

julia> readuntil(buf, "ob")
"foob"

julia> String(read(buf))
"arbaz"

```

---

<div class="post-metadata">

**Author:** ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)\
**Post date:** [April 10, 2018, 9:49pm UTC](https://discourse.julialang.org/t/is-seeking-a-binary-sequence-on-iostream-built-in/10184/3 "2018-04-10T21:49:59Z")

</div>

@bicycle1885 Thank you for letting me know about `readuntil`. Also, that’s a good point about byte-order.

That seems like very similar functionality, except that it returns the contents up to (and including) the “magic sequence”. Wanting of `seekuntil` instead of `readuntil` may be somewhat rare, but the main advantage of `seekuntil` should be lower memory usage.

That said, the issue of byte-order seems to be the best reason to restrict the `delim` sequence to something like `Vector{UInt8}`.
