www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reading from a ubyte[] | byte[] as if from a stream? (Phobos)

reply Charles Hixson <charleshixsn earthlink.net> writes:
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.
Sep 18 2008
next sibling parent reply BCS <ao pathlink.com> writes:
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
Sep 18 2008
parent reply Tomas Lindquist Olsen <tomas famolsen.dk> writes:
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 :)
Sep 18 2008
parent Charles Hixson <charleshixsn earthlink.net> writes:
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 :)

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.)
Sep 18 2008
prev sibling parent Tomas Lindquist Olsen <tomas famolsen.dk> writes:
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
Sep 18 2008