Thread overview
reading from a ubyte[] | byte[] as if from a stream? (Phobos)
Sep 18, 2008
Charles Hixson
Sep 18, 2008
BCS
Sep 18, 2008
Charles Hixson
September 18, 2008
Is it possible to read from a byte or ubyte array as if from a stream? It looks like it should be not only possible, but easy...but I haven't figured it out.

BufferedFile *MUST* do that kind of thing internally, but it feels like there ought to be a way to say "This array is your data, read from it!" rather than digging through the guts of it and copying all the relevant code into a separate code-space.
September 18, 2008
Reply to Charles,

> Is it possible to read from a byte or ubyte array as if from a stream?
> It looks like it should be not only possible, but easy...but I haven't
> figured it out.
> 
> BufferedFile *MUST* do that kind of thing internally, but it feels
> like there ought to be a way to say "This array is your data, read
> from it!" rather than digging through the guts of it and copying all
> the relevant code into a separate code-space.
> 

MemoryStream
http://www.digitalmars.com/d/1.0/phobos/std_stream.html


September 18, 2008
Charles Hixson wrote:
> Is it possible to read from a byte or ubyte array as if from a stream? It looks like it should be not only possible, but easy...but I haven't figured it out.
> 
> BufferedFile *MUST* do that kind of thing internally, but it feels like there ought to be a way to say "This array is your data, read from it!" rather than digging through the guts of it and copying all the relevant code into a separate code-space.

Now, I don't use phobos, but I think you're looking for std.stream.TArrayStream

something like:

---
auto data = new ubyte[1024];
auto str = new TArrayStream!(ubyte[])(data);
ubyte ub;
str.read(ub);
---

HTH
September 18, 2008
BCS wrote:
> Reply to Charles,
> 
>> Is it possible to read from a byte or ubyte array as if from a stream?
>> It looks like it should be not only possible, but easy...but I haven't
>> figured it out.
>>
>> BufferedFile *MUST* do that kind of thing internally, but it feels
>> like there ought to be a way to say "This array is your data, read
>> from it!" rather than digging through the guts of it and copying all
>> the relevant code into a separate code-space.
>>
> 
> MemoryStream
> http://www.digitalmars.com/d/1.0/phobos/std_stream.html
> 
> 

From what I read, MemoryStream copies, TArrayStream does not
Not sure what's wanted here though :)
September 18, 2008
Tomas Lindquist Olsen wrote:
> BCS wrote:
>> Reply to Charles,
>>
>>> Is it possible to read from a byte or ubyte array as if from a stream?
>>> It looks like it should be not only possible, but easy...but I haven't
>>> figured it out.
>>>
>>> BufferedFile *MUST* do that kind of thing internally, but it feels
>>> like there ought to be a way to say "This array is your data, read
>>> from it!" rather than digging through the guts of it and copying all
>>> the relevant code into a separate code-space.
>>>
>>
>> MemoryStream
>> http://www.digitalmars.com/d/1.0/phobos/std_stream.html
>>
>>
> 
>  From what I read, MemoryStream copies, TArrayStream does not
> Not sure what's wanted here though :)
Thanks to the both!

TArrayStream would be what I want, except that it seems to require that the compilation be in release mode.  I'm going to need to study just what that means.  But if it isn't what's needed, then MemoryStream seems a reasonable alternative.

(My suspicion is that it only constructs an array of bytes of memory if you don't pass it a buffer...but I haven't checked.  The documentation sure seems to say that it copies the buffer.)