Streaming transport interfaces: input
Denis Koroskin
2korden at gmail.com
Thu Oct 14 22:09:08 PDT 2010
On Fri, 15 Oct 2010 08:37:24 +0400, Brad Roberts <braddr at puremagic.com>
wrote:
> On 10/14/2010 8:34 AM, Andrei Alexandrescu wrote:
>> Starting a new thread from Denis' question:
>>
>>> Can we outline basic Stream interface now so that we could move on?
>>
>> Here's the input transport layer. Transport has no concern for
>> formatting - it
>> just moves stuff around.
>>
>
> I suggest having read and write with offset style apis. See also: man
> pread
>
> The value here is that you can safely concurrently access a file
> descriptor and
> not have to worry about atomicity of seek+read or seek+write at the app
> layer.
>
In my original API proposal I included reading from offsets:
interface SeekableInputStream : SeekableStream, InputStream
{
...
// reads up to buffer.length bytes from a stream
// returns number of bytes read
// throws on error
size_t read(ubyte[] buffer, long offset);
// reads up to buffer.length bytes from a specified offset
AsyncReadFromOffsetRequest readAsync(ubyte[] buffer, long offset,
Mailbox* mailbox = null);
...
}
interface SeekableOutputStream : SeekableStream, OutputStream
{
...
// returns number of bytes written
// throws on error
size_t write(const(ubyte)[] buffer, long offset);
// writes from from a specified offset
AsyncWriteFromOffsetRequest writeAsync(const(ubyte)[] buffer, long
offset, Mailbox* mailbox = null);
...
}
Although it complicates an API quite a bit, it has its advantages, too.
However, not all Stream types support reading/writing from an offset (e.g.
SocketStream doesn't).
More information about the Digitalmars-d
mailing list