Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ctor for MemoryConduit and misc. questions

Moderators: larsivi kris

Posted: 10/06/07 00:09:12

Recently I created a unit test with the following init code:

  const ubyte source[] =
  [
    0xAB,0xCD
  ];

  auto conduit = new MemoryConduit;
  conduit.write(source);

Is there a reason why MemoryConduit? has no ctor taking a void[] for initialization ? My code would then look like:

  const ubyte source[] =
  [
    0xAB,0xCD
  ];

  auto conduit = new MemoryConduit(source);

And some more questions: I had code working with std.Outbuffer from Phobos. Which classes from Tango would get closest to std.Outbuffer when it comes to functionality ? Is it a combination of MemoryConduit? and a Reader/Writer ? Or would you use a Buffer/GrowBuffer ? Thanks

KlausO

Author Message

Posted: 10/06/07 00:42:14

OutBuffer? maps to Buffer in Tango. In fact, Buffer plays a central role in Tango I/O. As well as Outbuffer style accumulation, it acts as a streaming host for conduit data, streaming tokenizer host, stack-based I/O host, data-conversion host, memory-mapped file facade, and a bunch of other things :)

I can't think of many reasons why one would choose MemoryConduit? over Buffer, since the latter is more capable. MemoryConduit? is present mostly for the sake of consistency (and certain debugging and repurposing tasks).

You'll note that both Buffer and Conduit implement InputStream? and OutputStream?. This means either can be used as a sink or source for many roles in the I/O package, and they can be bound together for buffered I/O. Where conduits gain traction is the optional set of filters you can attach to them.

Basically, it boils down to this: Conduits typically represent external entities such as files and sockets. You can read and write them directly, and they may have filters attached to mutate the data flow. Buffers are used as a switch-point for memory-based I/O, such the examples noted above. Either can be used independently, or they can be combined for fast block-based external I/O.

Hope that helps some?

- Kris

Posted: 10/06/07 01:08:34

I guess I missed part of your question.

Does the phobos Outbuffer do human-readable formatting, like printf() sorta thing? If so, then in Tango you perhaps combine something like a Buffer and a Print instance. The latter is what Stdout and friends use, and those are configured to hook up with the console conduit. If you want Stdout type functionality but directed to a file, socket, or memory, you can do something like this:

import tango.io.Print,
       tango.io.Buffer,
       tango.io.FileConduit;
import tango.text.convert.Layout;

auto layout = new Layout!(char);

Formatting to an array:

char[256] buf;
char[] result = layout.sprint (buf, "{} green bottles", 10);

Formatting to a file:

auto print = new Print!(char)(layout, new FileConduit("myFile", FileConduit.WriteCreate)); 

print.formatln ("{} green bottles", 10);

Formatting to a Buffer instance:

auto print = new Print!(char)(layout, new Buffer(1024)); 

print.formatln ("{} green bottles", 10);

Formatting to a socket:

auto sock = new SocketConduit;
auto print = new Print!(char)(layout, sock.connect(new InternetAddress("someaddress"))); 

print.formatln ("{} green bottles", 10);

(Print is currently missing a method to change the associated OutputStream, but that will be added shortly)

Posted: 10/06/07 11:48:35

Thank you very much, things are getting clearer now :)

Posted: 10/06/07 20:31:24

Welcome :)