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

Replacing Phobos' BufferedFile

Moderators: larsivi kris

Posted: 02/29/08 17:31:40

I'm porting some code from phobos, and have two use cases for BufferedFile? that I need a Tango way of doing. I originally expected to find classes that implement this in Tango, but I'm not sure anymore. Do I need to combine a FileConduit? and a BufferedStream? or something like that?

How would I do this: 1) Output some text to a file. Don't need formatting or anything, but would like to have buffering.

2) Read lines from a text file. With buffering.

I know how to use FileConduit?, TextFileInput?, LineIterator?, etc. I just need a straight forward way to add buffering. And I think my brain might be to small to fit the Tango IO package in it. So some hints would be welcome.

Author Message

Posted: 02/29/08 17:52:25

Well, this worked for me (for writing). I don't really understand a lot of the ideas in the IO package, but it might give you a hint:

FileConduit conduit;	// actual conduit; don't use directly when there's content in the buffer
IBuffer buffer;		// write strings directly to this (use opCall(void[]) )

// Open a conduit on the file:
conduit = new FileConduit (_path, FileConduit.WriteCreate);
scope(exit) conduit.close();

buffer = new Buffer(conduit);	// And a buffer
scope(exit) buffer.flush();

Posted: 02/29/08 18:59:51

Something like that was what I expected, but I'm just wondering what the 'canonical' way is, before I start converting my phobos code.

It also occurs to me that an example of this kind of thing is would be great to have in the docs. I know the docs are to be considered work in progress.

Posted: 02/29/08 19:20:35

torhu wrote:

I'm porting some code from phobos, and have two use cases for BufferedFile that I need a Tango way of doing. I originally expected to find classes that implement this in Tango, but I'm not sure anymore. Do I need to combine a FileConduit and a BufferedStream or something like that?

How would I do this: 1) Output some text to a file. Don't need formatting or anything, but would like to have buffering.

2) Read lines from a text file. With buffering.

I know how to use FileConduit, TextFileInput, LineIterator, etc. I just need a straight forward way to add buffering. And I think my brain might be to small to fit the Tango IO package in it. So some hints would be welcome.

A generic approach is to wrap a Buffer around a FileConduit, which gives you read/write buffering. If you want to use streams instead, then you can wrap a BufferStream around some other stream (such as FileStream):

output = new BufferOutput(new FileOutput("name"));

The same goes for input too. TextFileStream is quite handy, since it wraps all that up in one place.

Posted: 02/29/08 20:57:44

How do I know if something is buffered? Like will it always be buffered it it implements the interface Buffered?

Posted: 03/01/08 05:43:28

Turns out that TextFileOutput was not buffered, which is now fixed (to match the buffered input). Thanks for bringing this up.

Yes, anything implementing Buffered will perform buffered I/O

Posted: 03/01/08 16:13:43

Thanks. The docs for Buffered only talks about sharing a buffer. I wasn't sure if it was implied that a class implementing Buffered has to perform its own buffering in case it's not coupled with a stream that is buffered.

Maybe Buffer.share should be mentioned in connection with Buffered? :)

Posted: 03/02/08 21:51:42

Yes, you're right