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

Read file in chunks

Moderators: larsivi kris

Posted: 02/17/08 17:04:25

Hi Tango Fans,

I'm trying to transfer files using some kind of network stream. However I'm not sure how to translate this simple C# code to D-Tango..

while ((bytesRead = inputFileStream.Read(buffer, 0, buffer.Length)) > 0)
{
...
}

I'm using a FileStream? object to get the file in chunks (size = buffer.Length).

For me it looks like all those stream and conduits classes of Tango are trying to hide such a code but I must be able to control the data flow so it's exactly what I want to do..

Thanks!

Author Message

Posted: 02/17/08 20:55:55

All the Tango stream utilities represent a high-level layer above the device itself (a file, socket, console, etc). Some streams implement line-delineation, some support tokenizing, and so on.

You can read directly from an input stream into a local buffer also, using the stream.read(array) method, and you can write an output stream in a similar manner. That should do what you need it to. If you want to step down a level, you can communicate more directly with the underlying device - each stream has a conduit() method (a pseudonym for the device) which also has read(array) and write(array) methods.

In other words, you can read and write raw arrays from and input-stream or to an output-stream respectively. Devices/conduits have both these methods. The difference between a device/conduit and the stream utilities is this: many of the stream utilities are implicitly buffered, whereas a device/conduit is never implicitly buffered.

Getting back to your question, this is the equivalent code for an input-stream:

byte[256] buffer;
while ((count = input.read(buffer)) != input.Eof) 
        ...

The device underneath a FileInputStream is called FileConduit, which can be used in a similar manner as above .

Hope this helps to get you going?

Posted: 02/17/08 21:24:10

What a perfect answer! Thanks a lot!