Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isseekable to Stream/IO interface? #24242

Open
ssfrr opened this issue Oct 20, 2017 · 0 comments
Open

Add isseekable to Stream/IO interface? #24242

ssfrr opened this issue Oct 20, 2017 · 0 comments
Labels
io Involving the I/O subsystem: libuv, read, write, etc.

Comments

@ssfrr
Copy link
Contributor

ssfrr commented Oct 20, 2017

If I'm writing some code that takes an IO object externally, I might want to do different things based on whether the given stream is seekable, but as of now there isn't a standard way to check for it.

Here's a mock use-case that matches some real code I'm writing right now, where there's a leading length field in the file format that's should be written, but it's not the end of the world if its not:

struct AudioEncoder{T}
    io::T
    headerpos::Int64
    filelen::Int64
end

function AudioEncoder(io::IO)
    headerpos = position(io)
    write_len_header(io, 0xffffffffffffffff)
    
    AudioEncoder(io, headerpos, 0)
end

function AudioEncoder(f, io::IO)
    ae = AudioEncoder(io)
    try
        f(ae)
    finally
        close(ae)
    end
end

function write(ae::AudioEncoder, buf::SampleBuf)
    io.filelen += write_encoded_samples(ae.io, buf)
    length(buf)
end

function close(ae)
    if isseekable(ae.io) #this doesn't exist
        seek(ae.io, ae.headerpos)
        write_len_header(ae.io, ae.filelen)
    else
        # too bad, couldn't write the correct length, but the receiver can figure it out
    end
end

Which the user might use like so:

open("somefile", "w") do io
    AudioEncoder(io) do enc
        for _ in 1:1000
            write(enc, synthesize_audio_block())
        end
    end
end

At first it seems like this is a use-case for the mark API, but that requires you to just keep buffering the whole file, which could be gigabytes.

@ararslan ararslan added the io Involving the I/O subsystem: libuv, read, write, etc. label Oct 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
io Involving the I/O subsystem: libuv, read, write, etc.
Projects
None yet
Development

No branches or pull requests

2 participants